Lint GitHub Actions locally with actionlint (and a pre-commit hook)
A typo in runs-on or a needs: name only shows up after you push, wait for a runner, and open the Actions tab. actionlint finds those mistakes in milliseconds on your machine. This post shows what it catches, using one deliberately broken workflow, and how to block bad commits with a git hook.
Install
actionlint is a single Go binary. I used the release tarball from its GitHub releases page for linux_amd64. If you want ShellCheck to check your run: scripts as well, install shellcheck and keep it on your PATH: actionlint calls it automatically when it finds it.
The broken workflow
Four mistakes are hiding in this file:
name: CI
on:
pull_request:
types: [opened, synchronize]
jobs:
build:
runs-on: ubuntu-lates
steps:
- uses: actions/checkout@v4
- name: Greet the PR
run: echo "Thanks for ${{ github.event.pull_request.title }}"
- name: Build
run: |
files=$(ls src/*.js)
for f in $files; do
echo $f
done
- name: Use output of a step that does not exist
run: echo "${{ steps.compile.outputs.result }}"
deploy:
needs: biuld
runs-on: ubuntu-latest
steps:
- run: echo deploy
Run it by passing the file:
actionlint .github/workflows/ci.yml
Actual output (the long list of valid labels is shortened):
.github/workflows/ci.yml:8:14: label "ubuntu-lates" is unknown. available labels are "windows-latest", ... [runner-label]
.github/workflows/ci.yml:12:35: "github.event.pull_request.title" is potentially untrusted. avoid using it directly in inline scripts. instead, pass it through an environment variable. ... [expression]
.github/workflows/ci.yml:14:9: shellcheck reported issue in this script: SC2086:info:3:8: Double quote to prevent globbing and word splitting [shellcheck]
.github/workflows/ci.yml:20:24: property "compile" is not defined in object type {} [expression]
.github/workflows/ci.yml:21:3: job "deploy" needs job "biuld" which does not exist in this workflow [job-needs]
The exit code was 1. Each line is file:line:column, a message, and the rule name in brackets.
Fixing each one
The typo and the needs name are plain edits: ubuntu-latest and needs: build.
The script injection warning is the one worth understanding. ${{ github.event.pull_request.title }} is pasted into the shell script before the shell runs, so a pull request title containing quotes and a command becomes part of your script. Pass it through an environment variable instead, so the shell treats it as data:
- name: Greet the PR
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: echo "Thanks for ${PR_TITLE}"
The ShellCheck finding is the unquoted $f. A glob loop does not need the $(ls ...) detour either:
- name: Build
run: |
for f in src/*.js; do
echo "$f"
done
After those edits, actionlint printed nothing and exited 0.
Self-hosted runner labels
If you use a custom label, actionlint reports it as unknown:
runs-on: [self-hosted, gpu-box]
.github/workflows/ci.yml:5:28: label "gpu-box" is unknown. available labels are ...
Declare it in .github/actionlint.yaml:
self-hosted-runner:
labels:
- gpu-box
One detail to know: in my test the config file was only picked up inside a git repository. In a plain folder the same command still complained about gpu-box. After git init it exited 0.
A pre-commit hook that blocks bad workflows
Save this as .git/hooks/pre-commit and make it executable (chmod +x):
#!/usr/bin/env bash
files=$(git diff --cached --name-only --diff-filter=ACM -- '.github/workflows/*.yml' '.github/workflows/*.yaml')
[ -z "$files" ] && exit 0
# shellcheck disable=SC2086
actionlint -no-color $files
I tested both paths. Committing the broken workflow printed the five findings above and git commit exited 1 with nothing committed. Committing the fixed workflow succeeded (1 file changed, 27 insertions(+)).
Hooks in .git/hooks are not shared through the repository. For a team, run the same command in CI too, or use a hook manager so everyone gets it.
What this does not replace
actionlint reads the file. It does not know whether a secret exists, whether an action version is the one you want, or whether your deploy actually works. Treat it as a fast first filter, not as proof.
Reproduce it
The workflows, hook and script are in the evidence folder. ./run.sh regenerates every output shown here.