From 4dd6659abc264f752997bff3bf8bd528bc517a6a Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Fri, 15 Nov 2024 08:21:05 +0000 Subject: [PATCH 1/2] ci: [#1075] remove current coverage workflow --- .github/workflows/coverage.yaml | 85 --------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 .github/workflows/coverage.yaml diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml deleted file mode 100644 index 28c1be6d0..000000000 --- a/.github/workflows/coverage.yaml +++ /dev/null @@ -1,85 +0,0 @@ -name: Coverage - -on: - push: - branches: - - develop - pull_request_target: - branches: - - develop - -env: - CARGO_TERM_COLOR: always - -jobs: - report: - name: Report - environment: coverage - runs-on: ubuntu-latest - env: - CARGO_INCREMENTAL: "0" - RUSTFLAGS: "-Z profile -C codegen-units=1 -C opt-level=0 -C link-dead-code -C overflow-checks=off -Z panic_abort_tests -C panic=abort" - RUSTDOCFLAGS: "-Z profile -C codegen-units=1 -C opt-level=0 -C link-dead-code -C overflow-checks=off -Z panic_abort_tests -C panic=abort" - - steps: - - id: checkout_push - if: github.event_name == 'push' - name: Checkout Repository (Push) - uses: actions/checkout@v4 - - - id: checkout_pull_request_target - if: github.event_name == 'pull_request_target' - name: Checkout Repository (Pull Request Target) - uses: actions/checkout@v4 - with: - ref: "refs/pull/${{ github.event.pull_request.number }}/head" - - - id: setup - name: Setup Toolchain - uses: dtolnay/rust-toolchain@nightly - with: - toolchain: nightly - components: llvm-tools-preview - - - id: cache - name: Enable Workflow Cache - uses: Swatinem/rust-cache@v2 - - - id: tools - name: Install Tools - uses: taiki-e/install-action@v2 - with: - tool: grcov - - - id: check - name: Run Build Checks - run: cargo check --tests --benches --examples --workspace --all-targets --all-features - - - id: clean - name: Clean Build Directory - run: cargo clean - - - id: build - name: Pre-build Main Project - run: cargo build --workspace --all-targets --all-features --jobs 2 - - - id: build_tests - name: Pre-build Tests - run: cargo build --workspace --all-targets --all-features --tests --jobs 2 - - - id: test - name: Run Unit Tests - run: cargo test --tests --workspace --all-targets --all-features - - - id: coverage - name: Generate Coverage Report - uses: alekitto/grcov@v0.2 - - - id: upload - name: Upload Coverage Report - uses: codecov/codecov-action@v3 - with: - token: ${{ secrets.CODECOV_TOKEN }} - files: ${{ steps.coverage.outputs.report }} - verbose: true - fail_ci_if_error: true From 9d8174df6f0913abd65a90538619f9036cb38a13 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Fri, 15 Nov 2024 08:22:22 +0000 Subject: [PATCH 2/2] ci: [#1075] fix coverage report --- .cargo/config.toml | 1 + .github/workflows/generate_coverage.yaml | 87 +++++++++++++++++ .github/workflows/upload_coverage.yaml | 119 +++++++++++++++++++++++ .gitignore | 6 +- 4 files changed, 211 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/generate_coverage.yaml create mode 100644 .github/workflows/upload_coverage.yaml diff --git a/.cargo/config.toml b/.cargo/config.toml index a88db5f38..28cde74ec 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,6 +1,7 @@ [alias] cov = "llvm-cov" cov-lcov = "llvm-cov --lcov --output-path=./.coverage/lcov.info" +cov-codecov = "llvm-cov --codecov --output-path=./.coverage/codecov.json" cov-html = "llvm-cov --html" time = "build --timings --all-targets" diff --git a/.github/workflows/generate_coverage.yaml b/.github/workflows/generate_coverage.yaml new file mode 100644 index 000000000..8de299c74 --- /dev/null +++ b/.github/workflows/generate_coverage.yaml @@ -0,0 +1,87 @@ +name: Generate Coverage Report + +on: + push: + branches: + - develop + pull_request: + branches: + - develop + +env: + CARGO_TERM_COLOR: always + +jobs: + coverage: + name: Generate Coverage Report + environment: coverage + runs-on: ubuntu-latest + env: + CARGO_INCREMENTAL: "0" + RUSTFLAGS: "-Cinstrument-coverage" + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install LLVM tools + run: sudo apt-get update && sudo apt-get install -y llvm + + - id: setup + name: Setup Toolchain + uses: dtolnay/rust-toolchain@nightly + with: + toolchain: nightly + components: llvm-tools-preview + + - id: cache + name: Enable Workflow Cache + uses: Swatinem/rust-cache@v2 + + - id: tools + name: Install Tools + uses: taiki-e/install-action@v2 + with: + tool: grcov,cargo-llvm-cov + + - id: coverage + name: Generate Coverage Report + run: | + cargo clean + cargo llvm-cov --all-features --workspace --codecov --output-path ./codecov.json + + - name: Store PR number and commit SHA + run: | + echo "Storing PR number ${{ github.event.number }}" + echo "${{ github.event.number }}" > pr_number.txt + + echo "Storing commit SHA ${{ github.event.pull_request.head.sha }}" + echo "${{ github.event.pull_request.head.sha }}" > commit_sha.txt + + # Workaround for https://github.com/orgs/community/discussions/25220 + # Triggered sub-workflow is not able to detect the original commit/PR which is available + # in this workflow. + - name: Store PR number + uses: actions/upload-artifact@v4 + with: + name: pr_number + path: pr_number.txt + + - name: Store commit SHA + uses: actions/upload-artifact@v4 + with: + name: commit_sha + path: commit_sha.txt + + # This stores the coverage report in artifacts. The actual upload to Codecov + # is executed by a different workflow `upload_coverage.yml`. The reason for this + # split is because `on.pull_request` workflows don't have access to secrets. + - name: Store coverage report in artifacts + uses: actions/upload-artifact@v4 + with: + name: codecov_report + path: ./codecov.json + + - run: | + echo "The coverage report was stored in Github artifacts." + echo "It will be uploaded to Codecov using [upload_coverage.yml] workflow shortly." diff --git a/.github/workflows/upload_coverage.yaml b/.github/workflows/upload_coverage.yaml new file mode 100644 index 000000000..b9a65ae7c --- /dev/null +++ b/.github/workflows/upload_coverage.yaml @@ -0,0 +1,119 @@ +name: Upload Coverage Report + +on: + # This workflow is triggered after every successfull execution + # of `Generate Coverage Report` workflow. + workflow_run: + workflows: ["Generate Coverage Report"] + types: + - completed + +permissions: + actions: write + contents: write + issues: write + pull-requests: write + +jobs: + coverage: + name: Upload Coverage Report + environment: coverage + runs-on: ubuntu-latest + steps: + - name: "Download existing coverage report" + id: prepare_report + uses: actions/github-script@v7 + with: + script: | + var fs = require('fs'); + + // List artifacts of the workflow run that triggered this workflow + var artifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.payload.workflow_run.id, + }); + + let codecovReport = artifacts.data.artifacts.filter((artifact) => { + return artifact.name == "codecov_report"; + }); + + if (codecovReport.length != 1) { + throw new Error("Unexpected number of {codecov_report} artifacts: " + codecovReport.length); + } + + var download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: codecovReport[0].id, + archive_format: 'zip', + }); + fs.writeFileSync('codecov_report.zip', Buffer.from(download.data)); + + let prNumber = artifacts.data.artifacts.filter((artifact) => { + return artifact.name == "pr_number"; + }); + + if (prNumber.length != 1) { + throw new Error("Unexpected number of {pr_number} artifacts: " + prNumber.length); + } + + var download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: prNumber[0].id, + archive_format: 'zip', + }); + fs.writeFileSync('pr_number.zip', Buffer.from(download.data)); + + let commitSha = artifacts.data.artifacts.filter((artifact) => { + return artifact.name == "commit_sha"; + }); + + if (commitSha.length != 1) { + throw new Error("Unexpected number of {commit_sha} artifacts: " + commitSha.length); + } + + var download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: commitSha[0].id, + archive_format: 'zip', + }); + fs.writeFileSync('commit_sha.zip', Buffer.from(download.data)); + + - id: parse_previous_artifacts + run: | + unzip codecov_report.zip + unzip pr_number.zip + unzip commit_sha.zip + + echo "Detected PR is: $(> "$GITHUB_OUTPUT" + echo "override_commit=$(> "$GITHUB_OUTPUT" + + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ steps.parse_previous_artifacts.outputs.override_commit || '' }} + path: repo_root + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v5 + with: + verbose: true + token: ${{ secrets.CODECOV_TOKEN }} + files: ${{ github.workspace }}/codecov.json + fail_ci_if_error: true + # Manual overrides for these parameters are needed because automatic detection + # in codecov-action does not work for non-`pull_request` workflows. + # In `main` branch push, these default to empty strings since we want to run + # the analysis on HEAD. + override_commit: ${{ steps.parse_previous_artifacts.outputs.override_commit || '' }} + override_pr: ${{ steps.parse_previous_artifacts.outputs.override_pr || '' }} + working-directory: ${{ github.workspace }}/repo_root + # Location where coverage report files are searched for + directory: ${{ github.workspace }} diff --git a/.gitignore b/.gitignore index b60b28991..d9087bcff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .env +*.code-workspace **/*.rs.bk /.coverage/ /.idea/ @@ -12,5 +13,6 @@ /tracker.* /tracker.toml callgrind.out -perf.data* -*.code-workspace \ No newline at end of file +codecov.json +lcov.info +perf.data* \ No newline at end of file