Sub topic 11
Limiting Deployments to Branches and Tags
This chapter assumes you can write workflow YAML, know how a job targets an environment with environment:, and can open repository settings. Required reviewers gate who approves a deployment. Branch and tag restrictions gate which code can deploy at all.
Why restrict refs
Any workflow that names environment: production can try to deploy there. Without a restriction, someone can push a feature branch with a modified workflow that targets production. Reviewers might catch it. A branch rule blocks it earlier, before the job starts and before it can read the environment's secrets.
The three modes
In the environment's settings, under Deployment branches and tags, you choose one of three options.
| Option | Who can deploy | API values |
|---|---|---|
| All branches | Any branch or tag | `protected_branches: false`, `custom_branch_policies: false` |
| Protected branches only | Branches with a branch protection rule | `protected_branches: true`, `custom_branch_policies: false` |
| Selected branches and tags | Refs matching your name patterns | `protected_branches: false`, `custom_branch_policies: true` |
"Protected branches only" depends on branch protection rules or rulesets existing. If none match, nothing can deploy.
Selected branches and tags
With the selected option, you add one or more name patterns. Each pattern is either a branch or a tag pattern.
mainmatches only the branchmain.release/*matches branches such asrelease/1.4.v*added as a tag pattern matches tags such asv2.0.1.
The * wildcard does not match a slash. release/* matches release/1.4 but not release/1.4/hotfix. Use release/** if you need nested names.
You can add a pattern in the UI or with the API. The earlier API example enabled custom_branch_policies and then created a policy for main. The policy call takes a type of branch or tag:
gh api --method POST repos/OWNER/REPO/environments/production/deployment-branch-policies \
-f name='v*' -f type=tagWhat happens on a mismatch
The check runs when the job is about to start. If the workflow ref does not match, the job fails. It does not wait for approval and it does not run any steps. The failure message says the ref is not allowed to deploy to the environment.
Common errors
- Pattern typed as a branch when it is a tag. A
v*rule of typebranchnever matches a tag push. - Pull request workflows. For
pull_requestevents the ref isrefs/pull/N/merge, which matches no branch pattern. Deploy frompushtomainor fromworkflow_dispatchon an allowed branch instead. - Protected branches selected, none configured. Every deployment fails until a protection rule exists.
Exercise
You set production to "Selected branches and tags" with only the branch pattern main. A teammate pushes tag v1.2.0 and the release workflow fails at the production job without waiting for approval. What is the cause, and what is the fix?
Show answer
The tag does not match the main branch pattern, so the ref check fails before reviewers are asked. Add a deployment policy with name v* and type tag (or deploy from main instead).