Sub topic 10
Enforcing policy with CI checks and branch protection
A written policy only changes behavior if something makes it hard to ignore. This chapter covers the automated layer: CI checks, branch protection, and CODEOWNERS. Together they apply your team's rules to every pull request, whether a person or an AI tool wrote the code.
Prerequisites assumed: you can open and merge pull requests, you know what a CI pipeline is, and you have admin or maintainer rights on a repository (or can ask someone who does). Examples use GitHub. Other platforms have equivalents under different names.
Why enforcement belongs in tooling
Policy text asks people to remember rules. Automation checks the rules every time. This matters more with AI assistance, because AI tools can produce large amounts of plausible code quickly. Human reviewers cannot be the only line of defense.
A useful principle: enforce the outcome, not the origin. CI cannot reliably tell whether a human or a model wrote a line. It can verify that tests pass, that no secrets are present, that sensitive paths got the right review, and that the author completed the checklist. These checks apply to everyone, so the policy stays fair and workable.
The three layers
Three mechanisms work together. Each one covers a gap in the others.
| Layer | What it does | Example rule |
|---|---|---|
| CI workflow | Runs automated checks on each pull request | Tests and scans must pass |
| Branch protection | Decides what must be true before merging | CI must pass and one approval is required |
| CODEOWNERS | Routes changes in chosen paths to named reviewers | Changes to `/auth/` need the security team |
A CI workflow alone is advisory. A failing check is only a red mark until branch protection makes it block the merge. Branch protection alone has nothing meaningful to require unless CI produces checks. CODEOWNERS alone does nothing unless branch protection requires code owner review.
Step 1: A CI workflow that runs the same tests for everyone
Start with the basics. Every pull request runs the full test suite. The following workflow does that on pull requests and on pushes to main.
# .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 testThe job is named test. That name matters later, because branch protection refers to checks by name.
Adding checks that match your policy
Tests are the minimum. Your policy probably names other risks from earlier chapters. Map each risk to a check:
| Policy concern | Typical automated check |
|---|---|
| Incorrect code | Unit and integration tests, type checking, linting |
| Insecure code | Static analysis (SAST) and dependency vulnerability scanning |
| Leaked secrets | Secret scanning on commits and pull requests |
| Licensing | License scanning of new dependencies |
| Missing tests | Coverage thresholds on changed code |
Add these as separate jobs or separate workflows. Separate jobs give clearer failure messages, and each can be required independently.
Keeping the checks trustworthy
Enforcement fails quietly when checks are weak. Watch for these problems:
- Flaky tests. If tests fail randomly, people re-run until green and stop trusting the signal. Fix or quarantine flaky tests quickly.
- Tests that the change itself can edit. An AI tool, or a hurried author, can make a failing test pass by deleting or weakening it. Route changes to test configuration and CI files through CODEOWNERS.
- Coverage as a target. A coverage threshold can be met with tests that assert nothing. Treat it as a floor, and rely on review to judge test quality.
- Workflow changes in the same pull request. A pull request can modify
.github/workflows/to skip the very check that would block it. Protect that directory with a code owner rule.
Step 2: Branch protection
Branch protection (called rulesets or protected branches, depending on platform and settings) turns checks into gates. For main and any release branches, configure at least the following:
- Require a pull request before merging. No direct pushes.
- Require approvals. At least one, and more for sensitive code.
- Require status checks to pass. Select the
testjob and any other jobs you want to block on. - Require branches to be up to date before merging, so checks run against the code that will actually land.
- Require review from code owners. This activates the CODEOWNERS file.
- Dismiss stale approvals when new commits are pushed. Otherwise an author can get approval and then add unreviewed changes.
- Apply the rules to administrators. Exemptions for admins create a path around the policy.
- Block force pushes and branch deletion.
Who approves matters
"One approval" is weak if the approver is the person who prompted the AI tool and also the only teammate nearby. Consider requiring that the approver is not the author, and that the last pusher cannot approve their own latest changes. If an agentic tool commits on behalf of a developer, that developer is the author and cannot be the sole approver.
Step 3: CODEOWNERS for sensitive paths
CODEOWNERS maps file paths to the people or teams who must review changes there. It is the most direct way to apply the "restricted use" tier of your policy. Areas like authentication and payments get expert review no matter how the code was produced.
# .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-leadsPoints to check:
- Order matters. When several patterns match a file, the last matching line wins. Put broad rules first and specific ones after.
- Owners need write access. An owner without repository permission cannot satisfy the requirement, and the rule may block merges unexpectedly.
- Use teams, not individuals. People leave and go on vacation. A team keeps review available.
- Protect the file itself. Include
/.github/(as above) so nobody can quietly remove an owner entry. - Keep it small. If every path has an owner, reviews bottleneck and approvals become rubber stamps.
Step 4: Enforcing the human parts
Some policy rules are about people, such as "the author understands every line" and "disclose AI assistance." A pull request template prompts for these.
<!-- .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 toolA template is a prompt, not a gate. It records a statement of accountability that a reviewer can check against the diff. If you need a hard gate, add a small CI job that reads the pull request description and fails when the AI assistance section has no box checked. Use this sparingly. A check that people satisfy by ticking boxes without thinking adds friction without adding safety. Most teams keep the template as a prompt and rely on reviewers to push back.
Rolling enforcement out safely
Turning on every rule at once creates a pile of blocked work and resentment. A staged approach works better:
- Observe. Add the new check without making it required. Collect results for a week or two.
- Fix the noise. Remove false positives and repair flaky tests.
- Require it. Add the check to branch protection.
- Review exceptions. Track bypasses and ask whether the rule or the exception is wrong.
Decide in advance who may bypass protections in an emergency, and make bypasses visible, for example through an audit log or a required follow-up issue. Chapter on incidents and exceptions covers this process in more detail.
Limits of automation
Automation cannot catch everything. Keep these gaps in mind:
- Tests pass for code that is wrong in ways nobody tested.
- Secret scanners miss secrets in unusual formats, and they cannot undo data already sent to an external AI tool.
- CI cannot see what a developer pasted into a chat window. That risk depends on the data rules and approved-tool settings covered earlier.
- Checks can be bypassed by someone with enough permissions, which is why audit logs and the "apply to administrators" setting matter.
Automated guardrails reduce risk and make the baseline consistent. Human review and author accountability still carry the judgment calls.
Common errors
- Checks exist but are not required. The most frequent failure. Verify in branch protection settings, not in the workflow file.
- CODEOWNERS present but code owner review not required. The file only suggests reviewers until the setting is on.
- Typos in paths. A pattern that matches nothing protects nothing. Test with a pull request that touches each protected path.
- Required check name drift. Renaming a job breaks the link to the required status.
- Protections only on
main. Release and maintenance branches need the same rules.
Exercise
Your team has a CODEOWNERS file that lists /payments/ @example-org/payments-team, and a CI workflow with a test job. A pull request touching /payments/ was merged with no review from the payments team, and the test job had failed. Name two branch protection settings that were probably missing, and say what each would have done.
Show answer
First, "Require status checks to pass" with the test job selected would have blocked the merge while the tests were failing. Second, "Require review from code owners" would have forced an approval from the payments team, because CODEOWNERS only routes review requests until that setting is enabled. A third likely gap is that the rules did not apply to administrators, if an admin merged it.
Exercise
A developer asks an AI tool to fix a failing test. The tool edits .github/workflows/ci.yml to remove the npm test step, and the pull request goes green. Which two controls from this chapter should stop this from merging, and why?
Show answer
The CODEOWNERS entry for /.github/ requires the platform team to approve any change to workflow files, so a reviewer who understands CI will see the removed test step. Branch protection with required code owner review makes that approval mandatory. Dismissing stale approvals also keeps the author from adding the workflow change after an earlier approval. The root cause is that the check could be changed by the same pull request it guards, so protecting the workflow directory is the key control.