Lock your GitHub Actions

Lock your GitHub Actions

GitHub Actions 现在支持工作流锁定文件(lockfiles)。由于 GitHub Actions 是从 Git 获取的,之前的建议是锁定到完整的 SHA 而不是标签。这是因为标签是可变的,容易受到供应链攻击,攻击者可能会将之前安全的标签指向恶意代码。

For example, an old workflow looks like: name: Lint on: [push, pull_request, workflow_dispatch] permissions: {} env: FORCE_COLOR: 1 RUFF_OUTPUT_FORMAT: github jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - uses: j178/prek-action@44924fe9ef4a6f88253c4b6b1bf11c689d3ce53d # v3.1.0

例如,一个旧的工作流看起来像这样: name: Lint on: [push, pull_request, workflow_dispatch] permissions: {} env: FORCE_COLOR: 1 RUFF_OUTPUT_FORMAT: github jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - uses: j178/prek-action@44924fe9ef4a6f88253c4b6b1bf11c689d3ce53d # v3.1.0

Here’s how I set up lockfiles.

以下是我设置锁定文件的方法。

Install #Install the gh-actions-lock extension to the gh CLI: $ gh extension install github/gh-actions-lock ✓ Installed extension github/gh-actions-lock

安装 #将 gh-actions-lock 扩展安装到 gh CLI: $ gh extension install github/gh-actions-lock ✓ 已安装扩展 github/gh-actions-lock

Lock ’em #In your repo, run: $ gh actions-lock ✓ All 1 workflows valid

锁定它们 #在你的仓库中运行: $ gh actions-lock ✓ 所有 1 个工作流均有效

The workflow now uses vX.Y.Z tags and looks like this: diff —git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 946971c..3503607 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest steps:

  •  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
  •  - uses: actions/checkout@v7.0.1
       with:
         persist-credentials: false
  •  - uses: j178/prek-action@44924fe9ef4a6f88253c4b6b1bf11c689d3ce53d # v3.1.0
  •  - uses: j178/prek-action@v3.1.0

现在工作流使用 vX.Y.Z 标签,看起来像这样: diff —git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 946971c..3503607 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest steps:

  •  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
  •  - uses: actions/checkout@v7.0.1
       with:
         persist-credentials: false
  •  - uses: j178/prek-action@44924fe9ef4a6f88253c4b6b1bf11c689d3ce53d # v3.1.0
  •  - uses: j178/prek-action@v3.1.0

And it generates a lockfile at .github/workflows/actions.lock:

This file is machine-generated by gh actions-lock.

Do not edit by hand; run gh actions-lock to update.

Docs: https://gh.io/actions-lockfile

version: ‘v0.0.3’ workflows: ‘.github/workflows/lint.yml’: - ‘actions/checkout@v7.0.1’ - ‘j178/prek-action@v3.1.0’ dependencies: ‘actions/checkout@v7.0.1’: ref: ‘v7.0.1’ commit: ‘sha1-3d3c42e5aac5ba805825da76410c181273ba90b1’ owner_id: 44036562 repo_id: 197814629 ‘j178/prek-action@v3.1.0’: ref: ‘v3.1.0’ commit: ‘sha1-44924fe9ef4a6f88253c4b6b1bf11c689d3ce53d’ owner_id: 10510431 repo_id: 1052387486

它会在 .github/workflows/actions.lock 生成一个锁定文件:

此文件由 gh actions-lock 机器生成。

请勿手动编辑;运行 gh actions-lock 进行更新。

文档:https://gh.io/actions-lockfile

version: ‘v0.0.3’ workflows: ‘.github/workflows/lint.yml’: - ‘actions/checkout@v7.0.1’ - ‘j178/prek-action@v3.1.0’ dependencies: ‘actions/checkout@v7.0.1’: ref: ‘v7.0.1’ commit: ‘sha1-3d3c42e5aac5ba805825da76410c181273ba90b1’ owner_id: 44036562 repo_id: 197814629 ‘j178/prek-action@v3.1.0’: ref: ‘v3.1.0’ commit: ‘sha1-44924fe9ef4a6f88253c4b6b1bf11c689d3ce53d’ owner_id: 10510431 repo_id: 1052387486

Commit modified workflows and the new lockfile.

提交修改后的工作流和新的锁定文件。

Renovate and Dependabot #Good news, both already support lockfiles! Your periodic updates will also update the lockfile. Remember to use cooldowns.

Renovate 和 Dependabot #好消息是,两者都已经支持锁定文件!你的定期更新也会更新锁定文件。记得使用冷却时间(cooldowns)。

Validate #GitHub also recommends verifying lockfiles in CI to ensure it’s up-to-date and not corrupt. They suggest copying this verify-lockfile.yml into your repo. I decided to put it into my existing lint workflow, but in its own job so it runs independently and in parallel: name: Lint on: [push, pull_request, workflow_dispatch] permissions: {} env: FORCE_COLOR: 1 RUFF_OUTPUT_FORMAT: github jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7.0.1 with: persist-credentials: false - uses: j178/prek-action@v3.1.0

Fails when .github/workflows/actions.lock is out of sync with the

workflows or a locked pin no longer verifies upstream.

lockfile: runs-on: ubuntu-slim env: GH_TOKEN: ${{ github.token }} steps: - uses: actions/checkout@v7.0.1 with: persist-credentials: false - run: gh extension install github/gh-actions-lock —pin v0.4.1 - run: gh actions-lock —verify

验证 #GitHub 还建议在 CI 中验证锁定文件,以确保其是最新的且未损坏。 他们建议将此 verify-lockfile.yml 复制到你的仓库中。我决定将其放入我现有的 lint 工作流中,但作为一个独立的作业,这样它就可以独立且并行地运行: name: Lint on: [push, pull_request, workflow_dispatch] permissions: {} env: FORCE_COLOR: 1 RUFF_OUTPUT_FORMAT: github jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7.0.1 with: persist-credentials: false - uses: j178/prek-action@v3.1.0

当 .github/workflows/actions.lock 与工作流不同步,

或者锁定的引脚不再验证上游时,此作业会失败。

lockfile: runs-on: ubuntu-slim env: GH_TOKEN: ${{ github.token }} steps: - uses: actions/checkout@v7.0.1 with: persist-credentials: false - run: gh extension install github/gh-actions-lock —pin v0.4.1 - run: gh actions-lock —verify

Note I’m using the lightweight ubuntu-slim runner here. Also, it’s pinning to v0.4.1; we’ll have to update that at some point or figure out a way to automate it. That’s for another time.

注意,我在这里使用了轻量级的 ubuntu-slim 运行器。此外,它被锁定在 v0.4.1 版本;我们以后必须更新它,或者想办法实现自动化。那是以后要考虑的事情了。

Don’t forget that we just modified the actions in a workflow, so need to relock with gh actions-lock. Nothing actually changes if adding to lint.yml because it’s already locking actions/checkout. But if you created a new file like verify-lockfile.yml, you’ll need to relock for that. The CI will let you know.

别忘了我们刚刚修改了工作流中的操作,所以需要使用 gh actions-lock 重新锁定。如果添加到 lint.yml 中,实际上什么都不会改变,因为它已经锁定了 actions/checkout。但如果你创建了一个新文件(如 verify-lockfile.yml),则需要为其重新锁定。CI 会提醒你的。

zizmor #zizmor is a security linter for GitHub Actions and strongly recommended. If you don’t use it, please do. Versions before v1.32.0 tell you to hash-pin actions, and v1.32.0 is the first to support lockfiles. I use zizmor in prek, so add/update zizmor in .pre-commit-config.yaml:

zizmor #zizmor 是一个 GitHub Actions 的安全 linter,强烈推荐使用。如果你还没用,请务必使用。 v1.32.0 之前的版本会要求你对操作进行哈希锁定,而 v1.32.0 是第一个支持锁定文件的版本。 我在 prek 中使用了 zizmor,所以请在 .pre-commit-config.yaml 中添加/更新 zizmor:

Locked! #OK, commit and push!

已锁定! #好了,提交并推送!

Examples #python-pillow/Pillow#10428 python/release-tools#478 python/miss-islington#758 python/cpython#161811

示例 #python-pillow/Pillow#10428 python/release-tools#478 python/miss-islington#758 python/cpython#161811

Header photo: Slaymaker Barry Co. Sprocket Locks in The Bearings (vol. XII no. 23, 2nd January 1896) in the Smithsonian Libraries and Archives, with no known copyright restrictions.

页眉照片:史密森尼图书馆和档案馆收藏的《The Bearings》(第 XII 卷第 23 期,1896 年 1 月 2 日)中的 Slaymaker Barry Co. 链轮锁,无已知版权限制。