TExcelTAD
Home › Release Engineering & CI/CD › Environments, Approvals & Concurrency

Environments, Approvals & Concurrency

Building a release is safe to automate completely. Deploying or publishing it is where teams want a gate: a person who approves, a rule that only main or v* tags may deploy, secrets that only the production job can read, and a guarantee that two deployments never run at the same time. GitHub Actions provides these through environments and concurrency.

Environments

An environment (Settings → Environments, or created on first use) is a named target such as staging, production or npm. A job opts in with environment::

yaml
jobs:
  deploy-production:
    needs: build
    runs-on: ubuntu-24.04
    environment:
      name: production
      url: https://app.example.com          # shown on the run and the deployment
    steps:
      - uses: actions/download-artifact@v8
        with:
          name: release-files
      - run: ./deploy.sh
        env:
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}   # an *environment* secret
          REGION: ${{ vars.REGION }}                  # an environment variable

What an environment can enforce before the job starts:

Protection Details
Required reviewers up to 6 users or teams; one approval is enough; optionally prevent self-review (whoever triggered the run can't approve it)
Wait timer 1 to 43,200 minutes (30 days) before the job may start; useful for "deploy to production 30 minutes after staging"
Deployment branches and tags no restriction, protected branches only, or selected patterns (e.g. main and v* tags)
Custom protection rules GitHub Apps that approve or reject (change-management systems, monitoring checks)
Admin bypass allowed by default; can be turned off

Environment secrets are only available to jobs that reference the environment, and only after approval. That is the main security benefit: a pull request workflow can't read production credentials, because it never references the production environment, and the branch rule stops it from trying.

Availability depends on the plan: on GitHub Free, Pro and Team, required reviewers and wait timers work in public repositories only; private repositories need Enterprise for them. Deployment branch rules and environment secrets work in private repositories from Pro/Team upwards.

A gated release pipeline

yaml
on:
  push:
    tags: ['v*']
jobs:
  build:      { uses: ./.github/workflows/build.yml }
  staging:
    needs: build
    environment: staging
    runs-on: ubuntu-24.04
    steps:
      - run: ./deploy.sh staging
  production:
    needs: staging
    environment: production        # waits here for a reviewer
    runs-on: ubuntu-24.04
    steps:
      - run: ./deploy.sh production

The run pauses at production with a "Review deployments" button. The deployment record (who approved, when, which commit) is the audit trail auditors ask for.

Concurrency

Two deployments of the same environment at the same time, or two release runs racing to push tags, cause the most confusing failures. A concurrency group allows at most one running job or workflow per group:

yaml
# workflow level: one run per branch; a new push cancels the outdated run (good for CI)
concurrency:
  group: ci-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
yaml
# job level: deployments are serialised but never cancelled halfway
jobs:
  deploy:
    concurrency:
      group: deploy-production
      cancel-in-progress: false

The rules (group names are case-insensitive):

  • at most one running run/job per group;
  • by default at most one pending: when a new one arrives, an already pending one is cancelled and replaced (queue: single);
  • queue: max keeps up to 100 pending runs in order instead (and can't be combined with cancel-in-progress: true);
  • cancel-in-progress: true cancels the running one when a new one arrives.

The model below plays the same sequence of pushes through the three configurations:

python · try it yourself
def simulate(events, cancel_in_progress=False, queue="single"):
    running, pending, log = None, [], []
    for t, kind, name in events:
        if kind == "start":
            if running is None:
                running = name; log.append(f"t={t} {name} runs")
            elif cancel_in_progress:
                log.append(f"t={t} {running} cancelled (in progress)"); running = name
                log.append(f"t={t} {name} runs")
            elif queue == "single":
                for p in pending:
                    log.append(f"t={t} {p} cancelled (pending, replaced)")
                pending = [name]; log.append(f"t={t} {name} pending")
            else:
                pending.append(name); log.append(f"t={t} {name} pending ({len(pending)} queued)")
        elif running:  # the running one finishes
            log.append(f"t={t} {running} finished")
            running = pending.pop(0) if pending else None
            if running:
                log.append(f"t={t} {running} runs")
    return log

pushes = [(0, "start", "deploy#1"), (1, "start", "deploy#2"), (2, "start", "deploy#3"),
          (5, "finish", None), (9, "finish", None)]
for title, kwargs in [("default (queue: single)", {}),
                      ("queue: max", {"queue": "max"}),
                      ("cancel-in-progress: true", {"cancel_in_progress": True})]:
    print(f"--- {title}")
    for line in simulate(pushes, **kwargs):
        print("  " + line)
--- default (queue: single)
  t=0 deploy#1 runs
  t=1 deploy#2 pending
  t=2 deploy#2 cancelled (pending, replaced)
  t=2 deploy#3 pending
  t=5 deploy#1 finished
  t=5 deploy#3 runs
  t=9 deploy#3 finished
--- queue: max
  t=0 deploy#1 runs
  t=1 deploy#2 pending (1 queued)
  t=2 deploy#3 pending (2 queued)
  t=5 deploy#1 finished
  t=5 deploy#2 runs
  t=9 deploy#2 finished
  t=9 deploy#3 runs
--- cancel-in-progress: true
  t=0 deploy#1 runs
  t=1 deploy#1 cancelled (in progress)
  t=1 deploy#2 runs
  t=2 deploy#2 cancelled (in progress)
  t=2 deploy#3 runs
  t=5 deploy#3 finished

Read the three outcomes as policies:

  • default: the newest pending deployment wins, intermediate ones are skipped (#2 never runs). Right for "deploy the latest main".
  • queue: max: every deployment runs, in order. Right when each run matters (a release per tag).
  • cancel-in-progress: always the newest, even at the cost of interrupting one. Right for CI on pull requests; dangerous for deployments and releases (a half-pushed release).

Test yourself

Why are environment secrets safer than repository secrets for production credentials?

  1. They are encrypted twice
  2. Only jobs that reference the environment can read them, and only after its protection rules (approval, branch rules) pass
  3. They expire automatically
  4. They are hidden from admins

Three deployments are triggered quickly into a group with default concurrency settings. Which run?

  1. All three, in order
  2. The first and the last; the middle one is cancelled while pending
  3. Only the last
  4. Only the first

Which setting is a bad idea for a job that publishes a release?

  1. concurrency with a fixed group
  2. cancel-in-progress: true
  3. environment: production
  4. timeout-minutes