CI/CD Pipelines and Supply-Chain Hardening
skillWrite and debug GitHub Actions and GitLab CI pipelines: triggers, caching, concurrency, OIDC to the cloud, token permissions and action pinning.
agents that use this skill
instructions
Overview
Use this when you write, debug or harden a CI/CD pipeline on GitHub Actions or GitLab CI. The outcome is a pipeline that is fast, deterministic, and cannot hand its credentials to untrusted code. Platform facts below carry an as-of date; read the vendor documentation before relying on one that affects a decision.
Mental model
A pipeline is code that runs with credentials on someone else's trigger. Three questions decide every design choice: who can cause this job to run (the trigger), what can that job reach (token permissions, secrets, cloud role), and what code does it execute (your code, a pull request's code, a third-party action). Hardening means that untrusted triggers never meet powerful credentials, and that every executed dependency is fixed to an exact version.
Rules
Triggers and untrusted input (GitHub Actions)
pull_requestfrom a fork runs with a read-only token and no secrets.pull_request_targetandworkflow_runrun with the base repository's privileges: don't check out or build code from the pull request in them. Split privileged work into a separateworkflow_runworkflow and treat its artifacts as untrusted.- Expression values such as
github.event.pull_request.title, branch names and issue bodies are attacker-controlled. Don't write${{ ... }}of such a value inside arun:script. Pass it throughenv:and use the quoted shell variable, or use an action that takes it as an input. - Self-hosted runners almost never belong on public repositories. Where they are used, make them ephemeral (one job per runner).
Token permissions
- Set the workflow default to read-only, then raise per job:
permissions:
contents: read
jobs:
release:
permissions:
contents: write
id-token: write- GitLab: use job tokens with the narrowest allowlist, protected variables only on protected branches and tags, and masked variables for secrets.
Cloud access without stored keys
- Federate with OpenID Connect (OIDC) instead of long-lived keys. On GitHub, the job needs
id-token: write; the cloud role trusts the issuertoken.actions.githubusercontent.com, the audience (sts.amazonaws.comfor AWS), and asubcondition that names the repository and one ref or environment, for examplerepo:org/repo:environment:prodorrepo:org/repo:ref:refs/heads/main. A trust policy without asubcondition lets any workflow that reaches the issuer assume the role. - On GitLab, declare
id_tokens:with anaud:per target service; the token's claims includeref_protected, so the cloud side can require a protected ref. Useid_tokens, not the deprecatedCI_JOB_JWTvariables. - Apply to production through a protected environment with required reviewers.
Pin what executes
| Item | Rule |
|---|---|
| Third-party actions | Pin to a full commit SHA from the action's own repository (not a fork), with the tag in a trailing comment. Tags can be moved. Dependabot does not alert on SHA-pinned actions, so enable version updates for github-actions. |
| Transitive actions | SHA pinning covers only the top-level reference. GitHub's workflow lockfile (.github/workflows/actions.lock, made with the gh actions-lock extension) records commits for actions and their dependencies; it was announced in public preview on 2026-10-14 and became generally available on 2026-10-28 (GitHub changelog). Check the current docs; GitHub says a lockfile supersedes SHA pinning for repositories that adopt it. |
| Container images | By digest (image@sha256:...), not by tag. |
| Scanners and security tools | By exact version or digest. Trivy's releases and action tags were hijacked in March 2026 (CVE-2026-33634); a tag alone is not a fixed version, so pin the digest or commit. |
| Fresh releases | Delay adoption. Dependabot cooldown skips updates released within N days (default 3 for version updates; security updates are exempt): cooldown: { default-days: 7 }. |
| GitLab includes and components | Pin include:component to a version (@1.2.0) or SHA, include:project to a ref, never main. |
- Don't run
curl | shin a pipeline; download a pinned release and verify its checksum or signature. - Protect
.github/workflows/with CODEOWNERS so workflow changes get review.
Speed and correctness
- GitHub concurrency:
concurrency: { group: ${{ github.workflow }}-${{ github.ref }}, cancel-in-progress: true }for builds; for deploy jobs use a group without cancel so a deploy is never cut in half. - GitLab:
workflow:rulesdecides whether the pipeline exists; jobrulesdecide each job.interruptible: truelets a newer commit cancel a job.needs:starts a job before earlier stages finish.resource_group:serializes deploys. - Cache keys come from the lockfile: GitHub
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}; GitLabcache:key:files: [package-lock.json]. A cache holds dependencies; an artifact holds build output passed between jobs. Setretention-days(GitHub) orexpire_in(GitLab) on artifacts. - Matrix builds: set
fail-fast: falsewhen you need every result; keep the matrix to supported versions. - Reuse: GitHub reusable workflows (
on: workflow_call) and composite actions; GitLab components withspec: inputs:. Pin the caller's reference to a tag or SHA. - Release provenance: build in CI, attach a build attestation or SBOM, sign, and deploy by digest. The workflow identity that publishes needs
id-token: writeplusattestations: writeon GitHub.
Debugging a failed pipeline
| Symptom | First check |
|---|---|
| Works locally, fails in CI | Tool versions, working directory, missing env var, case-sensitive paths, shallow clone (fetch-depth) |
| Job never starts | Trigger filter (branches, paths), if: condition, rules match, runner label or tag |
| Permission denied or 403 on push or API | Token permissions, fork pull request, protected branch rule |
| Cloud auth fails with OIDC | sub claim in the trust policy against the real token, audience, missing id-token: write |
| Cache never hits | Key changes every run, cache scope by branch, key built from a file that is not committed |
| Flaky | Network calls to unpinned resources, shared state between parallel jobs, time-dependent tests |
Read the failing step's log from the first error, not the last line. Reproduce with the same image or runner OS before changing the workflow.
Examples
Bad: uses: some-org/deploy@v3 with permissions unset and run: echo "${{ github.event.issue.title }}".
Good: uses: some-org/deploy@<40-character commit SHA> # v3.1.0, workflow-level permissions: contents: read, and the title passed as env: TITLE: ${{ github.event.issue.title }} then echo "$TITLE".
Checklist
- Every trigger that can run for a fork has no secrets and no write token.
- No attacker-controlled expression is interpolated into a shell script.
- Workflow
permissionsdefault to read-only; each job states what it adds. - Cloud access uses OIDC with a
sub(or protected-ref) condition; no stored cloud keys. - Actions pinned to a SHA or locked; images by digest; tools by version; update cooldown set.
- Deploys are serialized, and production needs approval.
- Caches key off lockfiles; artifacts have an expiry.
- Anything not verified against the live platform documentation is marked unverified.