Skip to content
Source
Sign inSign up

Add branch rules

CI / Rules

Branch rules

Nothing stops the merge of a pull request with red checks, and a person with write access can push straight to main and skip review. A branch rule stops both: the branch takes changes only through pull requests, and a pull request merges only when its required checks passed on its head commit.

6 min read

Only an admin can merge past the rules.

This page sets the checks. A branch rule can also require reviews: approvals, code owner approvals and a linear history. To protect release tags like v*, add a tag rule.

You need admin access to the repository. Admins set branch rules.

1. Add a rule

  1. Open the repository, then Settings, then Branches under Code (/@<owner>/<repo>/settings/branches).
  2. Under Add a rule, enter a Branch name pattern, like main or release/**.
  3. Make sure that Require a pull request is selected.
  4. Select the Required checks (see the next step).
  5. Click Add rule.

With the src CLI, which uses your src login, or the TSS_* variables when you set them:

src ci rule set main --require-pull --check pr/test --check pr/lint --up-to-date

Over REST:

curl -X PUT "$TSS_FORGE_URL/repos/acme/app/ci/rules/main" \
  -H "Authorization: Bearer $TSS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"requirePull":true,"requiredChecks":["pr/test","pr/lint"],"requireUpToDate":true}'
  • src ci rule set and PUT replace the whole rule. An option that you leave out is off.
  • A pattern is a branch name or a glob, without refs/heads/. It cannot start with !.
  • If more than one rule matches a branch, the strictest setting of each wins. A skipped check counts as passed only when every matching rule says so.
  • A repository has at most 100 rules. A rule has at most 50 required checks.
  • The review rules (--approvals, --code-owners, --dismiss-stale, --linear-history) are on Require reviews before a merge.

2. Pick the required checks

A required check is a job of a workflow, with the name <workflow>/<job>, like pr/test.

  • Pick jobs from the plan of the base branch, in workflows that run on pull requests. A check counts only the runs of the pull request.
  • A job with a matrix passes only when every cell passes.
  • If the base branch has no such job, the check stays pending. If you rename or remove a job, change the rule too.

To make a merge wait for an app, or for a hidden workflow, see Require an app status and Hidden checks as required checks.

3. Read the merge box

Open a pull request into the branch. The merge box in the right column says Ready to merge or Not ready to merge. Under Not ready to merge, it lists each required check that failed or has not finished, with the reason. While something blocks the merge, the merge button is off. The CLI shows the same text:

src ci checks 7
Pull request #7 into main, head 1a2b3c4
NAME     STATE    REASON
pr/lint  passed   Passed on 1a2b3c4
pr/test  pending  Job test (shard: 2) is running
Up to date: no
May merge: no. This pull request can't merge into main yet. Required check pr/test is pending. Job test (shard: 2) is running. The branch is not up to date with main. Merge main into it, or rebase it.

Done

The branch takes changes only through pull requests. When every required check passed and the branch is up to date, the merge button is available.

How a check counts

The forge reads the latest run of the workflow on the head commit of the pull request. Each required check is in one of three states:

  • Passed: the run is over, and every cell of the job ended with success.
  • Failed: a cell failed, timed out, was cancelled, or ended with an error.
  • Pending: all other cases.

Three cases need care:

  • A check that never started is pending, never passed. This includes a commit with no run yet, and a run that waits for an approval.
  • A skipped check fails. If the rule has Count skipped checks as passed (--skipped-passes, skippedCountsAsPassed), it passes. This setting is off by default. For example, a job is skipped when a secret that it needs has no value at the trust level of the run.
  • A failed plan keeps the check pending. If the plan of the head commit fails, the run of the workflow has no jobs, so its checks never pass. Fix the errors that the run shows, and push again. See When the plan fails.

Require an up-to-date branch

Select Require the branch to be up to date (--up-to-date, requireUpToDate). Then a pull request merges only when its head contains the latest commit of the base branch. If the head is behind, merge the base branch into it, or rebase it.

Require an app status

An app sets its own status on the head commit of a pull request. To make a merge wait for it, add app:<slug> to the required checks:

src ci rule set main --require-pull --check pr/test --check app:pkg-preview
  • Passed: the app set success on the head commit.
  • Failed: the app set failure on the head commit.
  • Pending: the app set pending, or set no status on the head commit yet. After a push, the old status does not count.

A required app status stays pending on a monorepo pull request, which has no single head commit. See Cards and statuses.

Hidden checks as required checks

A hidden workflow is a workflow file that only admins see. Admins keep it under Hidden workflows, in the CI section of the repository settings. To require one of its jobs, add the check as hidden:<workflow>/<job>.

  • Only the runs of the hidden workflow count for a hidden: check.
  • A <workflow>/<job> check counts only the workflows of the repository. So a hidden workflow and a repository workflow with the same name never decide the checks of the other.
  • Contributors see the result, the summary, and the repro command that an admin wrote. The logs, findings and artifacts are for admins only.

Protected CI pushes

Require a pull request refuses every direct push to the branch, with one exception. If the push permission of a job in a protected run names the branch, that job can push. For example: permissions: { push: ['main'] }.

  • A trusted or untrusted run never pushes to a branch with a rule. See the permissions table.
  • A branch whose rule requires a pull request gives protected runs: a push (a merge) or a manual() run on it is protected. A run is protected only on such a branch, because its code came through a pull request. A tag, and a branch with no rule that requires a pull request, the default branch too, gives trusted runs. See Trust levels.
  • A branch with any rule counts as a protected ref: only a protected run pushes to it.
  • A merge of a pull request is not a push. The rule does not refuse it.
  • A tag rule protects tags in the same way: only an admin, or a job of a protected run with push: ['tags'], changes a matching tag.

One gate for every merge

Every merge goes through one gate in the forge. The merge button merges through it, and REST, MCP and src read the same result:

  • REST: GET /api/v1/repos/<owner>/<repo>/pulls/<number>/checks
  • MCP: the tool get_pull_request_checks
  • CLI: src ci checks <pr>