Sub topic 5
Build and test before you deploy
This chapter assumes you can read a workflow file, know that jobs run on runners, and have used uses: to call an action. It builds on the earlier chapters on triggers and jobs. Here you add the stage that decides whether a deployment should happen at all.
Why the build comes first
A deploy job should only run on code that has already compiled and passed its tests. If the deploy step is the first place a broken build shows up, you have already changed the live environment. Putting build and test in a separate job upstream gives you a gate. If that job fails, the jobs that depend on it never start.
A build and test job
The following workflow runs on pull requests and on pushes to main. Running it on pull requests means problems show up before the merge, not after.
name: CI
on:
push:
branches: [main]
pull_request:
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 buildEach step runs in order, and a step that exits with a non-zero status fails the job. That is the whole gate mechanism. You do not need extra logic to stop on a failing test.
Choosing the commands
Pick commands that behave the same on every run.
npm ciinstalls exactly whatpackage-lock.jsonlists and fails if the lock file andpackage.jsondisagree.npm installcan change the lock file, so avoid it in CI.npm testshould exit non-zero on any failure. Check that your test runner does this, and that it does not start a watch mode that never exits.npm run buildruns after the tests so a slow build does not delay feedback from a failing test.
The same shape works for other stacks. Swap in actions/setup-python with pip install -r requirements.txt and pytest, or actions/setup-java with mvn verify.
Order of steps
Run cheap checks first. A linter or type check that takes seconds should come before a test suite that takes minutes. You get a failure sooner and use fewer runner minutes.
Keep the build output in a known folder, such as dist. The next chapter uploads that folder as an artifact so the deploy job receives the same files you tested.
Common errors
- Tests pass locally but fail in CI. The runner starts clean. Look for tests that rely on files, environment variables, or a database that exist only on your machine.
npm cifails with a lock file error. Commit the updatedpackage-lock.json.- The deploy job runs even though tests failed. The deploy job is missing
needs: build. Without it, jobs run in parallel. - A step is marked
continue-on-error: true. The step can fail without failing the job. Remove it from test steps.
Connecting the gate to deployment
The deploy job reaches the gate through needs:
jobs:
deploy:
needs: build
runs-on: ubuntu-latestBy default, a job with needs runs only if every listed job succeeded. If build fails or is cancelled, deploy is skipped.
Exercise
Your workflow has build and deploy jobs, and deploy runs even when a test fails. Name the likely cause and the fix.
Show answer
The deploy job has no needs: build, so the two jobs run in parallel. Add needs: build to deploy. Also check that no test step uses continue-on-error: true or || true.