Sub topic 11
Putting the deploy job together
This chapter assumes you know triggers, jobs and needs, artifacts, secrets, environments, and the basics of OIDC from earlier chapters. Here you combine them into one deploy job and see what each line does.
What the deploy job needs
A deploy job has five jobs of its own, and each one maps to an earlier concept:
| Need | Where it appears in the job |
|---|---|
| Run only after tests pass | `needs: build` |
| Get the built files | `actions/download-artifact` |
| Require approval and scope secrets | `environment: production` |
| Get cloud credentials without stored keys | `permissions: id-token: write` and `configure-aws-credentials` |
| Never run two deploys at once | `concurrency` |
The complete workflow
name: Deploy
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm test
- run: npm run build
- uses: actions/upload-artifact@v4
with:
name: site
path: dist
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write
contents: read
concurrency:
group: deploy-production
cancel-in-progress: false
steps:
- uses: actions/download-artifact@v4
with:
name: site
path: dist
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1
- run: aws s3 sync dist s3://example-bucket --deleteHow the run unfolds
The build job runs first. The deploy job starts only if build succeeds. When it starts, GitHub checks the production environment's protection rules, so a required reviewer must approve before any step runs.
Why each choice matters
- Job-level permissions. The workflow default is
contents: read. The deploy job addsid-token: write, which lets it request an OIDC token. The build job never gets that right. cancel-in-progress: false. A second deploy waits in the queue instead of killing a deploy that is half finished. Cancelling mid-sync can leave the bucket in a mixed state.- Artifact, not rebuild. Deploying the exact files that passed tests means production gets what you verified.
--delete. It removes files from the bucket that are not indist. This keeps the bucket matching the build, but a wrongpathcan empty the bucket.
Common mistakes
- Putting
id-token: writein the job but omittingcontents: read. A job-levelpermissionsblock replaces the workflow-level one, so any scope you leave out becomesnone. - Using a different artifact
namein upload and download. The names must match exactly. - Writing the environment name with a different spelling than the one in repository settings. GitHub then creates a new environment with no rules.
Exercise
The deploy job sets its own permissions block with only id-token: write. A later step fails with an error from actions/checkout. What happened, and how do you fix it?
Show answer
A job-level permissions block replaces the inherited one, so every scope not listed is set to none. contents is now none, and checkout cannot read the repository. Add contents: read to the job's permissions block, next to id-token: write.