Sub topic 4
Targeting an environment from a job
This chapter assumes you can read workflow YAML, know how jobs and needs work, and understand why secrets stay out of source control. It covers the environment key, which connects a job to a deployment target.
The environment key
A job targets an environment with the environment key. The job then runs under that environment's rules and can read its secrets and variables.
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}When the runner would normally start this job, GitHub first checks the production environment's protection rules. The job waits until they pass. Only then does it get the environment's secrets, so secrets.DEPLOY_TOKEN resolves to the environment's value.
Two syntax forms
The short form is a string. The long form is a mapping with name and an optional url.
environment:
name: production
url: https://example.comThe url appears as a link on the deployment in the GitHub UI. It can be an expression, such as one built from a step output, which is useful when each deployment gets its own address.
What happens at the job level
The environment belongs to a job, not to a workflow or a step. Keep these points in mind:
- Each job that needs an environment declares it separately.
- Jobs without the key run with no environment rules and see only repository-level and organization-level secrets.
- A job that names a missing environment creates it automatically. The new environment has no protection rules, so a typo in the name silently produces an unprotected target.
The last point is a common error. If you meant production and wrote prodution, the job runs immediately with no reviewers and no environment secrets.
Each use creates a deployment
Every run of a job that targets an environment creates a deployment record. The repository's Deployments view and the environment's history list these records, along with their status and the optional url.
A job can be in one of several states while it waits.
Common errors
| Symptom | Likely cause |
|---|---|
| Secret is empty | The secret is set on a different environment, or the job has no `environment` key |
| Job runs with no approval | The environment name is misspelled, so an unprotected one was created |
| Job is blocked from starting | The branch or tag is not allowed to deploy to this environment |
| Job stays in "waiting" | A rule such as required reviewers or a wait timer has not been satisfied |
Checking your understanding
Exercise
A teammate renames the environment in a workflow from production to prod, but the repository only has an environment called production with required reviewers. What happens when the job runs, and why?
Show answer
GitHub creates a new environment called prod with no protection rules. The job runs without waiting for reviewers and cannot read the secrets stored on production. The fix is to use the exact name of the existing environment, or to create prod and configure its rules and secrets.
Exercise
Write the long form of the environment key for a job that targets staging and shows the link https://staging.example.com on the deployment.
Show answer
environment: name: staging url: https://staging.example.com