GitHub Actions is the automation service built into GitHub. When something happens in your repository, for example a push or a pull request, it starts a virtual machine, checks out your code and runs the steps you describe in a YAML file. It is a very good first CI tool, because it needs no server of your own, and the ideas you learn here (triggers, jobs, steps, secrets, artifacts) carry over to Jenkins, GitLab CI and the others. In this lesson you build a pipeline for a small Node project.
Why do we need it?
Running tests by hand is slow and people forget. A pipeline runs the same checks on every change, so mistakes are caught in minutes. GitHub Actions needs no server of your own.
A real-life example
It is like a robot assistant at a factory gate who inspects every product before it leaves. You write the inspection list once, and the robot follows it every time without getting tired.
The vocabulary
- Workflow: one YAML file in
.github/workflows/. A repository can have many. - Trigger (`on`): the event that starts it, such as
push,pull_request, a schedule or a manual button. - Job: a group of steps that runs on one fresh machine (a runner). Jobs run in parallel unless you say one
needsanother. - Step: one command (
run) or one reusable action (uses). - Action: a ready-made step from the marketplace, for example
actions/checkout. - Runner: the virtual machine, such as
ubuntu-latest.
The sample project
The project is tiny on purpose: one function, two tests that use Node’s own test runner, and a build script. It was run in the course lab with Node 22: npm ci installed with zero packages, npm test passed both tests, and npm run build printed build finished. The lock file package-lock.json is part of the project, because npm ci needs it for repeatable installs.
{
"name": "ci-demo",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": { "test": "node --test", "build": "node build.js" }
}
export const sum = (a, b) => a + b;
import test from 'node:test';
import assert from 'node:assert';
import { sum } from '../sum.js';
test('adds two numbers', () => assert.equal(sum(2, 3), 5));
test('works with negatives', () => assert.equal(sum(-2, 1), -1));
import { mkdirSync, writeFileSync } from 'node:fs';
mkdirSync('dist', { recursive: true });
writeFileSync('dist/build-info.txt', 'built ok\n');
console.log('build finished');
The workflow file
Save this as .github/workflows/ci.yml. The file was checked by parsing the YAML and reading back its triggers, matrix and steps. It was not executed on GitHub for this course, because that needs a real repository, so the first run on your own repository is your final test.
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm test
- run: npm run build
- uses: actions/upload-artifact@v4
if: matrix.node == 22
with:
name: dist
path: dist/
Reading it line by line
onstarts the workflow for pushes tomainand for pull requests that targetmain.strategy.matrix.noderuns the job twice, once with Node 20 and once with Node 22. A matrix is how you test against several versions without copying the job.actions/checkout@v4downloads your code onto the runner. Without it the machine is empty.actions/setup-node@v4installs Node and, withcache: npm, keeps downloaded packages between runs to save time.npm ciinstalls exactly what the lock file says. It is faster and stricter thannpm install, and it is what CI should use.npm testandnpm run buildare your checks. If any step exits with a failure code, the job stops and turns red.upload-artifactsaves the build output so you can download it or use it in a later job. Theiflimits it to one matrix run.
Make the pipeline matter
- Push the project and the workflow to a GitHub repository, and open the Actions tab to watch the first run.
- Break a test on purpose in a branch, open a pull request and watch the check fail.
- In the repository settings, add a branch protection rule on
mainthat requires this check to pass before merging. Now the pipeline is a real gate. - Fix the test and see the check turn green.
Secrets
Pipelines often need passwords or tokens, for example to deploy. Store them in the repository settings under Secrets, and read them in the workflow as ${{ secrets.NAME }}. GitHub hides their values in logs. Never write them into the YAML file, and give each secret only the access it needs. For deployments to a cloud, a better practice you will meet later is OpenID Connect, which lets the workflow get short-lived access without a stored long-lived key.
Next steps
Later lessons add more to this pipeline: building a Docker image, scanning it for vulnerabilities, deploying to a server or Kubernetes, and sending notifications. The pattern stays the same, only more steps. Start small and add one thing at a time, running it after each addition.
Architecture diagram
Here is the idea as a simple picture. You can use the same diagram in your own notes.
flowchart LR
P[Push or pull request] --> R[Runner starts]
R --> C[Checkout code]
C --> I[npm ci]
I --> T[npm test]
T --> B[npm run build]
B --> U[Upload artifact]
T -->|fail| X[Red check, merge blocked]
How to check it worked
- The Actions tab shows a green check for both matrix jobs.
- A pull request with a failing test cannot be merged when branch protection is on.
- You can say what
npm cidoes differently fromnpm install.
How companies use it
Teams run tests, linters and builds on every pull request, publish images and deploy from tagged releases. Branch protection blocks merges until checks pass.
Common errors and how to fix them
- Workflow does not start. The file is not in
.github/workflows/or has YAML errors. - npm ci fails. Commit
package-lock.jsonand keep it in sync with package.json. - Secret is empty. Secrets are not passed to workflows from forks. Check the name and where it is defined.
- Works locally, fails in CI. Different Node version or missing environment variables.
- YAML indentation errors. Use two spaces and a YAML linter.
Best practices
- Pin action versions.
- Cache dependencies.
- Keep jobs short.
- Store secrets in repository settings.
- Require checks before merging.
Interview questions with simple answers
- What is a workflow? A YAML file that defines automation in a repository.
- What is a runner? The machine that runs a job.
- Job vs step? A job runs on a runner and contains steps. A step is one command or action.
- What does `on` do? Defines the events that trigger the workflow.
- What is a matrix? Runs a job with several values, such as Node versions.
- What is an artifact? A file saved from a run.
- How do you use secrets? Store in settings and read as
secrets.NAME. - Why `npm ci`? Repeatable installs from the lock file.
- What is branch protection? Rules that require checks and reviews before merging.
- How do you run a workflow manually? Add
workflow_dispatch.
Practical exercises
Try each one yourself before you look at the hint. Struggling for a few minutes is how the skill sticks.
- Create the workflow and get a green run. Hint: Copy the ci.yml and push.
- Break a test on purpose and watch the check fail. Hint: Change an expected value.
- Add Node 24 to the matrix. Hint: Edit the list.
- Add a step that prints the Node version. Hint:
run: node --version. - Turn on branch protection requiring the check. Hint: Repository settings, branches.
Mini project: CI for my project
Add a pipeline to your own project that lints, tests and builds. Add a status badge to the README and require the check on pull requests.
Knowledge test
Answer the ten questions in the quiz at the end of this lesson. You pass with 60 percent. If an answer is wrong, read the explanation, then reread the part of the lesson it points to.