|
| 1 | +name: coverage |
| 2 | +permissions: |
| 3 | + contents: read |
| 4 | + actions: write |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: [ "main" ] |
| 8 | + pull_request: |
| 9 | + branches: [ "main" ] |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + runs-on: |
| 13 | + description: "Runner type" |
| 14 | + type: choice |
| 15 | + options: |
| 16 | + - ubuntu-24.04-arm |
| 17 | + - ubuntu-latest |
| 18 | + - aliyun-ecs-x64 |
| 19 | + default: ubuntu-latest |
| 20 | + rebuildDiskCache: |
| 21 | + description: "Rebuild disk cache" |
| 22 | + type: boolean |
| 23 | + default: false |
| 24 | +concurrency: |
| 25 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}-${{ inputs.runs-on || vars.COVERAGE_RUNS_ON || 'ubuntu-latest' }} |
| 26 | + cancel-in-progress: ${{ github.event_name == 'pull_request' }} |
| 27 | +jobs: |
| 28 | + coverage-github: |
| 29 | + outputs: |
| 30 | + coverage-artifact-url: ${{ steps.upload_coverage_artifacts.outputs.artifact-url }} |
| 31 | + if: ${{ (inputs.runs-on || vars.COVERAGE_RUNS_ON || 'ubuntu-latest') == 'ubuntu-latest' || (inputs.runs-on || vars.COVERAGE_RUNS_ON || 'ubuntu-latest') == 'ubuntu-24.04-arm' }} |
| 32 | + name: coverage |
| 33 | + runs-on: ${{ inputs.runs-on || vars.COVERAGE_RUNS_ON || 'ubuntu-latest' }} |
| 34 | + container: |
| 35 | + image: ghcr.io/alibaba/tair-kvcache-kvcm-dev:2026_02_13_12_03_24230b1 |
| 36 | + volumes: |
| 37 | + - /:/host_root/ |
| 38 | + options: --privileged |
| 39 | + steps: |
| 40 | + # https://github.com/actions/runner-images/issues/2840 |
| 41 | + - name: Free up disk space |
| 42 | + run: | |
| 43 | + echo "Disk space before cleanup:" |
| 44 | + df -h |
| 45 | + rm -rf /host_root/usr/share/dotnet |
| 46 | + rm -rf /host_root/usr/local/lib/android |
| 47 | + rm -rf /host_root/opt/ghc |
| 48 | + echo "Disk space after cleanup:" |
| 49 | + df -h |
| 50 | +
|
| 51 | + - &checkout_step |
| 52 | + name: Checkout repository |
| 53 | + uses: actions/checkout@v4 |
| 54 | + with: |
| 55 | + fetch-depth: 0 |
| 56 | + |
| 57 | + - &setup_bazel_step |
| 58 | + name: setup_bazel |
| 59 | + uses: bazel-contrib/setup-bazel@0.18.0 |
| 60 | + with: |
| 61 | + bazelisk-cache: true |
| 62 | + disk-cache: ${{ runner.os }}-${{ github.workflow }} |
| 63 | + repository-cache: true |
| 64 | + cache-save: ${{ github.event_name == 'push' || (inputs.rebuildDiskCache || false) }} |
| 65 | + |
| 66 | + - &clean_disk_cache_step |
| 67 | + name: clean_disk_cache |
| 68 | + if: ${{ inputs.rebuildDiskCache || false }} |
| 69 | + run: | |
| 70 | + rm -rf ~/.cache/bazel-disk |
| 71 | +
|
| 72 | + - &install_coverage_tools_step |
| 73 | + name: install_coverage_tools |
| 74 | + run: | |
| 75 | + set -e |
| 76 | + set -x |
| 77 | + if ! command -v genhtml >/dev/null 2>&1; then |
| 78 | + if command -v apt-get >/dev/null 2>&1; then |
| 79 | + apt-get update -qq |
| 80 | + DEBIAN_FRONTEND=noninteractive apt-get install -y -qq lcov |
| 81 | + elif command -v yum >/dev/null 2>&1; then |
| 82 | + yum install -y -q lcov |
| 83 | + else |
| 84 | + echo "No supported package manager found for installing lcov/genhtml" >&2 |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + fi |
| 88 | + genhtml --version |
| 89 | +
|
| 90 | + - &bazel_coverage_step |
| 91 | + name: bazel_coverage |
| 92 | + env: |
| 93 | + COVERAGE_BASE_REF: ${{ github.event.pull_request.base.sha || github.event.before || 'origin/main' }} |
| 94 | + COVERAGE_XTRACE: "1" |
| 95 | + run: | |
| 96 | + set -e |
| 97 | + tools/coverage/run_coverage.sh \ |
| 98 | + --base-ref "${COVERAGE_BASE_REF:-origin/main}" \ |
| 99 | + --head-ref HEAD \ |
| 100 | + --output-dir coverage \ |
| 101 | + --include-prefix kv_cache_manager/ \ |
| 102 | + --jobs 8 \ |
| 103 | + --local-test-jobs 8 \ |
| 104 | + --test-timeout 900 \ |
| 105 | + --test-output errors \ |
| 106 | + --fetch-main \ |
| 107 | + -- \ |
| 108 | + //kv_cache_manager/... \ |
| 109 | + //integration_test/... |
| 110 | +
|
| 111 | + - &delete_old_disk_cache_step |
| 112 | + name: delete_old_disk_cache |
| 113 | + if: ${{ github.event_name == 'push' || (inputs.rebuildDiskCache || false) }} |
| 114 | + env: |
| 115 | + GH_TOKEN: ${{ github.token }} |
| 116 | + run: | |
| 117 | + set -x |
| 118 | + ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/') |
| 119 | + curl -sLo /usr/local/bin/jq "https://github.com/jqlang/jq/releases/download/jq-1.8.1/jq-linux-${ARCH}" && chmod +x /usr/local/bin/jq |
| 120 | + GH_VER=2.89.0 |
| 121 | + curl -sL "https://github.com/cli/cli/releases/download/v${GH_VER}/gh_${GH_VER}_linux_${ARCH}.tar.gz" | tar xz --strip-components=1 -C /usr/local |
| 122 | + RUNNER_ARCH_LOWER=$(echo "${{ runner.arch }}" | tr '[:upper:]' '[:lower:]') |
| 123 | + DISK_CACHE_NAME="disk-${{ runner.os }}-${{ github.workflow }}" |
| 124 | + gh cache list --repo ${{ github.repository }} --ref "${{ github.ref }}" --json id,key --limit 100 | \ |
| 125 | + jq -r --arg arch "$RUNNER_ARCH_LOWER" --arg name "$DISK_CACHE_NAME" \ |
| 126 | + '.[] | select((.key | contains($arch)) and (.key | contains($name))) | .id' | \ |
| 127 | + xargs -I {} gh cache delete {} --repo ${{ github.repository }} || true |
| 128 | +
|
| 129 | + - &append_coverage_summary_step |
| 130 | + name: Append coverage summary |
| 131 | + if: ${{ always() && hashFiles('coverage/coverage-summary.md') != '' }} |
| 132 | + run: | |
| 133 | + cat coverage/coverage-summary.md >> "${GITHUB_STEP_SUMMARY}" |
| 134 | + if [ -f coverage/html/index.html ]; then |
| 135 | + { |
| 136 | + echo "" |
| 137 | + echo "HTML coverage report: \`coverage/html/index.html\` in the uploaded artifact." |
| 138 | + } >> "${GITHUB_STEP_SUMMARY}" |
| 139 | + fi |
| 140 | +
|
| 141 | + - &upload_coverage_artifacts_step |
| 142 | + name: Upload coverage artifacts |
| 143 | + id: upload_coverage_artifacts |
| 144 | + uses: actions/upload-artifact@v6 |
| 145 | + if: always() |
| 146 | + with: |
| 147 | + name: coverage-report |
| 148 | + path: | |
| 149 | + coverage/** |
| 150 | + bazel-testlogs/**/*.xml |
| 151 | + if-no-files-found: warn |
| 152 | + overwrite: true |
| 153 | + |
| 154 | + coverage-aliyun: |
| 155 | + outputs: |
| 156 | + coverage-artifact-url: ${{ steps.upload_coverage_artifacts.outputs.artifact-url }} |
| 157 | + if: ${{ (inputs.runs-on || vars.COVERAGE_RUNS_ON || 'ubuntu-latest') == 'aliyun-ecs-x64' }} |
| 158 | + name: coverage |
| 159 | + runs-on: aliyun-ecs-x64 |
| 160 | + container: |
| 161 | + image: ghcr.io/alibaba/tair-kvcache-kvcm-dev:2026_02_13_12_03_24230b1 |
| 162 | + options: --privileged |
| 163 | + steps: |
| 164 | + - *checkout_step |
| 165 | + - *setup_bazel_step |
| 166 | + - *clean_disk_cache_step |
| 167 | + - *install_coverage_tools_step |
| 168 | + - *bazel_coverage_step |
| 169 | + - *delete_old_disk_cache_step |
| 170 | + - *append_coverage_summary_step |
| 171 | + - *upload_coverage_artifacts_step |
| 172 | + |
| 173 | + comment-pr-coverage: |
| 174 | + if: ${{ always() && github.event_name == 'pull_request' && (needs.coverage-github.outputs.coverage-artifact-url != '' || needs.coverage-aliyun.outputs.coverage-artifact-url != '') }} |
| 175 | + needs: |
| 176 | + - coverage-github |
| 177 | + - coverage-aliyun |
| 178 | + name: comment_pr_coverage |
| 179 | + runs-on: ubuntu-latest |
| 180 | + permissions: |
| 181 | + actions: read |
| 182 | + issues: write |
| 183 | + steps: |
| 184 | + - name: Download coverage artifact |
| 185 | + uses: actions/download-artifact@v6 |
| 186 | + continue-on-error: true |
| 187 | + with: |
| 188 | + name: coverage-report |
| 189 | + path: coverage-artifact |
| 190 | + |
| 191 | + - name: Comment PR coverage |
| 192 | + uses: actions/github-script@v8 |
| 193 | + env: |
| 194 | + COVERAGE_ARTIFACT_URL: ${{ needs.coverage-github.outputs.coverage-artifact-url || needs.coverage-aliyun.outputs.coverage-artifact-url }} |
| 195 | + with: |
| 196 | + script: | |
| 197 | + const fs = require('fs'); |
| 198 | +
|
| 199 | + const marker = '<!-- tair-kvcache-coverage-report -->'; |
| 200 | + const summaryPath = 'coverage-artifact/coverage/coverage-summary.md'; |
| 201 | + if (!fs.existsSync(summaryPath)) { |
| 202 | + core.warning(`Coverage summary not found at ${summaryPath}; skip coverage comment.`); |
| 203 | + return; |
| 204 | + } |
| 205 | +
|
| 206 | + let summary = fs.readFileSync(summaryPath, 'utf8').trim(); |
| 207 | + const maxSummaryLength = 60000; |
| 208 | + if (summary.length > maxSummaryLength) { |
| 209 | + summary = `${summary.slice(0, maxSummaryLength)}\n\n_Comment truncated; see the uploaded artifact for the full report._`; |
| 210 | + } |
| 211 | +
|
| 212 | + const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 213 | + const artifactUrl = process.env.COVERAGE_ARTIFACT_URL || ''; |
| 214 | + const links = artifactUrl |
| 215 | + ? `HTML report and raw LCOV: [coverage-report](${artifactUrl})` |
| 216 | + : `Workflow run: [${context.runId}](${runUrl})`; |
| 217 | + const body = [ |
| 218 | + marker, |
| 219 | + summary, |
| 220 | + '', |
| 221 | + links, |
| 222 | + `Workflow run: [${context.runId}](${runUrl})`, |
| 223 | + ].join('\n'); |
| 224 | +
|
| 225 | + const issue_number = context.payload.pull_request?.number; |
| 226 | + if (!issue_number) { |
| 227 | + core.warning('No pull request number found; skip coverage comment.'); |
| 228 | + return; |
| 229 | + } |
| 230 | +
|
| 231 | + try { |
| 232 | + const { owner, repo } = context.repo; |
| 233 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 234 | + owner, |
| 235 | + repo, |
| 236 | + issue_number, |
| 237 | + per_page: 100, |
| 238 | + }); |
| 239 | + const existing = comments.find((comment) => comment.body?.includes(marker)); |
| 240 | +
|
| 241 | + if (existing) { |
| 242 | + await github.rest.issues.updateComment({ |
| 243 | + owner, |
| 244 | + repo, |
| 245 | + comment_id: existing.id, |
| 246 | + body, |
| 247 | + }); |
| 248 | + } else { |
| 249 | + await github.rest.issues.createComment({ |
| 250 | + owner, |
| 251 | + repo, |
| 252 | + issue_number, |
| 253 | + body, |
| 254 | + }); |
| 255 | + } |
| 256 | + } catch (error) { |
| 257 | + core.warning(`Failed to write PR coverage comment: ${error.message}`); |
| 258 | + } |
| 259 | +
|
| 260 | + validate-runner-selection: |
| 261 | + if: ${{ (inputs.runs-on || vars.COVERAGE_RUNS_ON || 'ubuntu-latest') != 'ubuntu-latest' && (inputs.runs-on || vars.COVERAGE_RUNS_ON || 'ubuntu-latest') != 'ubuntu-24.04-arm' && (inputs.runs-on || vars.COVERAGE_RUNS_ON || 'ubuntu-latest') != 'aliyun-ecs-x64' }} |
| 262 | + name: validate_runner_selection |
| 263 | + runs-on: ubuntu-latest |
| 264 | + steps: |
| 265 | + - name: fail_invalid_runner_selection |
| 266 | + run: | |
| 267 | + echo "::error::Invalid runner selection. Set runs-on or COVERAGE_RUNS_ON to one of: ubuntu-latest, ubuntu-24.04-arm, aliyun-ecs-x64." |
| 268 | + exit 1 |
0 commit comments