The heart of CI is simple: build the project, then test it. If either one goes wrong, the run must turn red.
Build, then test
A build turns source code into something runnable, and often installs dependencies first. Tests check that it behaves as expected. This example is for a Node.js project with build and test scripts defined in package.json:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 22
- run: npm ci
- run: npm run build
- run: npm testsetup-node selects a Node.js version. npm ci installs exactly what the lock file records and fails if it disagrees with package.json, which suits CI.
Exit status decides
A step fails when its command exits with a nonzero status. Try it in a terminal:
test -f missing.txt; echo $?1You can hide failures by accident. This always succeeds:
npm test || truePipelines can hide them too, because the status of a pipeline is normally that of its last command:
false | cat; echo $?
set -o pipefail
false | cat; echo $?0
1In GitHub Actions on Linux, the default run shell stops on errors but does not enable pipefail. Setting shell: bash on the step does.
Testing several versions
A matrix runs the same job once for each value:
strategy:
matrix:
node: [20, 22]
steps:
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}This creates two jobs. By default, when one matrix job fails, the others that are still running or queued are cancelled. Set fail-fast: false under strategy to let them all finish.
Flaky tests
A flaky test passes and fails without any code change, often because of timing, test order or a network call. Rerunning until green hides the problem. Find the cause, or clearly mark and track the test.
Try it
Write a script check.sh containing grep -q hello greeting.txt. Run sh check.sh; echo $? with and without the word in the file. Then break it on purpose with || true and see why that is dangerous. The streams and text lab helps you practise pipes.
Check your understanding
Builds and Tests
All 3 sections · no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.