TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Environments & Manual Approvals

Environments & Manual Approvals

An environment in GitHub Actions is a named deployment target (staging, production) with its own secrets, variables and protection rules. A job that declares environment: production doesn't start until those rules are satisfied. That is how continuous delivery gets its manual gate, and how production credentials stay out of every other job. Environments, Approvals & Concurrency adds concurrency groups for deployments; this lesson builds a complete staging → production flow.

Protection rules

Rule Effect
Required reviewers up to six people or teams; one approval releases the job. Optionally prevent self-review (the person who triggered the run can't approve it)
Wait timer the job waits a fixed number of minutes before it may start (a "bake time" after staging)
Deployment branches and tags only main, protected branches, or chosen patterns (main, v* tags) may deploy here
Custom deployment protection rules a GitHub App approves or rejects (a change-management ticket, a monitoring check)

Availability depends on the plan and on whether the repository is public; check GitHub's current plan documentation before relying on reviewers in a private repository.

Staging → production

yaml
# .github/workflows/deploy.yml
name: deploy
on:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-24.04
    outputs:
      digest: ${{ steps.push.outputs.digest }}
    steps:
      - uses: actions/checkout@v7
      - id: push
        run: ./ci/build-and-push.sh          # prints digest=sha256:... to $GITHUB_OUTPUT

  staging:
    needs: build
    runs-on: ubuntu-24.04
    environment:
      name: staging
      url: https://staging.shop.example.com
    concurrency: { group: deploy-staging, cancel-in-progress: false }
    steps:
      - uses: actions/checkout@v7
      - run: ./ci/deploy.sh staging "$DIGEST"
        env:
          DIGEST: ${{ needs.build.outputs.digest }}
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}     # the *staging* environment's secret
      - run: ./ci/smoke-test.sh https://staging.shop.example.com

  production:
    needs: [build, staging]
    runs-on: ubuntu-24.04
    environment:
      name: production                                # waits here for a reviewer
      url: https://shop.example.com
    concurrency: { group: deploy-production, cancel-in-progress: false }
    steps:
      - uses: actions/checkout@v7
      - run: ./ci/deploy.sh production "$DIGEST"
        env:
          DIGEST: ${{ needs.build.outputs.digest }}       # the same image staging tested
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}     # the *production* environment's secret

Points to notice:

  • The same secret name, two values. DEPLOY_TOKEN is defined in both environments; each job gets its environment's value. The scripts stay identical.
  • The digest travels, not the tag. Production deploys exactly the image staging tested.
  • The approval is recorded. The run page shows who approved and when, and the repository's Deployments view lists every deployment per environment, with links to the runs: an audit trail for free.
  • Approval waits are limited (a pending job waits up to 30 days), and while waiting the job uses no runner.

A model: who may deploy where

The protection rules combine: the branch rule decides whether the job may even ask, and the reviewers decide whether it proceeds. The model evaluates a few deployment requests against rules like the ones above (patterns as simple globs):

python · try it yourself
from fnmatch import fnmatchcase

ENVIRONMENTS = {
    "staging":    {"refs": ["main", "release/*"], "reviewers": set(), "prevent_self_review": False},
    "production": {"refs": ["main", "v*"], "reviewers": {"ana", "sre-team"}, "prevent_self_review": True},
}

def decide(env, ref, actor, approver=None):
    rules = ENVIRONMENTS[env]
    if not any(fnmatchcase(ref, p) for p in rules["refs"]):
        return f"rejected: {ref} may not deploy to {env}"
    if not rules["reviewers"]:
        return "runs immediately"
    if approver is None:
        return "waiting for review"
    if approver not in rules["reviewers"]:
        return f"{approver} is not a required reviewer: still waiting"
    if rules["prevent_self_review"] and approver == actor:
        return "self-review blocked: still waiting"
    return f"approved by {approver}: runs"

requests = [
    ("staging", "main", "bob", None),
    ("staging", "feature/cart", "bob", None),
    ("production", "main", "bob", None),
    ("production", "main", "bob", "carl"),
    ("production", "main", "ana", "ana"),
    ("production", "main", "bob", "ana"),
    ("production", "v1.4.0", "bob", "sre-team"),
]
for env, ref, actor, approver in requests:
    print(f"{env:<10} {ref:<13} by {actor:<4} approver={str(approver):<8} -> {decide(env, ref, actor, approver)}")
staging    main          by bob  approver=None     -> runs immediately
staging    feature/cart  by bob  approver=None     -> rejected: feature/cart may not deploy to staging
production main          by bob  approver=None     -> waiting for review
production main          by bob  approver=carl     -> carl is not a required reviewer: still waiting
production main          by ana  approver=ana      -> self-review blocked: still waiting
production main          by bob  approver=ana      -> approved by ana: runs
production v1.4.0        by bob  approver=sre-team -> approved by sre-team: runs

Environments on other platforms

GitLab has the same concept: environment: on a job, protected environments with required approvals, and deployment history (Environments & Manual Deployments). Azure Pipelines has environments with approvals and checks (Toolkit). The design is identical everywhere: deployment credentials belong to the environment, and the environment decides who may use them.

Test yourself

A PR workflow tries to use environment: production, whose deployment branch rule is main. What happens?

  1. It deploys
  2. It is rejected: the ref doesn't match the environment's deployment branches
  3. It waits for approval
  4. It runs without secrets

With prevent self-review enabled, who can approve a production run that ana triggered?

  1. ana
  2. Any other required reviewer
  3. Anyone with read access
  4. Nobody

Why does the production job deploy the digest from the build job instead of the latest tag?

  1. Tags are slower
  2. So production runs exactly the image that was tested in staging
  3. Digests are shorter
  4. Environments require digests