Sub topic 13
Building a staging-to-production pipeline
This chapter assumes you can read workflow YAML, know how needs orders jobs, and have already set up a production environment with required reviewers. Here you combine those pieces into a pipeline where production only deploys after staging succeeds and a human approves.
The pipeline shape
The pipeline has three jobs in a chain: build, deploy to staging, deploy to production. Two mechanisms control the order:
needscontrols when a job becomes eligible to run.environmentcontrols whether the eligible job is allowed to start, because protection rules are checked at that point.
The workflow
name: Release
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: ./build.sh
deploy-staging:
needs: build
runs-on: ubuntu-latest
environment: staging
steps:
- run: ./deploy.sh staging
deploy-production:
needs: deploy-staging
runs-on: ubuntu-latest
environment:
name: production
url: https://example.com
steps:
- run: ./deploy.sh productionEach deploy job names its own environment. The staging environment usually has no rules, so that job starts as soon as build passes. The production job waits for deploy-staging, and then GitHub evaluates the production environment's rules. If a required reviewer is configured, the run shows "Waiting" until someone approves.
The long form name plus url lets you attach a link to the deployment. It appears on the run summary and in the repository's deployments list.
What happens at each step
- A push to
mainstarts the run. Onlybuildis eligible. deploy-stagingruns with the staging environment's secrets and variables.- When staging succeeds,
deploy-productionbecomes eligible. GitHub checks branch restrictions, then the wait timer and reviewers. - After approval, the job starts and can read the production secrets. Before approval, it cannot.
Secrets with the same name, such as DEPLOY_TOKEN, can exist in both environments with different values. The job gets the value from the environment it targets, so the same deploy.sh works for both stages.
Common errors
- Missing
needs. Withoutneeds: deploy-staging, the production job is eligible immediately and waits for approval in parallel with staging. A reviewer could then approve a release that staging has not validated. - Rules attached to the wrong environment. Protection rules belong to the environment, not the workflow. If the job says
environment: prodbut the rules live onproduction, GitHub creates a new unprotectedprodenvironment and the job runs without waiting. - Branch policy blocks the run. If production allows only
mainand the workflow runs from a feature branch, the job fails before it reaches approval. - Failed staging. When
deploy-stagingfails,deploy-productionis skipped by default. Reviewers are never asked.
Exercise
A teammate removes needs: deploy-staging from the deploy-production job. The production environment still requires one reviewer. What changes in how the run behaves, and why is that a problem?
Show answer
The production job no longer waits for staging. It becomes eligible right after build (or immediately, if it has no other needs), so it waits for reviewer approval in parallel with the staging deployment. The reviewer protection still applies, but the staging check is gone, so a release that fails in staging can still be approved and deployed to production.