|
| 1 | +# Choosing a workflow strategy |
| 2 | + |
| 3 | +The action supports different trigger and job layouts. Choose the one that best fits your CI workflow. |
| 4 | + |
| 5 | +## Pull requests and default-branch pushes |
| 6 | + |
| 7 | +For most repositories, run coverage for pull requests and pushes to the default branch: |
| 8 | + |
| 9 | +```yaml |
| 10 | +on: |
| 11 | + pull_request: |
| 12 | + push: |
| 13 | + branches: [main] |
| 14 | +``` |
| 15 | +
|
| 16 | +This avoids running coverage for branches that do not have a pull request. Pull request events also provide the PR number directly, so the upload job does not need `pull-requests: read`. |
| 17 | + |
| 18 | +## Push-only workflows |
| 19 | + |
| 20 | +Some workflows use `push` events to run tests on every branch, without a `pull_request` trigger. This trigger strategy works with either the separate-job or same-job layout described below. |
| 21 | + |
| 22 | +In a separate-job layout, the test job generates the report and the upload job receives it through an artifact on every push. The action then determines whether the report can be uploaded: |
| 23 | + |
| 24 | +- Default-branch pushes upload normally. |
| 25 | +- Pushes with an open pull request upload using the discovered PR number. |
| 26 | +- Pushes without an open pull request skip successfully. |
| 27 | + |
| 28 | +Push-only workflows have an existing requirement to grant the upload job `pull-requests: read`: |
| 29 | + |
| 30 | +```yaml |
| 31 | +permissions: |
| 32 | + contents: read |
| 33 | + code-quality: write |
| 34 | + pull-requests: read |
| 35 | +``` |
| 36 | + |
| 37 | +See the [README permissions section](../README.md#permissions) for the complete requirements, including GitHub CLI availability. |
| 38 | + |
| 39 | +## Separate upload job |
| 40 | + |
| 41 | +A separate upload job keeps `code-quality: write` away from the job that runs repository code and third-party dependencies. Because jobs have isolated filesystems, pass the coverage report between them with `actions/upload-artifact` and `actions/download-artifact`. |
| 42 | + |
| 43 | +It also allows the test job to run independently when the upload job is skipped. This is the recommended layout. |
| 44 | + |
| 45 | +## Same-job upload |
| 46 | + |
| 47 | +For a simpler workflow, generate and upload the report in the same job: |
| 48 | + |
| 49 | +```yaml |
| 50 | +permissions: |
| 51 | + contents: read |
| 52 | + code-quality: write |
| 53 | +
|
| 54 | +steps: |
| 55 | + # ... generate cobertura.xml ... |
| 56 | +
|
| 57 | + - uses: actions/upload-code-coverage@v1 |
| 58 | + with: |
| 59 | + file: cobertura.xml |
| 60 | + language: Java |
| 61 | + label: code-coverage/jacoco |
| 62 | +``` |
| 63 | + |
| 64 | +No artifact transfer is needed, but the job that runs tests also receives `code-quality: write`. If the job runs on every push, tests still run before the action decides whether to upload or skip. Applying an event condition to the entire job instead would skip both testing and uploading. |
| 65 | + |
| 66 | +## Avoiding duplicate runs |
| 67 | + |
| 68 | +A workflow with broad `push` and `pull_request` triggers may run twice for the same pull request commit. To avoid that, either: |
| 69 | + |
| 70 | +- Use `pull_request` plus default-branch `push`; or |
| 71 | +- Use a push-only workflow with `pull-requests: read`. |
| 72 | + |
| 73 | +## Multiple reports |
| 74 | + |
| 75 | +Call the action once for each report and use a distinct `label`: |
| 76 | + |
| 77 | +```yaml |
| 78 | +- uses: actions/upload-code-coverage@v1 |
| 79 | + with: |
| 80 | + file: backend.xml |
| 81 | + language: Python |
| 82 | + label: backend |
| 83 | +
|
| 84 | +- uses: actions/upload-code-coverage@v1 |
| 85 | + with: |
| 86 | + file: frontend.xml |
| 87 | + language: JavaScript |
| 88 | + label: frontend |
| 89 | +``` |
0 commit comments