Labs · DevOps
Lab: Write a GitHub Actions Workflow That Runs Node.js Tests on Every Pull Request and Blocks a Red Merge
Course: DevOps · Chapter 9: GitHub Actions Hands-On · Lesson: 9.11 Lab: PR tests and main image push
Chapter 9 explains workflows, jobs, steps, secrets and branch protection; this lab sets up the most common one.
Chala mitrano! Code review aadhi robot ne test chalavle tar reviewer cha vel vaachto. Aaj to robot banvuya.
Suppose we are…
Suppose we are on a Netflix-style team where twenty pull requests are opened every day. If a reviewer has to pull every branch and run tests by hand, reviews wait for hours, and sometimes a broken change slips into main.
In this lab GitHub Actions runs the tests on every pull request. A green tick means tests pass; a red cross blocks the merge because we make the check required.
Goal of this lab
By the end you will have:
- A workflow
.github/workflows/ci.ymlthat runs on pull requests tomain. - One red run (failing test) and one green run.
- Branch protection on
mainwith the test job as a required check.
What you need
- A free GitHub account and a new repository of your own (public or private).
- Git on your laptop and sign-in that can push (lab 1 of this course).
- 45 minutes.
Part 1: A tiny app with a test
Clone your new repo and add the app, a test and
package.json:git clone https://github.com/YOURUSER/badge-ci.git && cd badge-ci cat > sum.js <<'EOF' module.exports = (a, b) => a + b; EOF cat > sum.test.js <<'EOF' const assert = require("node:assert"); const test = require("node:test"); const sum = require("./sum"); test("adds two numbers", () => assert.strictEqual(sum(2, 3), 5)); EOF cat > package.json <<'EOF' { "name": "badge-ci", "version": "1.0.0", "scripts": { "test": "node --test" } } EOF
Part 2: The workflow
Create the workflow file:
.github/workflows/ci.yml
name: ci on: pull_request: branches: [main] push: branches: [main] permissions: contents: read jobs: test: runs-on: ubuntu-latest timeout-minutes: 10 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 20 - run: npm testmkdir -p .github/workflowspermissions: contents: readgives the job only read access.timeout-minutesstops a stuck job.Commit and push to main:
git add . git commit -m "Add sum function, test and CI workflow" git push origin mainWhat you should see: on the repo's Actions tab, a run named
citurns green in about a minute.
Part 3: Protect main
On GitHub open Settings → Branches → Add branch ruleset (or Add classic branch protection rule) for
main. Turn on Require a pull request before merging and Require status checks to pass, then add the checktest. Save.What you should see:
mainlisted under protected branches.
Part 4: A red pull request, then a green one
Break the function on a branch and open a pull request:
git switch -c bug/minus echo "module.exports = (a, b) => a - b;" > sum.js git commit -am "Change sum (introduces a bug)" git push -u origin bug/minusOn GitHub click Compare & pull request, then Create pull request.
Watch the check on the pull request page.
What you should see:
ci / testwith a red cross and the merge button blocked. Open Details and find the failing assertionExpected values to be strictly equal: -1 !== 5.Fix the bug on the same branch and push again:
echo "module.exports = (a, b) => a + b;" > sum.js git commit -am "Fix sum" git pushWhat you should see: the check turns green and Merge pull request becomes available.
Ravindra Bagale's Tip
Always read a red log from the first error line, not the last line. The last line usually just says "Process completed with exit code 1".
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Workflow file in the wrong folder | No run appears in Actions | It must be .github/workflows/*.yml |
| Tabs in YAML | Invalid workflow file | Use two spaces for indentation |
| Required check name typed wrongly | Merge stays blocked forever | Pick the check name from the list after one run |
| Putting tokens in the workflow file | Secret visible to everyone with repo access | Use Settings → Secrets and variables → Actions |
Self-check checklist
0 of 4 done
Try-at-home challenge
Add a matrix so tests run on Node 18, 20 and 22 (strategy: matrix: node: [18, 20, 22]). Then add an npm ci cache with cache: npm in setup-node and compare run times.
Samajla ka? Robot tests first, human review second. Aata pudhe jaauya Jenkins pipelines.