gha-best-practices
GitHub Actions workflow rules: triggers, draft guards, path filters, concurrency, permissions, secrets, action pinning, timeouts, caching, artifacts, checkout depth, installs, step naming, actionlint,
Files1
View on GitHubSKILL.mdskills/gha-best-practices/SKILL.md
GitHubApply every rule to each workflow file you create or modify. Fix existing violations in the same edit, unasked.
Triggers
Declare explicit pull_request types. Bare pull_request: (implicit opened, synchronize,
reopened) fires on drafts and never on ready_for_review, so a draft-skipped job never runs
when the PR is marked ready.
on:
pull_request:
types: [opened, reopened, synchronize, ready_for_review]
Skip drafts on every pull_request job:
jobs:
build:
if: github.event.pull_request.draft == false
Mixed-trigger workflow → add the escape clause, or the job never runs on push:
if: github.event.pull_request.draft == false || github.event_name != 'pull_request'
Path filters
- Filter
pushandpull_requestwithpaths-ignorefor docs-only changes. pathsandpaths-ignoreare mutually exclusive on one event.- Never filter out a path the build reads: workflow files, lockfiles, build-time imports.
docs/**and**/*.mdare safe. - Always pair path filters with
workflow_dispatch; manual runs ignore filters.
on:
push: ...
pull_request:
types: [opened, reopened, synchronize, ready_for_review]
paths-ignore:
- "**/*.md"
- "docs/**"
workflow_dispatch:
Never make a path-filtered workflow a required status check. A filtered-out workflow never
reports, so the required check stays Pending and blocks merge. (A skipped job reports
Success; a skipped workflow reports nothing.) Pick one:
| Need | Do |
|---|---|
| Required check | No path filter on it |
| Path filter | Check stays optional |
| Both | One unfiltered workflow: a changes job detects paths, the required job gates on its output with if: and reports Success when skipped |
jobs:
changes:
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read
pull-requests: read
outputs:
code: ${{ steps.filter.outputs.code }}
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Detect changed paths
id: filter
uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4.0.3
with:
predicate-quantifier: every
filters: |
code:
- '**'
- '!docs/**'
- '!**/*.md'
build:
needs: changes
if: needs.changes.outputs.code == 'true'
Never use a same-named second workflow with the inverse filter: a PR touching both paths runs both, they share the concurrency group and cancel each other, and required checks match job names, not workflow names.
Concurrency
Every workflow:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Permissions
- Job level, never workflow level. Only what the job uses.
pull-requests: writeonly when the job posts comments or status checks.- Never
write-all. Never omit the block; omission inherits broad defaults.
jobs:
test:
permissions:
contents: read
Secrets
Step-scoped only. Job- or workflow-level env exposes the secret to every step, third-party
actions included.
# WRONG
jobs:
deploy:
env:
API_KEY: ${{ secrets.API_KEY }}
# CORRECT
steps:
- name: Deploy
env:
API_KEY: ${{ secrets.API_KEY }}
run: ./deploy.sh
- Use the default
GITHUB_TOKEN. A PAT or GitHub App token only when the user asks for one. - Never print a secret. Mask runtime-derived sensitive values:
echo "::add-mask::$TOKEN". - Never guard with
if: env.SECRET != ''. Let the step fail; skipping hides a config error behind a green run.
secrets: inherit
Never by default. It passes the callee every repo/org secret, including ones added later.
# WRONG
jobs:
deploy:
uses: ./.github/workflows/deploy.yml
secrets: inherit
# CORRECT
jobs:
deploy:
uses: ./.github/workflows/deploy.yml
secrets:
GCP_SERVICE_ACCOUNT: ${{ secrets.GCP_SERVICE_ACCOUNT }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- Callee declares each secret under
on.workflow_call.secretswithrequired:, so a missing one fails at call time instead of arriving as an empty string. - Never pass
GITHUB_TOKEN; the callee gets its own, scoped by itspermissions. - Prefer OIDC where the provider supports it (Google Cloud, AWS, Azure).
inheritallowed only when caller and callee share a repo and the needed set varies per caller. Justify it inline.
Action pinning
Resolve every version at write time, never from memory or an example:
tag=$(gh api repos/<owner>/<repo>/releases/latest --jq .tag_name)
gh api repos/<owner>/<repo>/commits/$tag --jq .sha
Pin every action, actions/* included, to the full commit SHA plus a version comment. Never a
mutable tag (@latest, @main, @v7): a moved tag runs new code with the job's token and
secrets. Dependabot keeps the SHAs current.
- uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
Timeouts
timeout-minutes on every job; the default is 6 hours. CI tests 10–15 · deploys 10 · Sonar 10.
jobs:
test:
timeout-minutes: 15
Caching
Key off a lockfile hash, never a static string. uv, Playwright, and similar: built-in cache
option, or actions/cache with a lockfile-derived key.
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
cache: 'pnpm'
cache-dependency-path: web/pnpm-lock.yaml
Artifacts
if: always() so reports survive failed tests. Explicit retention-days (default is 90).
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
if: always()
with:
name: test-report
path: coverage/
retention-days: 30
Checkout depth
fetch-depth: 0 only for jobs that read full history: Sonar, release-please, changelog
generation. Build and test jobs keep the shallow default.
Dependency install
Disable lifecycle scripts; they run arbitrary package code on a runner holding secrets.
| Package manager | Command |
|---|---|
| npm | npm ci --ignore-scripts |
| pnpm | pnpm install --frozen-lockfile --ignore-scripts |
| yarn (Berry, v2+) | yarn install --immutable --mode=skip-build |
| yarn (Classic, v1) | yarn install --frozen-lockfile --ignore-scripts |
| bun | bun install --frozen-lockfile --ignore-scripts |
A package that needs its build script (native bindings) gets that one script as its own named step. Never re-enable all scripts.
Step naming
Every step gets a descriptive name:.
Repo tooling
- Repo has workflows → add
actionlint(brew) to the pre-commit hook andmake ai-checks. .github/dependabot.yml:github-actionsplus every package ecosystem in the repo, each withopen-pull-requests-limit: 2unless the user sets another.