AI Hints
CI/CD with GitHub Actions · Lesson 106 of 138

Pinning Actions

Versions you trust.

⏱ 2 min read▶ Video at 2:03:38🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

When you write uses, actions slash checkout at v four, the v four is a tag. Tags can be moved. If an action's owner is hacked, the attacker can point v four at bad code, and every workflow using it runs that code.

Pinning Actions
From the video: Pinning Actions
  • @v4 is a tag
  • Tags can move
  • A hacked action spreads

Three ways to pin

The first line pins checkout to a full commit SHA, which can never change. The comment says which version it is. The second uses a version tag, which is fine for trusted publishers. The third uses a branch, main, which changes all the time. Avoid that.

.github/workflows/ci.ymlyaml
name: CI
on: push
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - uses: actions/setup-python@v5
      - uses: someone/cool-action@main
      - run: pytest -q

Finding unpinned actions

Grep lists every action used in the workflows. A second grep keeps only those not pinned to a forty character SHA. Two need attention. Tools like Dependabot can update pinned SHAs for you, so pinning doesn't mean falling behind.

Terminal
grep -rhoE "uses: [^ ]+" .github/workflows
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
uses: actions/setup-python@v5
uses: someone/cool-action@main
grep -rhoE "uses: [^ ]+" .github/workflows | grep -vE "@[0-9a-f]{40}$"
uses: actions/setup-python@v5
uses: someone/cool-action@main

Summary

Pin third party actions to a full commit SHA, with a version comment, and let Dependabot update them. Next, a sneaky attack called script injection.

Key takeaways
  • Tags can move
  • Pin to a SHA
  • Dependabot updates them
Next up: Script Injection — untrusted text in run