GitHub Actions workflow for Node.js: run tests on every push
A ci.yml file that installs, lints, builds and tests your Node.js project on GitHub.
A GitHub Actions workflow runs your checks on GitHub's servers each time you push code. This one installs your packages and runs the steps you choose. Fill in the options, save the file in your repository, and push.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 24
cache: npm
- run: npm ci
- run: npm run lint --if-present
- run: npm run build --if-present
- run: npm testHow this setup works
Step-by-step setup
- In your repository create the folder .github/workflows.
- Save the file above there as ci.yml.
- Commit and push to the main branch.
- Open the Actions tab on GitHub to watch the run.
- A green tick on your commit means every step passed.
What the important lines mean
- on: push starts the workflow when you push to main.
- actions/checkout downloads your code onto the runner.
- setup-node installs Node 24 and caches your npm downloads.
- --if-present skips a script that does not exist in package.json instead of failing.
Common errors and how to fix them
- npm error The npm ci command can only install with an existing package-lock.json
- The lock file is not committed. Run npm install locally and commit package-lock.json.
- npm error Missing script: "test"
- package.json has no test script. Add one under "scripts", or untick the test step.
- The workflow does not start
- The file must be in .github/workflows and end in .yml or .yaml. The push must be to the branch named in the file.
- Error: Unexpected value or a YAML error
- Indentation is wrong. YAML uses spaces, never tabs. Paste the file into a YAML checker to find the line.
- Both branches and branches-ignore error
- You cannot use both for the same event. Use branches with ! patterns instead; order matters because later patterns override earlier ones.
- Environment variable set in one step is missing in the next
- Each step runs in its own process, so shell environment changes do not carry over. Write to $GITHUB_ENV or use env on the job.
Frequently asked questions
- Where do I put a GitHub Actions workflow file?
- In the .github/workflows folder at the top of your repository. Each .yml file there is a separate workflow.
- How do I run a workflow only on one branch?
- List the branch under on: push: branches:, as this file does for your chosen branch.
- Do the action versions need updating?
- From time to time. The @v4 parts are version tags. GitHub shows newer versions in the action's own page, and Dependabot can update them for you.
- What permissions does my workflow have?
- The GITHUB_TOKEN starts from the repository default. Setting any scope in a permissions block sets every unspecified scope to none.
- How do I cancel older runs on the same branch?
- Add concurrency with a group and cancel-in-progress: true. Without that a newer queued run only replaces a pending one.
- Must the file be named a certain way?
- It must be YAML ending in .yml or .yaml inside .github/workflows.
More developer configs
- package.json fileCreate a package.json with the right name, version, module type, scripts and Node version.
- tsconfig.json fileCreate a tsconfig.json for a Node or browser TypeScript project, with strict mode, target and output folder.
- SSH config for multiple GitHub accountsUse two or three GitHub accounts on one computer with separate SSH keys and host names.
- SSH config fileGive servers short names and choose the right key for each.
- .gitconfig setupSet your name, email, default branch and shortcuts once.
- .env file exampleCreate a .env file for your app: port, database address and a secret key.