Sub topic 6
Scoping Secrets to Environments, Branches, and Jobs
This chapter assumes you can write a basic GitHub Actions workflow, know what a repository secret is, and understand environment variables in a shell. The examples use GitHub Actions. GitLab CI and other platforms offer similar controls under different names.
Why scope secrets
A secret that every job can read is exposed to every line of code that runs in your pipeline. A leaked test token is an annoyance. A leaked production deploy key is an incident. Scoping means giving each secret the smallest audience that still lets the work get done. This is the least privilege principle applied to pipelines.
You can narrow a secret along four axes:
| Axis | Question it answers | Typical control |
|---|---|---|
| Environment | Which deployment target may use it? | Environment secrets |
| Branch | Which code may reach it? | Branch rules on the environment |
| Job | Which unit of work receives it? | `env:` on a step or job |
| Credential | What can the secret do if stolen? | Narrow permissions on the token itself |
Scope by environment
Repository secrets are visible to every workflow in the repository. Environment secrets are visible only to jobs that declare environment: <name>. Create one environment per deployment target, such as staging and production, and store a different value in each.
jobs:
deploy-production:
runs-on: ubuntu-latest
environment: production
steps:
- name: Deploy
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
run: ./deploy.shBoth environments can use the secret name DEPLOY_TOKEN. The job resolves the value from the environment it names. A job without environment: production cannot read production's value.
Environments also support protection rules. Required reviewers pause the job until a person approves it, and the secret is not released before then. A wait timer adds a delay.
Scope by branch
An environment can be restricted to specific branches or tags through its deployment branch policy. If you allow only main for production, a job running from a feature branch is refused the environment and its secrets, even if someone edits the workflow file to request it.
Scope by job and step
Declare a secret only where it is used. Setting env: at the workflow level places the value in every step of every job. Setting it on one step limits exposure to that step's process.
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run unit tests
run: make test
publish:
needs: test
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- name: Publish package
env:
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
run: make publishThe test job runs code from dependencies and test files, and it never sees REGISTRY_TOKEN. Only the final step of publish does. Splitting a workflow into separate jobs gives you a clean boundary for secrets, because each job runs on its own runner.
Scope the credential itself
Scoping the delivery path is half the work. The other half is what the secret can do. Prefer a token limited to one repository, one project, or one API action over an account-wide key. Use read-only credentials for jobs that only read. If a staging token is stolen, it should not open production.
You should also limit the automatic GITHUB_TOKEN:
permissions:
contents: readThis sets read-only repository access for all jobs. A job that needs more can request it in its own permissions: block.
Common errors
- Defining a secret at the repository level when only one environment needs it.
- Sharing one credential across staging and production, so a leak in the lower tier reaches the higher one.
- Placing
env:at the workflow top level out of convenience. - Protecting production with a branch
if:condition instead of an environment branch policy. - Granting broad scopes to a token because narrower ones took time to work out.
Exercise
Your workflow has one job that builds the app, runs tests, and deploys to production. The production API key is a repository secret and is set in a workflow-level env: block. Name three changes that reduce its exposure.
Show answer
First, move the key to an environment secret on a production environment and set environment: production only on the deploy job. Second, split deployment into its own job that depends on the build and test job, so tests never run on a runner holding the key. Third, remove the workflow-level env: and set the key only on the single deploy step that needs it. Optionally, restrict the environment to the main branch and add required reviewers.
Exercise
A developer adds if: github.ref == 'refs/heads/main' to the deploy job and considers the production secret protected. Why is this not enough, and what should replace it?
Show answer
The condition lives in the workflow file, which any user who can push a branch can edit. They can remove the condition on their branch and run the job. The restriction should be a deployment branch policy on the production environment, which is configured in repository settings and cannot be changed from the workflow file.