Skip to content
mentoor

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?

Choose one answer

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

  1. 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.
  2. Work on separate branches. Agents push to their own branch names, such as copilot/* or agent/*. Rules can target those names.
  3. 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/.
  4. CI gates. Tests, type checks, linting and a security scan run on every pull request. Required checks mean red cannot merge.
  5. 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.
  6. 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/workflows with 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?

Choose one answer

Check yourself · 3 of 3

A code-owner rule on .github/workflows/ is added. What risk does it reduce?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic