Skip to content

DevOps Operations / Continuous integration · Step 4 of 7

Lesson: Builds and TestsSwitch
← DevOps Operations

Delivery and containers

Builds and Tests

Run a build and tests in CI, and make failures show up as failures.

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:

yaml
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 test

setup-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:

bash
test -f missing.txt; echo $?
1

You can hide failures by accident. This always succeeds:

bash
npm test || true

Pipelines can hide them too, because the status of a pipeline is normally that of its last command:

bash
false | cat; echo $?
set -o pipefail
false | cat; echo $?
0
1

In 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:

yaml
    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.