Sub topic 5
Checking out code and installing the toolchain
This chapter assumes you can read a basic workflow file, and that you know what a job, a step and a runner are from the earlier chapters. It also assumes your project has a test command that runs from the terminal. Here you will learn what a fresh runner contains, and how to prepare it so the tests can run.
Start from an empty machine
Each job runs on a new virtual machine. Nothing from your repository is on it, and nothing from a previous run is left behind. Before npm test can work, the job has to do two things:
- Get your code onto the runner.
- Install the language runtime and the project's dependencies.
Skipping either one is the most common reason a first workflow fails.
Checking out the code
The actions/checkout action copies your repository onto the runner. It is a reusable piece of code that someone else wrote, and you refer to it with uses.
steps:
- uses: actions/checkout@v4The part after @ is the version of the action. v4 is a tag that follows the latest 4.x release. By default, checkout fetches the commit that triggered the run. For a pull request, that is a temporary merge of the pull request into its base branch. Your tests therefore run against the code as it would look after merging.
Checkout also fetches only one commit by default, which is faster. If a test needs the full Git history, for example to read tags, add fetch-depth: 0 under with.
Installing the language runtime
GitHub's Ubuntu runners come with several tool versions already installed, but you should not depend on them. They change over time, and your project needs a specific version. Use a setup action to choose one explicitly. For Node.js, that action is actions/setup-node.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20The with block passes inputs to the action. Other ecosystems have equivalent actions, such as actions/setup-python and actions/setup-java. They all take the version as an input.
Installing dependencies
After the runtime is ready, install the project's dependencies with a run step. A run step executes a shell command on the runner.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testUse npm ci instead of npm install in CI. It installs exactly what package-lock.json lists, and it fails if the lockfile and package.json disagree. This makes runs repeatable, so a green build on your machine means the same thing on the runner.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `package.json` not found, or `npm ci` cannot find a lockfile | The checkout step is missing or comes after the install step | Put `actions/checkout` first |
| `npm ci` fails with a lockfile mismatch | `package-lock.json` was not committed or is out of date | Run `npm install` locally and commit the lockfile |
| Tests pass locally but fail on the runner | The local Node.js version differs from `node-version` | Pin the same version in the workflow |
| `node-version: 20` behaves differently over time | A version number such as `20` resolves to the latest 20.x release | Pin a full version if you need exact repeatability |
Exercise
A workflow has the steps actions/setup-node@v4, npm ci, npm test, in that order, and no checkout step. What error do you expect, and what is the fix?
Show answer
npm ci fails because there is no package.json or lockfile in the working directory, since the repository was never copied onto the runner. Add - uses: actions/checkout@v4 as the first step.
Exercise
Why is npm ci preferred over npm install in a CI workflow?
Show answer
npm ci installs exactly the versions in package-lock.json and fails if the lockfile does not match package.json. This keeps runs repeatable.