Test and Debug GitHub Actions Locally with nektos/act: Stop Pushing Dummy Commits

DevOps tutorial - IT technology blog
DevOps tutorial - IT technology blog

Staring at a Git log full of fix ci, try again, and please work gets old fast. Waiting two or three minutes for a remote runner—only to find a missing environment variable or a typo in a shell script—kills your momentum. nektos/act fixes that by running your GitHub Actions workflows inside local Docker containers on your own machine.

1. Three Ways to Test GitHub Actions

Not every CI check needs a full container run. Here is how the three main testing approaches compare in day-to-day work:

  • Push-and-Wait on a Feature Branch: You edit .github/workflows/ci.yml, commit, push, and watch the Actions tab. It matches production runners 100%, but a single typo costs 2–5 minutes and clutters your pull request history.
  • Static Linters (such as actionlint): Tools like actionlint check YAML syntax, expression types, and shellcheck rules in under a second. They catch broken syntax early, though they cannot tell you whether your build script actually passes on Linux.
  • Local Execution with nektos/act: act parses your workflow triggers and runs each job inside a local Docker container. You get real runtime output in seconds without touching Git.

2. Pros and Cons of Using nektos/act

Teams that adopt act—especially when onboarding engineers who are new to GitHub Actions YAML—usually cut their CI debugging cycles in half. Local emulation still has trade-offs, though, so know what it can and cannot do.

Pros

  • Seconds Instead of Minutes: Failing steps show up directly in your terminal right away. No more waiting in a remote runner queue.
  • Cleaner Git History: Your branch stays tidy because you only push once the workflow passes locally.
  • Lower CI Costs: GitHub’s free plan caps private repositories at 2,000 Linux minutes per month. Catching broken runs locally keeps you well under that limit.
  • Warm Containers: Reusing containers between runs skips repeated apt installs and package downloads.

Cons

  • Containers Are Not Full VMs: GitHub-hosted runners are full virtual machines, while act uses Docker containers. Jobs that depend on systemd or custom kernel modules may behave differently.
  • Local Disk Footprint: Runner images take real storage space, ranging from ~200 MB to 20 GB depending on the image you pick.
  • Cloud-Only Features: Workflows that rely on GitHub OIDC tokens, repository environments, or protected secrets still need one real GitHub run for final verification.

3. Recommended Setup for Beginners

On your first run, act prompts you to choose a default runner image: Micro, Medium, or Large. Skip Micro—it lacks basics like Node, Python, and curl. Skip Large too unless you want to download an image that unpacks to roughly 18–20 GB.

For most projects, the Medium image (catthehacker/ubuntu:act-latest) hits the sweet spot. It downloads at roughly 500 MB (~1.5 GB on disk) and already ships with Node.js, Python, Docker CLI, and standard build tools.

Drop two helper files into your project root right away, and add both to .gitignore:

  • .actrc — stores default CLI flags so you do not have to retype long options.
  • .secrets — holds dummy or local test tokens in KEY=VALUE format.

4. Step-by-Step Implementation Guide

Let’s wire up a sample workflow and run it locally from scratch.

Step 1: Verify Docker and Install act

First, confirm Docker Desktop or Docker Engine is running:

docker ps

Next, install act. On macOS or Linux with Homebrew, run:

brew install act

Windows users can install it via Winget:

winget install nektos.act

Step 2: Create a Sample Workflow

If your repo does not have a workflow yet, save this Node.js test pipeline to .github/workflows/ci.yml:

name: CI Pipeline
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

Step 3: List and Run Your Workflow Locally

Start by listing the jobs act detects. This doubles as a quick sanity check for your YAML:

act -l

Now run the test job using the Medium runner image:

act -j test -P ubuntu-latest=catthehacker/ubuntu:act-latest

Save that platform mapping inside .actrc so future runs only need act -j test:

-P ubuntu-latest=catthehacker/ubuntu:act-latest

Step 4: Pass Secrets and Event Payloads Safely

Workflows that read ${{ secrets.API_KEY }} can pull values from a local .secrets file (make sure it is in .gitignore):

API_KEY=local-dev-token-123

Pass the file when triggering a push event:

act push --secret-file .secrets

Testing a pull request trigger with custom branch metadata? Put a minimal payload in event.json and pass it with -e:

{
  "pull_request": {
    "head": {
      "ref": "feature/login"
    }
  }
}
act pull_request -e event.json

Step 5: Debug Failing Steps Faster

Two flags cut down waiting time when a step breaks:

  • --reuse (or -r): Keeps the container alive between runs so you do not re-download dependencies every time.
  • -v: Prints verbose logs showing environment variables, container mounts, and exact shell commands.
act -j test --reuse -v

Want to skip Slack alerts or deploy steps while debugging locally? Add if: ${{ !env.ACT }} to those steps. Because act automatically sets ACT=true, GitHub still runs them in CI while your local runs skip right past them.

Once your local run goes green, push your branch for one final check on GitHub’s hosted runner. Your Git history stays clean, and your afternoon stays intact.

Share: