Sub topic 12
Keeping required checks green with a gate job
This chapter assumes you can write a GitHub Actions workflow, know how needs and job if conditions work, and have a change-detection job like the one built earlier. You also need to know what branch protection is, meaning the repository rule that blocks merging until named status checks pass.
The problem: skipped checks and required checks
Selective CI creates a conflict with branch protection. Branch protection requires checks by job name. If you require backend-test and a pull request touches only the frontend, backend-test is skipped by its if condition. A job skipped by a job-level if reports as "Skipped", and GitHub treats that as passing for required checks.
Two cases still cause trouble:
- A workflow-level
pathsfilter that doesn't match means the workflow never starts. No check is reported at all, so a required check stays "Expected, waiting for status" and the pull request cannot merge. - A job that
needsa failed or skipped job is itself skipped, unless you override that withif: always(). A skipped job in the chain can hide a real failure.
Requiring every individual job is also fragile. Each time you add, rename or remove a project, you must edit the branch protection settings.
The gate job pattern
Add one job that always runs, looks at the results of the jobs it needs, and fails only if one of them failed or was cancelled. Then require only that job in branch protection.
ci-ok:
if: always()
needs: [backend-test, frontend-test]
runs-on: ubuntu-latest
steps:
- name: Fail if any needed job failed or was cancelled
run: |
if [ "${{ contains(needs.*.result, 'failure') }}" = "true" ] || [ "${{ contains(needs.*.result, 'cancelled') }}" = "true" ]; then
exit 1
fiEach part matters:
if: always()makes the job run even when a needed job failed or was skipped. Without it, the gate would be skipped in exactly the cases where it must report.needs.*.resultcollects the result of every needed job. Each result is one ofsuccess,failure,cancelledorskipped.- The script fails on
failureorcancelledand ignoresskipped, so jobs that were intentionally not needed don't block the merge.
Configure branch protection
In the repository's branch protection rule or ruleset, require only ci-ok. Adding a new project then means adding its job to the gate's needs list. The protection settings stay unchanged.
Do not combine this pattern with a workflow-level paths filter on the same workflow. If the filter prevents the run, ci-ok is never reported and the pull request waits forever. Put all selection logic inside the workflow, in the change-detection job, so the workflow always starts and the gate always reports.
Common mistakes
| Mistake | Effect |
|---|---|
| Omitting `if: always()` on the gate | The gate is skipped when a needed job fails, so no failure is reported. |
| Checking only for `success` | Intentionally skipped jobs fail the gate. |
| Leaving `changes` out of `needs` | A broken change-detection job can let the pull request merge. |
| Using a workflow-level `paths` filter | The gate never reports and the required check hangs. |
Exercise
A pull request changes only files under apps/web. The backend-test job is skipped by its if condition, frontend-test succeeds, and ci-ok runs with if: always() and fails only on failure or cancelled. What does ci-ok report, and why?
Show answer
It passes. The skipped backend-test result is "skipped", which the gate does not treat as a failure, and frontend-test succeeded. Branch protection sees a passing ci-ok and allows the merge.
Exercise
Your workflow has a top-level paths filter limited to services/api/**, and ci-ok is the only required check. A pull request changes only README.md. What happens, and how do you fix it?
Show answer
The workflow never runs, so ci-ok is never reported and the pull request stays blocked as "Expected". To fix it, remove the workflow-level paths filter and let a change-detection job decide which jobs run, so the gate always executes.