GitHub Actions environments: secrets and approvals
GitHub environments: environment secrets and variables, required reviewers, branch rules and wait timers, deployment URLs, and deployment: false.
In this series24 guides, you're on Environments
A GitHub environment is a named deployment target, such as staging or production, configured in the repository settings. It holds three things:
- Secrets and variables that only jobs targeting that environment can read.
- Protection rules that must pass before such a job starts: required reviewers, a wait timer, allowed branches, or a custom check.
- A deployment history, with the URL of each deployment shown in the repository and on pull requests.
A job opts in with the environment key. Without it, the job sees none of the environment’s secrets.
Environments are not the same thing as environment variables. To set an environment variable for a workflow, a job or a step, use env:: you don’t need an environment for that. An environment can also hold variables, read with ${{ vars.NAME }}.
Creating an environment#
- Open Settings → Environments in the repository and click New environment.
- Name it (
production). Names are case-insensitive. - Add protection rules, environment secrets and environment variables.
Referencing an environment that doesn’t exist yet in a workflow creates it, with no protection rules. Create it in the settings first if it guards anything sensitive.
Deploying to an environment#
name: Deploy
on: push: branches: [main]
jobs: deploy: runs-on: ubuntu-latest environment: production steps: - uses: actions/checkout@v7 - run: ./deploy.sh env: API_KEY: ${{ secrets.API_KEY }} # the production environment's secret REGION: ${{ vars.REGION }} # the production environment's variableWhen a secret or variable with the same name exists at several levels, the environment’s value wins over the repository’s, which wins over the organization’s. That lets every environment define the same API_KEY with its own value.
To promote a release from staging to production, give each environment its own job and chain them with needs:
jobs: deploy-staging: runs-on: ubuntu-latest environment: staging steps: - uses: actions/checkout@v7 - run: ./deploy.sh env: API_KEY: ${{ secrets.API_KEY }} # staging's value
deploy-production: needs: deploy-staging runs-on: ubuntu-latest environment: production steps: - uses: actions/checkout@v7 - run: ./deploy.sh env: API_KEY: ${{ secrets.API_KEY }} # production's valueThe production job starts only once staging has deployed, and then waits for production’s protection rules.
Environments and OIDC#
Instead of storing a cloud key as an environment secret, a deploy job can exchange a GitHub OIDC token for short-lived cloud credentials. On AWS:
jobs: deploy: runs-on: ubuntu-latest environment: production permissions: id-token: write # lets the job request an OIDC token contents: read steps: - uses: actions/checkout@v7 - uses: aws-actions/configure-aws-credentials@v6 with: role-to-assume: arn:aws:iam::123456789012:role/deploy-production aws-region: us-east-1 - run: ./deploy.shBecause the job references an environment, the token’s subject ↗ (sub) is repo:my-org/my-repo:environment:production, so the role’s trust policy can admit only jobs that passed the environment’s rules. Repositories created after July 15, 2026, and older ones that opt in, use an immutable format that adds the owner and repository IDs: repo:my-org@123456/my-repo@456789:environment:production. Adding environment: to a job that already assumes a role changes its sub and breaks a trust policy that matched ref:refs/heads/main. For the AWS side (the identity provider, the role’s trust policy and the common errors), see OIDC with AWS.
Showing the deployment URL#
environment also accepts a url, displayed on the run’s graph, in the repository’s deployments list and on pull requests. It can come from a step output:
jobs: preview: runs-on: ubuntu-latest environment: name: preview url: ${{ steps.deploy.outputs.url }} steps: - uses: actions/checkout@v7 - id: deploy run: | ./deploy-preview.sh "pr-${{ github.event.pull_request.number }}" echo "url=https://pr-${{ github.event.pull_request.number }}.preview.example.com" >> "$GITHUB_OUTPUT"name can be an expression too, such as ${{ inputs.environment }} from a type: environment dispatch input. It can read the github, inputs, vars, needs, strategy and matrix contexts, and url can also read steps, job, runner and env.
Protection rules#
| Rule | What it does |
|---|---|
| Required reviewers | Up to six people or teams; one of them must approve before the job starts. You can block people from approving their own deployments. |
| Wait timer | Delays the job by 1 to 43,200 minutes (30 days). Waiting isn’t billed. |
| Deployment branches and tags | Only refs matching your name patterns can deploy, for example main and releases/* (* doesn’t match /). With Protected branches only instead, every branch can deploy until you protect one. |
| Custom rules | A GitHub App (Datadog, ServiceNow or your own) approves or rejects the deployment. In public preview as of November 2026, up to six per environment. |
| Admin bypass | On by default; turn it off so admins go through the rules too. |
Until a required reviewer approves, the job waits in waiting state and can’t read the environment’s secrets. A run can wait up to 30 days for an approval.
To approve, a reviewer opens the run, clicks Review deployments, picks the environment and clicks Approve and deploy. Reject fails the run. Unless admin bypass is off, an admin can click Start all waiting jobs to skip the rules. Approving through the REST API takes a required reviewer’s token with write access to deployments.
In public repositories, all of this works on GitHub Free, Pro, Team and Enterprise. On GitHub Free, environments work only in public repositories: if a repository becomes private, its environments’ rules and secrets are ignored. On Pro and Team, private repositories get environments, their secrets and variables, and branch and tag rules; required reviewers, wait timers, custom rules and the admin bypass setting there need GitHub Enterprise.
Branch rules are matched against GITHUB_REF. A pull_request run has GITHUB_REF=refs/pull/42/merge, so it can’t deploy to an environment limited to main unless you add a refs/pull/*/merge rule.
A branch rule checks where a workflow runs from, not who started it. pull_request_target (since December 2025), issue_comment and workflow_run workflows run from the default branch, so they pass a main-only rule even when a fork PR or an outsider’s comment started them. The rule doesn’t stop such a job from running the PR’s code or deploying whenever anyone comments /deploy. Never run PR code there, and if the job can reach production, add a required reviewer.
Using environments without deployments#
Since March 2026 a job can use an environment just for its secrets and variables, without creating a deployment record, by setting deployment: false:
jobs: integration-tests: runs-on: ubuntu-latest environment: name: staging deployment: false steps: - uses: actions/checkout@v7 - run: npm run test:integration env: STAGING_API_KEY: ${{ secrets.API_KEY }}The job still gets the environment’s secrets and variables, and required reviewers and wait timers still apply, but no deployment shows up in the history. That keeps CI jobs that borrow staging credentials out of the deployment list. deployment also accepts an expression, such as ${{ github.ref_name == 'main' }}. Custom protection rules need a deployment object, so a job with deployment: false on an environment that has one fails immediately.
Environments in reusable workflows#
A job that calls a reusable workflow can’t set environment. Put environment: production on the job inside the called workflow instead. The caller must still pass the secrets, with secrets: inherit or by name, even those that exist only in the environment. A secret the caller doesn’t pass resolves to an empty string, with no error, so declare it with required: true under on.workflow_call.secrets to make the run fail when the caller forgets it.
One deploy at a time#
Environments don’t serialize deployments by themselves: two runs can deploy to production at once. Add a concurrency group:
jobs: deploy: runs-on: ubuntu-latest environment: production concurrency: group: production queue: max # keep every pending deploy, in order (up to 100) steps: - uses: actions/checkout@v7 - run: ./deploy.shAny concurrency group runs one deploy at a time. Without queue: max, a newer pending deploy replaces the one waiting, which is usually enough: the latest commit includes the others. queue: max keeps every pending deploy, for releases or migrations that must all run.
Pull requests from forks#
pull_request runs from forks get no secrets at all, environment secrets included, and an approval doesn’t change that: an environment can’t make fork PR code safe to run with secrets. If a fork PR needs a privileged follow-up (a preview deploy, a comment), run the untrusted build without secrets and do the privileged part in a separate workflow_run workflow that treats the build’s output as data. The pull_request vs pull_request_target guide shows the pattern, and where to run tests that need secrets.
Why use environments?#
- Least privilege. Production credentials are only readable by jobs that target
production, and only from the branches you allow (apull_request_target,issue_commentorworkflow_runrun counts as the default branch). - Controlled releases. A reviewer, a wait timer or an external check stands between a merge and production.
- Traceability. Every deployment is recorded with its commit, its URL and who approved it.
On RunsOn#
A RunsOn environment is not a GitHub environment: it picks the RunsOn stack that runs the job, and with it the AWS account, network and runner IAM role the job gets. To run deploys apart from CI, for example in your production AWS account, give them a stack of their own and pick it with the env label (on Fleet, the fleet’s runner label carries it):
jobs: deploy: runs-on: runs-on=${{ github.run_id }}/runner=2cpu-linux-x64/env=deploy environment: production steps: - uses: actions/checkout@v7 - run: ./deploy.shOn Flex, nothing ties that label to the GitHub environment: any job in a repository the stack serves can request env=deploy and get the stack’s runner IAM role. So keep production credentials behind the GitHub environment (its secrets, or an OIDC role that trusts only environment:production), not in the runner role. A Flex job without an env label goes to the default stack, named production, so once you run several stacks, set env on every job.