Sub topic 2
Your first workflow file
This chapter assumes you know basic Git and GitHub usage (repositories, branches, pull requests) and can read YAML. It also assumes your project has a test command that runs from the terminal. The examples use a Node.js project with npm test, but the structure is the same for other languages.
Where workflow files live
GitHub Actions reads workflow files from the .github/workflows directory at the root of your repository. Each file ending in .yml or .yaml defines one workflow. GitHub finds them by location, so a file stored anywhere else is ignored.
Create the file .github/workflows/test.yml and commit it to your repository. Once it is on a branch GitHub watches, the workflow starts running when its trigger fires.
The minimal test workflow
This workflow runs your tests on every push to main and on every pull request.
name: Tests
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testReading the file top to bottom
The file has three top-level keys. Each one answers a different question.
| Key | Question it answers |
|---|---|
| `name` | What label should GitHub show for this workflow? |
| `on` | Which events start it? |
| `jobs` | What work should it do? |
name is optional. Without it, GitHub shows the file path instead. A short name such as Tests is easier to spot in the Actions tab and in pull request checks.
The on key lists events. Here, push with branches: [main] means only pushes to main trigger the run. A bare pull_request triggers on pull request activity such as opening one or pushing new commits to it. The next chapter covers triggers in more detail.
Under jobs, you give each job an ID. This one is called test. The job runs on ubuntu-latest, a GitHub-hosted Linux virtual machine, and then runs its steps in order.
The four steps
actions/checkout@v4copies your repository into the runner. A fresh runner starts empty, so without this step there is nothing to test.actions/setup-node@v4installs Node.js. Thewithblock passes inputs to the action, here the version number.npm ciinstalls dependencies exactly as recorded inpackage-lock.json. It fails if the lockfile andpackage.jsondisagree, which is what you want in CI.npm testruns your test suite.
Steps that start with uses run a reusable action. Steps that start with run execute a shell command. If any step exits with a non-zero status, the job stops and is marked as failed. This is how a failing test becomes a failing check.
How the pieces connect
Running it
- Save the file as
.github/workflows/test.yml. - Commit it and push it to
main, or open a pull request that contains it. - Open the Actions tab of your repository.
- Select the Tests workflow and open the latest run to see the job and its steps.
A green check means every step succeeded. A red cross means a step failed, and you can expand that step to read its output.
Common first-time mistakes
- The file is in
.github/workfloworgithub/workflows. The directory name must match exactly, including the leading dot. - The
checkoutstep is missing, so commands fail with "no such file" or "missing script" errors. - The branch name under
pushdoes not match your default branch. If your default branch ismaster, a filter formainnever fires. npm testis not defined inpackage.json, so the step fails even though the workflow is correct.
Exercise
Your workflow file is committed to the repository, but nothing appears in the Actions tab after you push. The file is at .github/workflow/test.yml. What is wrong, and what is one other cause you would check if the path were correct?
Show answer
The directory must be named workflows, with an s, so GitHub ignores files in .github/workflow. Moving the file to .github/workflows/test.yml fixes it. If the path were correct, check that the push matched the trigger, for example that the pushed branch is the one listed under push.branches. Also check for a YAML syntax error that stops the file from parsing.
Exercise
Why does the workflow run npm ci instead of npm install?
Show answer
npm ci installs exactly the versions recorded in package-lock.json and fails if the lockfile and package.json disagree. That makes CI runs reproducible. npm install can update the lockfile and install different versions.