Skip to content

Menu

GitHub Actions Cheat Sheet: Triggers, Matrix, Expressions and Security

Checked against GitHub Actions docs.github.com and GitHub changelog as of 28 Nov 2026 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for people who already have a workflow or two and keep looking up the same keys. It follows the GitHub Actions docs and changelog as they stood on 28 Nov 2026, including the new workflow lockfiles and the pull_request_target change. GitHub Actions is a hosted service, so the rows are checked against the docs rather than run here.

When to use what

Pick the trigger by who you trust. pull_request runs fork code without your secrets, which is what you want for CI. pull_request_target runs in the base repository with secrets and write access, so only use it for jobs that never check out the PR's code, such as labelling. workflow_dispatch gives you a manual button, and schedule covers nightly jobs.

Split work into jobs when steps can run on different machines in parallel, and use needs when one job depends on another's output. A matrix is the right tool for the same steps over several versions or operating systems; a reusable workflow (workflow_call) is better when several repositories share a pipeline. The workflow generator drafts a starting file, and our GitHub Actions guide and advanced patterns guide walk through bigger setups.

For security, set permissions at the top of every workflow, starting from contents: read, and widen it per job. Then lock your actions. GitHub's own hardening page now calls a workflow lockfile the strongest option and SHA pinning the fallback; tags are only safe if you trust the author. The security auditor flags unpinned actions and risky triggers, and the expression evaluator helps when an if: doesn't do what you think.

on: { push: { branches: [main] } }

Run on pushes to main; you can't use branches and branches-ignore on the same event.

Try it: github-actions-generatorFrom the docs, not run by us
branches: ['releases/**', '!releases/**-alpha']

Include then exclude with !; a negative pattern needs at least one positive pattern before it.

From the docs, not run by us
schedule: [{ cron: '30 5 * * 1-5', timezone: 'America/New_York' }]

Run at 5:30 local time on weekdays; without timezone cron is UTC.

Try it: Cron ParserFrom the docs, not run by us
every 5 minutes

Shortest allowed schedule interval; scheduled runs use the latest commit on the default branch.

From the docs, not run by us
60 days

In a public repo, scheduled workflows are disabled after 60 days without repository activity.

From the docs, not run by us
workflow_dispatch:

Adds a Run workflow button and API trigger, with optional typed inputs.

From the docs, not run by us
pull_request_target:

Runs with the base repo's secrets and token; never check out and run PR code from it.

From the docs, not run by us
needs: [build, test]

Wait for these jobs to succeed before starting; their outputs become needs.<job>.outputs.

From the docs, not run by us
echo "name=value" >> "$GITHUB_OUTPUT"

Set a step output that a job output can expose to later jobs.

From the docs, not run by us
strategy.matrix: { node: [20, 22], os: [ubuntu-latest, windows-latest] }

Run the job once per combination; a matrix tops out at 256 jobs per workflow run.

From the docs, not run by us
strategy.fail-fast: false

Keep other matrix jobs running when one fails; the default true cancels them.

From the docs, not run by us
strategy.max-parallel: 2

Cap how many matrix jobs run at the same time.

From the docs, not run by us
matrix: ${{ fromJSON(needs.setup.outputs.matrix) }}

Build the matrix dynamically from a JSON string produced by an earlier job.

From the docs, not run by us

Every if: gets an implicit success() unless it already contains a status function.

if: ${{ failure() }}

Run this step only when an earlier step failed; add other conditions with &&.

if: ${{ always() }}

Run even after failure or cancellation, for cleanup and uploading logs.

contains(fromJSON('["push", "pull_request"]'), github.event_name)

Shorter than chaining || comparisons against a list of event names.

contains(github.event.issue.labels.*.name, 'bug')

Object filter .* collects a field from every array item, here label names.

hashFiles('**/package-lock.json')

Hash of matching files, used to build cache keys that change with the lockfile.

From the docs, not run by us
format('{0}-{1}', runner.os, github.ref_name)

Fill numbered placeholders; double braces {{ print a literal brace.

From the docs, not run by us

Token lifetime and recursion rules are on the GITHUB_TOKEN page.

permissions: { contents: read }

Least-privilege default for the whole workflow; any scope you don't list becomes none.

Try it: GitHub Actions Security AuditorFrom the docs, not run by us
permissions: { id-token: write }

Needed to request an OIDC token for cloud login without stored keys.

From the docs, not run by us
permissions: read-all

Read access to every scope; write-all exists too but is rarely what you want.

From the docs, not run by us
${{ secrets.GITHUB_TOKEN }}

Per-job token that expires when the job ends; self-hosted jobs past 24 hours need another credential.

From the docs, not run by us
push with GITHUB_TOKEN

Doesn't start new workflow runs, which prevents loops; PRs it opens create runs that need approval.

From the docs, not run by us
secrets: inherit

Pass all the caller's secrets to a reusable workflow instead of naming each one.

From the docs, not run by us

Concurrency rules come from the concurrency how-to.

concurrency: { group: ${{ github.workflow }}-${{ github.ref }}, cancel-in-progress: true }

One run per workflow and branch; a new push cancels the one in progress.

From the docs, not run by us
concurrency.queue: max

Keep up to 100 pending runs instead of replacing the single pending one.

From the docs, not run by us
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}

Exact-match cache key, max 512 characters; a cache can't be changed once saved.

From the docs, not run by us
restore-keys: | ${{ runner.os }}-npm-

Fallback prefixes tried in order on a miss; the newest partial match is restored.

From the docs, not run by us
10 GB / 7 days

Default repo cache limit, and entries unused for 7 days are evicted; admins can raise the limit.

From the docs, not run by us
on: workflow_call:

Makes a workflow callable from others, with its own inputs and secrets.

From the docs, not run by us
uses: org/repo/.github/workflows/ci.yml@<sha>

Call a reusable workflow at job level; pin it like an action.

From the docs, not run by us
secrets: { token: { required: true } }

Fail the call if the caller doesn't pass the secret, instead of getting an empty string.

From the docs, not run by us
10 levels

Maximum nesting: the caller plus nine reusable workflows, with no loops; permissions can only shrink down the chain.

From the docs, not run by us
gh actions-lock

GitHub CLI extension that writes .github/workflows/actions.lock with exact commits for actions and their dependencies.

Try it: GitHub Actions Security AuditorFrom the docs, not run by us
uses: actions/checkout@<full 40-char SHA>

Pin to a full commit SHA when you can't use a lockfile; check the SHA comes from the real repo, not a fork.

Try it: GitHub Actions Security AuditorFrom the docs, not run by us
env: { TITLE: ${{ github.event.pull_request.title }} }

Pass untrusted input through an env var and use "$TITLE" in the script, so it can't inject shell code.

From the docs, not run by us
workflow_run

Preferred over pull_request_target when a privileged job must follow untrusted CI.

From the docs, not run by us

Worked example: A hardened CI workflow with a matrix, cache and cancel-on-push

Here is a test workflow for a Node project that runs on two Node versions, cancels stale runs on the same branch, and keeps the token read-only:

name: ci
on:
  pull_request:
  push:
    branches: [main]
permissions:
  contents: read
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        node: [20, 22]
    steps:
      - uses: actions/checkout@<full-commit-sha>
      - uses: actions/setup-node@<full-commit-sha>
        with:
          node-version: ${{ matrix.node }}
          cache: npm
      - run: npm ci
      - run: npm test
      - if: ${{ failure() }}
        run: echo "Tests failed on Node ${{ matrix.node }}"

What each part buys you: permissions limits the token to reading code, so a compromised step can't push or open releases. The concurrency group is the workflow name plus the ref, so pushing twice to a branch cancels the older run but leaves other branches alone. fail-fast: false means a failure on Node 20 still lets Node 22 finish, which tells you whether the bug is version-specific. The last step only runs when something before it failed. Replace the SHA placeholders with real commit SHAs from each action's repository, or keep tags and generate a lockfile with gh actions-lock. I haven't run this file; in the Actions tab you should see two test jobs, one per Node version.

Common mistakes

  • Writing a cron for 9 AM local time and getting runs at the wrong hour.

    Cron runs in UTC unless you set timezone with an IANA name such as Europe/Berlin next to the cron line.

  • Adding if: github.ref == 'refs/heads/main' to a cleanup step and seeing it skip after a failure.

    The implicit success() still applies. Write if: ${{ always() && github.ref == 'refs/heads/main' }}.

  • Interpolating ${{ github.event.pull_request.title }} straight into a run: script.

    That's script injection. Put the value in env: and reference the shell variable in double quotes.

  • Using pull_request_target with actions/checkout of the PR head to run tests on forks.

    That hands fork code your secrets. Use pull_request for tests; in public repos the trigger is blocked by default from 2 Dec 2026 anyway.

  • Expecting a commit pushed with GITHUB_TOKEN to start the next workflow.

    Pushes made with that token don't trigger runs. Use workflow_dispatch, workflow_call, or a GitHub App token for chained work.

  • Setting permissions: { pull-requests: write } and then seeing checkout fail.

    Listing one scope sets all others to none. Add contents: read alongside it.

Sources

How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe
Reviews and commentsShow

Comments

Sign in to leave a comment.

Loading comments…