Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Write a GitHub Actions Workflow That Runs Node.js Tests on Every Pull Request and Blocks a Red Merge

Beginner45 minA free GitHub account · Git · Node.js 20 (optional, for local tests)

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.yml that runs on pull requests to main.
  • One red run (failing test) and one green run.
  • Branch protection on main with 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

  1. 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

  1. 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 test
    
    mkdir -p .github/workflows
    

    permissions: contents: read gives the job only read access. timeout-minutes stops a stuck job.

  2. Commit and push to main:

    git add .
    git commit -m "Add sum function, test and CI workflow"
    git push origin main
    

    What you should see: on the repo's Actions tab, a run named ci turns green in about a minute.

Part 3: Protect main

  1. 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 check test. Save.

    What you should see: main listed under protected branches.

Part 4: A red pull request, then a green one

  1. 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/minus
    

    On GitHub click Compare & pull request, then Create pull request.

  2. Watch the check on the pull request page.

    What you should see: ci / test with a red cross and the merge button blocked. Open Details and find the failing assertion Expected values to be strictly equal: -1 !== 5.

  3. 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 push
    

    What 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

MistakeWhat happensFix
Workflow file in the wrong folderNo run appears in ActionsIt must be .github/workflows/*.yml
Tabs in YAMLInvalid workflow fileUse two spaces for indentation
Required check name typed wronglyMerge stays blocked foreverPick the check name from the list after one run
Putting tokens in the workflow fileSecret visible to everyone with repo accessUse 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.