Skip to content

Commit 9799cc5

Browse files
committed
ci: [#770] fix coverage workflow
1 parent 720c046 commit 9799cc5

File tree

3 files changed

+219
-40
lines changed

3 files changed

+219
-40
lines changed

.github/workflows/coverage.yaml

+12-40
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,25 @@ on:
44
push:
55
branches:
66
- develop
7-
pull_request_target:
8-
branches:
9-
- develop
107

118
env:
129
CARGO_TERM_COLOR: always
1310

1411
jobs:
1512
report:
16-
name: Report
13+
name: Generate Coverage Report
1714
environment: coverage
1815
runs-on: ubuntu-latest
1916
env:
2017
CARGO_INCREMENTAL: "0"
21-
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"
22-
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"
18+
RUSTFLAGS: "-Cinstrument-coverage"
2319

2420
steps:
25-
- id: checkout_push
26-
if: github.event_name == 'push'
27-
name: Checkout Repository (Push)
21+
- name: Checkout repository
2822
uses: actions/checkout@v4
2923

30-
- id: checkout_pull_request_target
31-
if: github.event_name == 'pull_request_target'
32-
name: Checkout Repository (Pull Request Target)
33-
uses: actions/checkout@v4
34-
with:
35-
ref: "refs/pull/${{ github.event.pull_request.number }}/head"
24+
- name: Install LLVM tools
25+
run: sudo apt-get update && sudo apt-get install -y llvm
3626

3727
- id: setup
3828
name: Setup Toolchain
@@ -49,41 +39,23 @@ jobs:
4939
name: Install Tools
5040
uses: taiki-e/install-action@v2
5141
with:
52-
tool: grcov
42+
tool: grcov,cargo-llvm-cov
5343

5444
- id: imdl
5545
name: Install Intermodal
5646
run: cargo install imdl
5747

58-
- id: check
59-
name: Run Build Checks
60-
run: cargo check --tests --benches --examples --workspace --all-targets --all-features
61-
62-
- id: clean
63-
name: Clean Build Directory
64-
run: cargo clean
65-
66-
- id: build
67-
name: Pre-build Main Project
68-
run: cargo build --workspace --all-targets --all-features --jobs 2
69-
70-
- id: build_tests
71-
name: Pre-build Tests
72-
run: cargo build --workspace --all-targets --all-features --tests --jobs 2
73-
74-
- id: test
75-
name: Run Unit Tests
76-
run: cargo test --tests --workspace --all-targets --all-features
77-
7848
- id: coverage
7949
name: Generate Coverage Report
80-
uses: alekitto/grcov@v0.2
50+
run: |
51+
cargo clean
52+
cargo llvm-cov --all-features --workspace --codecov --output-path ./codecov.json
8153
8254
- id: upload
8355
name: Upload Coverage Report
84-
uses: codecov/codecov-action@v4
56+
uses: codecov/codecov-action@v5
8557
with:
86-
token: ${{ secrets.CODECOV_TOKEN }}
87-
files: ${{ steps.coverage.outputs.report }}
8858
verbose: true
59+
token: ${{ secrets.CODECOV_TOKEN }}
60+
files: ${{ github.workspace }}/codecov.json
8961
fail_ci_if_error: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
name: Generate Coverage Report (PR)
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- develop
7+
8+
env:
9+
CARGO_TERM_COLOR: always
10+
11+
jobs:
12+
coverage:
13+
name: Generate Coverage Report
14+
environment: coverage
15+
runs-on: ubuntu-latest
16+
env:
17+
CARGO_INCREMENTAL: "0"
18+
RUSTFLAGS: "-Cinstrument-coverage"
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
- name: Install LLVM tools
25+
run: sudo apt-get update && sudo apt-get install -y llvm
26+
27+
- id: setup
28+
name: Setup Toolchain
29+
uses: dtolnay/rust-toolchain@nightly
30+
with:
31+
toolchain: nightly
32+
components: llvm-tools-preview
33+
34+
- id: cache
35+
name: Enable Workflow Cache
36+
uses: Swatinem/rust-cache@v2
37+
38+
- id: tools
39+
name: Install Tools
40+
uses: taiki-e/install-action@v2
41+
with:
42+
tool: grcov,cargo-llvm-cov
43+
44+
- id: imdl
45+
name: Install Intermodal
46+
run: cargo install imdl
47+
48+
- id: coverage
49+
name: Generate Coverage Report
50+
run: |
51+
cargo clean
52+
cargo llvm-cov --all-features --workspace --codecov --output-path ./codecov.json
53+
54+
- name: Store PR number and commit SHA
55+
run: |
56+
echo "Storing PR number ${{ github.event.number }}"
57+
echo "${{ github.event.number }}" > pr_number.txt
58+
59+
echo "Storing commit SHA ${{ github.event.pull_request.head.sha }}"
60+
echo "${{ github.event.pull_request.head.sha }}" > commit_sha.txt
61+
62+
# Workaround for https://github.com/orgs/community/discussions/25220
63+
# Triggered sub-workflow is not able to detect the original commit/PR which is available
64+
# in this workflow.
65+
- name: Store PR number
66+
uses: actions/upload-artifact@v4
67+
with:
68+
name: pr_number
69+
path: pr_number.txt
70+
71+
- name: Store commit SHA
72+
uses: actions/upload-artifact@v4
73+
with:
74+
name: commit_sha
75+
path: commit_sha.txt
76+
77+
# This stores the coverage report in artifacts. The actual upload to Codecov
78+
# is executed by a different workflow `upload_coverage.yml`. The reason for this
79+
# split is because `on.pull_request` workflows don't have access to secrets.
80+
- name: Store coverage report in artifacts
81+
uses: actions/upload-artifact@v4
82+
with:
83+
name: codecov_report
84+
path: ./codecov.json
85+
86+
- run: |
87+
echo "The coverage report was stored in Github artifacts."
88+
echo "It will be uploaded to Codecov using [upload_coverage.yml] workflow shortly."
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
name: Upload Coverage Report (PR)
2+
3+
on:
4+
# This workflow is triggered after every successfull execution
5+
# of `Generate Coverage Report` workflow.
6+
workflow_run:
7+
workflows: ["Generate Coverage Report (PR)"]
8+
types:
9+
- completed
10+
11+
permissions:
12+
actions: write
13+
contents: write
14+
issues: write
15+
pull-requests: write
16+
17+
jobs:
18+
coverage:
19+
name: Upload Coverage Report
20+
environment: coverage
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: "Download existing coverage report"
24+
id: prepare_report
25+
uses: actions/github-script@v7
26+
with:
27+
script: |
28+
var fs = require('fs');
29+
30+
// List artifacts of the workflow run that triggered this workflow
31+
var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
32+
owner: context.repo.owner,
33+
repo: context.repo.repo,
34+
run_id: context.payload.workflow_run.id,
35+
});
36+
37+
let codecovReport = artifacts.data.artifacts.filter((artifact) => {
38+
return artifact.name == "codecov_report";
39+
});
40+
41+
if (codecovReport.length != 1) {
42+
throw new Error("Unexpected number of {codecov_report} artifacts: " + codecovReport.length);
43+
}
44+
45+
var download = await github.rest.actions.downloadArtifact({
46+
owner: context.repo.owner,
47+
repo: context.repo.repo,
48+
artifact_id: codecovReport[0].id,
49+
archive_format: 'zip',
50+
});
51+
fs.writeFileSync('codecov_report.zip', Buffer.from(download.data));
52+
53+
let prNumber = artifacts.data.artifacts.filter((artifact) => {
54+
return artifact.name == "pr_number";
55+
});
56+
57+
if (prNumber.length != 1) {
58+
throw new Error("Unexpected number of {pr_number} artifacts: " + prNumber.length);
59+
}
60+
61+
var download = await github.rest.actions.downloadArtifact({
62+
owner: context.repo.owner,
63+
repo: context.repo.repo,
64+
artifact_id: prNumber[0].id,
65+
archive_format: 'zip',
66+
});
67+
fs.writeFileSync('pr_number.zip', Buffer.from(download.data));
68+
69+
let commitSha = artifacts.data.artifacts.filter((artifact) => {
70+
return artifact.name == "commit_sha";
71+
});
72+
73+
if (commitSha.length != 1) {
74+
throw new Error("Unexpected number of {commit_sha} artifacts: " + commitSha.length);
75+
}
76+
77+
var download = await github.rest.actions.downloadArtifact({
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
artifact_id: commitSha[0].id,
81+
archive_format: 'zip',
82+
});
83+
fs.writeFileSync('commit_sha.zip', Buffer.from(download.data));
84+
85+
- id: parse_previous_artifacts
86+
run: |
87+
unzip codecov_report.zip
88+
unzip pr_number.zip
89+
unzip commit_sha.zip
90+
91+
echo "Detected PR is: $(<pr_number.txt)"
92+
echo "Detected commit_sha is: $(<commit_sha.txt)"
93+
94+
# Make the params available as step output
95+
echo "override_pr=$(<pr_number.txt)" >> "$GITHUB_OUTPUT"
96+
echo "override_commit=$(<commit_sha.txt)" >> "$GITHUB_OUTPUT"
97+
98+
- name: Checkout repository
99+
uses: actions/checkout@v4
100+
with:
101+
ref: ${{ steps.parse_previous_artifacts.outputs.override_commit || '' }}
102+
path: repo_root
103+
104+
- name: Upload coverage to Codecov
105+
uses: codecov/codecov-action@v5
106+
with:
107+
verbose: true
108+
token: ${{ secrets.CODECOV_TOKEN }}
109+
files: ${{ github.workspace }}/codecov.json
110+
fail_ci_if_error: true
111+
# Manual overrides for these parameters are needed because automatic detection
112+
# in codecov-action does not work for non-`pull_request` workflows.
113+
# In `main` branch push, these default to empty strings since we want to run
114+
# the analysis on HEAD.
115+
override_commit: ${{ steps.parse_previous_artifacts.outputs.override_commit || '' }}
116+
override_pr: ${{ steps.parse_previous_artifacts.outputs.override_pr || '' }}
117+
working-directory: ${{ github.workspace }}/repo_root
118+
# Location where coverage report files are searched for
119+
directory: ${{ github.workspace }}

0 commit comments

Comments
 (0)