Environments and Deployment Protection
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
A workflow that only builds and tests is harmless; a workflow that deploys can change production. GitHub models the
target of a deployment as an environment: a named place (staging, production, github-pages) that carries its own
secrets, variables and protection rules. A job that declares environment: production is held until every rule of that
environment passes, and only then does it receive the environment’s secrets. This page covers how to define
environments, what the protection rules do, and how to build approval gates, promotion pipelines, rollbacks and Pages
deployments. Versions of actions are those verified on 2026-11-10; the official documentation wins on any discrepancy.
The running example is this documentation site’s own publish.yml, which today pushes the built Antora site to the
gh-pages branch with crazy-max/ghaction-github-pages and a custom domain. A later section shows how it would look
as a deployment to the github-pages environment. For secrets and OIDC in general see
Secrets, Variables and OIDC.
Environments
An environment belongs to a repository (or is created implicitly the first time a workflow names it). It bundles:
-
Secrets and variables that exist only for jobs targeting that environment.
-
Protection rules evaluated before the job starts.
-
A deployment history: every run of a job that targets the environment creates a deployment record.
Creating an environment
-
UI: repository Settings > Environments > New environment.
-
Implicitly: referencing an unknown
environment:name in a workflow creates it with no protection rules. Do not rely on this for anything sensitive; create the environment first and configure it. -
REST API or
gh api:
# create or update the "production" environment: 30-minute wait, one required reviewer team, protected branches only
gh api --method PUT repos/OWNER/REPO/environments/production \
--input - <<'JSON'
{
"wait_timer": 30,
"prevent_self_review": true,
"reviewers": [{ "type": "Team", "id": 1234567 }],
"deployment_branch_policy": { "protected_branches": false, "custom_branch_policies": true }
}
JSON
# allow only the main branch and v* tags to deploy to production
gh api --method POST repos/OWNER/REPO/environments/production/deployment-branch-policies -f name=main -f type=branch
gh api --method POST repos/OWNER/REPO/environments/production/deployment-branch-policies -f name='v*' -f type=tag
# list environments and read one back
gh api repos/OWNER/REPO/environments --jq '.environments[].name'
gh api repos/OWNER/REPO/environments/production
Environment secrets and variables
gh secret set DEPLOY_KEY --env production < deploy_key.pem
gh variable set API_URL --env production --body "https://api.example.com"
gh variable set API_URL --env staging --body "https://api.staging.example.com"
gh secret list --env production
Inside the job, ${{ secrets.DEPLOY_KEY }} and ${{ vars.API_URL }} resolve against the environment first, then the
repository, then the organization. The same name can therefore mean different things per environment, which is the point:
the workflow stays identical and the target changes.
Protection rules
| Rule | Behaviour |
|---|---|
Required reviewers |
Up to six users or teams; one approval from any of them releases the job. Prevent self-review stops the person who triggered the run from approving it. |
Wait timer |
Delays the job for a fixed number of minutes after it is otherwise ready (useful as a soak or rollback window). |
Deployment branches and tags |
Restricts which refs may deploy: all, protected branches only, or selected branch and tag name patterns (for example
|
Custom deployment protection rules |
A GitHub App can approve or reject a deployment programmatically (an observability gate, a change-management ticket
check, a freeze window). The App receives a |
Environment secrets and variables |
Not a gate, but only released to jobs that passed every rule. |
Availability depends on the plan: environments, secrets and variables are available in all public repositories and in private repositories on paid plans; required reviewers, wait timers and deployment branch policies on a private repository need GitHub Pro, Team or Enterprise (Enterprise Cloud for some features), and custom protection rules likewise. Check the current plan table in the official documentation before designing around a rule.
Using an environment in a workflow
jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: production
url: ${{ steps.deploy.outputs.page_url }}
steps:
- id: deploy
run: ./deploy.sh "$API_URL"
env:
API_URL: ${{ vars.API_URL }}
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
The short form environment: production sets only the name. With the long form, url is shown as a button on the run
summary and in the repository Deployments view; it may be an expression using step or job outputs, evaluated after the
job’s steps have run.
Deployment status and the Deployments view
A job targeting an environment creates a deployment object and reports a status on it: queued or waiting while
rules are pending, in_progress, then success or failure (or inactive when a newer deployment supersedes it).
The repository home page shows an Environments panel with the latest deployment per environment, and
Deployments lists the history with commit, actor and URL. The same data is available from the API:
gh api repos/OWNER/REPO/deployments --jq '.[] | {id, environment, ref, sha}'
gh api repos/OWNER/REPO/deployments/DEPLOYMENT_ID/statuses --jq '.[].state'
Approval flow
While waiting for approval the job holds no runner, so it does not consume minutes. A pending approval expires if it is
not reviewed within 30 days, and the run is then cancelled. A rejection fails the job and, through needs, skips
everything downstream.
Concurrency for deploys
Two deployments to the same target at once can interleave and leave it in a mixed state. Give every deploy job a concurrency group per environment and do not cancel the one in progress:
concurrency:
group: deploy-${{ github.workflow }}-production
cancel-in-progress: false
With cancel-in-progress: false, the running deployment finishes and at most one newer run waits; intermediate pending
runs are replaced by the latest. Use cancel-in-progress: true only for build and test jobs, where discarding a stale
run is safe. The group may also be set on the job rather than the workflow when only the deploy job needs serializing.
Promotion pipeline: dev, staging, production
name: release-pipeline
on:
push:
branches: [main]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.meta.outputs.version }}
steps:
- uses: actions/checkout@v7
- id: meta
run: echo "version=$(git describe --tags --always)" >> "$GITHUB_OUTPUT"
- run: ./build.sh
- uses: actions/upload-artifact@v7
with: { name: app, path: dist/ }
deploy-dev:
needs: build
runs-on: ubuntu-latest
environment: { name: dev, url: 'https://dev.example.com' }
concurrency: { group: deploy-dev, cancel-in-progress: false }
steps:
- uses: actions/download-artifact@v8
with: { name: app, path: dist/ }
- run: ./deploy.sh dev "${{ needs.build.outputs.version }}"
deploy-staging:
needs: deploy-dev
runs-on: ubuntu-latest
environment: { name: staging, url: 'https://staging.example.com' }
concurrency: { group: deploy-staging, cancel-in-progress: false }
steps:
- uses: actions/download-artifact@v8
with: { name: app, path: dist/ }
- run: ./deploy.sh staging "${{ needs.build.outputs.version }}"
- run: ./smoke-test.sh https://staging.example.com
deploy-production:
needs: deploy-staging # runs only if staging and its smoke test succeeded
runs-on: ubuntu-latest
environment: { name: production, url: 'https://example.com' } # reviewers + wait timer gate this job
concurrency: { group: deploy-production, cancel-in-progress: false }
steps:
- uses: actions/download-artifact@v8
with: { name: app, path: dist/ }
- run: ./deploy.sh production "${{ needs.build.outputs.version }}"
The same artifact is promoted through every stage: it is built once and never rebuilt, so what reached production is
exactly what was tested in staging. Only production has required reviewers; dev and staging deploy
unattended. See Jobs, Steps and Matrix for needs
and outputs, and Caching and Artifacts for artifact limits.
Rollbacks
GitHub has no "undo" button; a rollback is a new deployment of an older, known-good version. Typical options:
-
Re-run the old deployment: open the successful run that deployed the good commit and use Re-run all jobs. The jobs check out the original SHA and go through the environment rules again (reviewers included), so a rollback is as controlled as a release. Artifacts of that run may have expired, in which case rebuild from the SHA.
-
Redeploy a tag: keep a
workflow_dispatchinput that selects the ref, and run it against a release tag. -
Revert and roll forward:
git revertthe bad commit onmainand let the normal pipeline deploy it.
on:
workflow_dispatch:
inputs:
ref:
description: 'Tag or SHA to deploy (rollback target)'
required: true
jobs:
rollback:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v7
with: { ref: "${{ inputs.ref }}" }
- run: ./deploy.sh production "${{ inputs.ref }}"
The deployment branch policy must allow the tag (for example v*), otherwise the job is rejected.
OIDC and environments
When a job requests an OIDC token (permissions: id-token: write), the sub claim encodes where the job runs. If the
job declares an environment, the subject is repo:OWNER/REPO:environment:production, regardless of branch. A cloud role
that trusts exactly that subject can only be assumed by jobs that passed the environment’s protection rules:
permissions:
id-token: write
contents: read
jobs:
deploy:
environment: production
runs-on: ubuntu-latest
steps:
- uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: arn:aws:iam::123456789012:role/gha-deploy-production
aws-region: eu-west-1
Pair the trust policy with the subject above rather than with a wildcard on the repository; the details are on Secrets, Variables and OIDC.
GitHub Pages as an environment
A Pages site whose source is GitHub Actions (Settings > Pages) is deployed through a built-in environment named
github-pages, with branch protection that limits deployment to the default branch. The deployment uses three
official actions: actions/configure-pages (reads or enables the Pages configuration and exposes the base URL),
actions/upload-pages-artifact (packs the site directory) and actions/deploy-pages (publishes it and reports the page
URL). The job needs pages: write and id-token: write; deploy-pages uses the OIDC token to prove the deployment
comes from this repository.
The docs site as a github-pages deployment
name: publish
on:
schedule:
- cron: '0 3 * * *'
workflow_dispatch:
permissions:
contents: read
concurrency:
group: pages
cancel-in-progress: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with: { node-version: 24, cache: npm }
- uses: actions/configure-pages@v6
- run: |
npm ci
node scripts/validate-mermaid.mjs
npx antora antora-playbook.yml
- uses: actions/upload-pages-artifact@v5
with: { path: build/site }
deploy:
needs: build
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- id: deployment
uses: actions/deploy-pages@v5
The real workflow today does not do this: it builds, then pushes build/site to the gh-pages branch with
crazy-max/ghaction-github-pages and sets fqdn: www.irurueta.com so the action writes a CNAME file. Moving to the
Pages environment would replace the branch push with the artifact-based deployment above; the custom domain is then
configured in the repository’s Pages settings instead of through a CNAME file. Because the Pages source is a
repository setting, the switch is made there first and the workflow second. The build job keeps read-only permissions
while only the deploy job can write Pages, which is the least-privilege shape described on
Security Hardening.
Common mistakes
-
Putting production secrets at repository level instead of in a protected environment, so any workflow on any branch can read them.
-
Relying on an implicitly created environment: it has no reviewers, no branch policy and no wait timer.
-
Using
cancel-in-progress: trueon a deploy job, which can abort a half-finished deployment. -
Rebuilding the artifact in each stage instead of promoting the one that was tested.
-
Forgetting that required reviewers on a private repository need a paid plan; the rule silently does not exist otherwise.
-
Allowing every branch to deploy: set a deployment branch and tag policy on
production. -
Granting
id-token: writeat workflow level when only the deploy job needs it.