Sub topic 10
Enforcing policy with CI checks and branch protection
This chapter assumes you know the pull request workflow, have used a CI pipeline such as GitHub Actions, and have read the earlier chapters on accountability and review. A written policy that nobody checks tends to fade. Automated guardrails turn the rules into checks that every change must pass, whoever or whatever wrote the code.
Why enforce in the repository
Reviewers get tired and rules get forgotten. Machines apply the same rule every time. Your policy should say what the rule is. The repository settings should make it hard to skip.
Enforce the rules on the change, not on the tool. You usually cannot tell how a line was written, so the checks run on every pull request.
Run the same tests on every change
A CI workflow runs your tests on each pull request. It does not ask who wrote the code.
# .github/workflows/ci.yml
name: ci
on:
pull_request:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testA workflow alone only reports results. A failing job does not stop a merge unless you mark it as a required status check in branch protection.
Route sensitive paths to owners
A CODEOWNERS file names the people who must review changes to certain paths. AI-assisted changes in those areas get the same required review as any other change.
# .github/CODEOWNERS
# Changes to these paths always need review from the named owners,
# no matter how the code was written.
/auth/ @example-org/security-team
/payments/ @example-org/payments-team
/.github/ @example-org/platform-team
/docs/policies/ @example-org/engineering-leadsNote that /.github/ is listed. Without it, a change could edit the workflow or the CODEOWNERS file itself and weaken the checks.
Ask authors to confirm
A pull request template cannot block a merge by itself. It prompts the author to confirm they understand the change and to record any AI assistance, which supports the accountability and disclosure rules.
<!-- .github/pull_request_template.md -->
## What changed and why
## How I verified it
- [ ] I read and understand every line in this PR
- [ ] I ran the tests locally
- [ ] New or changed behavior has tests
## AI assistance (per team policy)
- [ ] No AI tool was used
- [ ] AI tool used for part of this change (tool and scope: ____ )
- [ ] No secrets, customer data, or restricted code were shared with the toolTurn the rules on with branch protection
Branch protection settings on main are what make the pieces above binding. A typical setup:
| Setting | Effect |
|---|---|
| Require a pull request before merging | No direct pushes to `main` |
| Require approvals | At least one human reviewer must approve |
| Require review from code owners | Owners named in CODEOWNERS must approve their paths |
| Require status checks to pass | The `test` job must succeed |
| Dismiss stale approvals on new commits | A later push cannot reuse an old approval |
| Include administrators | The rules apply to everyone |
Common errors
- Writing the workflow but not marking the job as required. The check runs, but merges still go through when it fails.
- Letting the author be the only approver. Require someone other than the author.
- Leaving
/.github/out of CODEOWNERS, so the guardrails can be edited without review. - Exempting administrators, which creates an easy bypass.
- Treating the checkbox template as enforcement. It records intent and does not verify it.
Exercise
Your CI workflow runs npm test on every pull request, but a teammate merged a PR with failing tests. Name the likely cause and the fix.
Show answer
The test job was not set as a required status check in branch protection, so a failure did not block merging. In the branch protection settings for main, add the test job to the required status checks. Also enable the option that applies the rules to administrators.
Exercise
Why should /.github/ appear in CODEOWNERS?
Show answer
The directory holds the CI workflows and the CODEOWNERS file. If anyone can change them without review, a pull request could remove the tests or the ownership rules. Assigning an owner such as the platform team means those changes need approval too.