Topic 4 of 5 · 5 min read
Branch protection and CI for agent PRs
Goal: Configure repository rules so an agent's pull request cannot merge or reach production secrets without human review.
Do not rely on the agent to behave; set repository rules so that an agent's pull request cannot merge, deploy or read production secrets without a person and passing checks. Rules are enforced by the platform even when the agent, or a prompt hidden in the code it read, tries something else.
Check yourself · 1 of 3
What stops an agent from pushing straight to the main branch?
Why it matters
A cloud agent writes code and pushes a branch on its own. If the main branch accepts that push, or CI runs with production secrets, a mistake or a prompt injection (instructions hidden in an issue, a web page or a file the agent reads) can become a release. The cure is to limit what the agent's identity can do.
The layers
- Protect the default branch. Use branch protection or a ruleset to require a pull request, at least one approving review, and passing status checks before merge. The agent can open a pull request but not push to main.
- Work on separate branches. Agents push to their own branch names, such as
copilot/*oragent/*. Rules can target those names. - A person approves. The person who asked for the work should not be the only gate for sensitive paths. Use code owners for areas such as
auth/,billing/and.github/workflows/. - CI gates. Tests, type checks, linting and a security scan run on every pull request. Required checks mean red cannot merge.
- Limit secrets. Workflows triggered by agent branches get no production secrets and a read-only token by default. Deployment secrets live in a protected environment that requires approval.
- Restrict the sandbox network and permissions. Give the agent's token the least access that works: one repository, no admin rights, no access to deploy settings.
Before and after
Before: the agent's token can push to main, and the deploy workflow runs on any push with the production key loaded.
After, as a short ruleset sketch:
# Conceptual ruleset for the default branch
target: main
rules:
pull_request:
required_approving_review_count: 1
require_code_owner_review: true
required_status_checks: [test, typecheck, lint]
restrict_pushes: true # only the merge button, no direct pushes
CODEOWNERS
/.github/workflows/ @acme/platform-team
/src/billing/ @acme/payments-team
What goes wrong
- Agent can edit the workflow. If it changes the CI file, it can change what the checks do. Protect
.github/workflowswith code owners. - Secrets in pull-request workflows. Anything an agent branch can run, a prompt injection can use. Keep production secrets out of that path.
- Admin bypass. If administrators and bots are exempt from the rules, the rules do not apply to the case you worry about.
- One reviewer who rubber-stamps. Rules require a review. They cannot make it a careful one.
Examples
Protecting the workflow files
An agent fixes a flaky test and also edits .github/workflows/test.yml to skip it. With a code-owner rule on /.github/workflows/, the pull request cannot merge until the platform team reviews that file, and they can see what changed.
Secrets scoped to an environment
The deploy key is stored in a production environment that needs a named approver. A workflow running on an agent branch cannot read it, even if the agent or a hidden instruction asks it to print the environment.
Key points
- Enforce limits in repository settings, not in the agent's prompt.
- Require a pull request, an approving human review and passing checks before anything merges.
- Use code owners for sensitive paths, including the CI workflow files.
- Keep production secrets out of workflows that agent branches can trigger.
- Give the agent's identity the least access that works, and avoid bypass rights for bots.
Common mistakes
- Trusting the agent's instructions file as a security control; it is guidance, not enforcement.
- Letting bots or admins bypass branch rules.
- Running pull-request workflows with production secrets available.
- Leaving the CI workflow files outside code-owner review.
Check yourself · 2 of 3
Why should production secrets stay out of workflows that run on agent branches?
Check yourself · 3 of 3
A code-owner rule on .github/workflows/ is added. What risk does it reduce?
Practice items, not counted towards your mastery.
Start here
- About GitHub Copilot cloud agent (opens in a new tab)DocsFree
How an agent is assigned an issue, works in a sandbox, opens a draft pull request and waits for human review.
- Codex cloud (OpenAI) (opens in a new tab)DocsFree
How Codex runs tasks in a cloud environment with a setup script, and what network access it has.
- Claude Code on the web (opens in a new tab)DocsFree
How Claude Code runs sessions in a remote environment, how setup scripts and network rules work, and how changes come back as a branch.
More for this topic
- About rulesets (GitHub Docs) (opens in a new tab)DocsFree
How to require reviews, status checks and restricted pushes on matching branches.
- About protected branches (GitHub Docs) (opens in a new tab)DocsFree
The branch protection rules behind required approvals and passing checks before merge.
- Secure use reference (GitHub Actions) (opens in a new tab)DocsFree
How to limit secrets, token permissions and third-party actions in CI that runs untrusted code.