checkout

其他

actions/checkout

用于检出 GitHub 仓库的 Action。

项目简介

此 Action 将仓库检出到 $GITHUB_WORKSPACE 下,让工作流能访问代码。支持认证、稀疏检出、子模块和多种获取选项。由 GitHub 官方维护。

README 预览

[](https://github.com/actions/checkout/actions/workflows/test.yml)\n\n# Checkout v7\n\n## What's new\n\n- Safer fork pull request handling: checkout now refuses to check out fork pull request code by default when the workflow is triggered by `pull_request_target` or `workflow_run`. These triggers run with the base repository's `GITHUB_TOKEN`, secrets, and runner access, where executing a fork's code commonly leads to "pwn request" vulnerabilities.\n  - To opt in after [reviewing the risks](https://gh.io/securely-using-pull_request_target), set the new `allow-unsafe-pr-checkout: true` input.\n- Migrated `actions/checkout` to ESM to support new versions of the `@actions/*` packages.\n- Updated direct and transitive dependencies, including security fixes for known vulnerabilities.\n\n# Checkout v6\n\n## What's new\n\n- Improved credential security: `persist-credentials` now stores credentials in a separate file under `$RUNNER_TEMP` instead of directly in `.git/config`\n- No workflow changes required — `git fetch`, `git push`, etc. continue to work automatically\n- Running authenticated git commands from a [Docker container action](https://docs.github.com/actions/sharing-automations/creating-actions/creating-a-docker-container-action) requires Actions Runner [v2.329.0](https://github.com/actions/runner/releases/tag/v2.329.0) or later\n\n# Checkout v5\n\n## What's new\n\n- Updated to the node24 runtime\n  - This requires a minimum Actions Runner version of [v2.327.1](https://github.com/actions/runner/releases/tag/v2.327.1) to run.\n\n# Checkout v4\n\nThis action checks-out your repository under `$GITHUB_WORKSPACE`, so your workflow can access it.\n\nOnly a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set `fetch-depth: 0` to fetch all history for all branches and tags. Refer [here](https://docs.github.com/actions/using-workflows/events-that-trigger-workflows) to learn which commit `$GITHUB_SHA` points to for different events.\n\nThe au

常见问题 (5)

故障排除
如何修复 Windows self-hosted runner 上 actions/checkout v7 在清理(cleanup)阶段出现的 "error: invalid pattern" 错误?

这是 actions/checkout v7.0.0 中的一个 bug。在 Windows 上,清理命令会将凭据路径(例如 C:\runner\_work\_temp\...)传递给 git config --unset,而 Git 会将该值中的反斜杠当作正则表达式模式来解释。

升级到 **v7.0.1 或更高版本**,该版本会对传递给 --unset 的值进行转义:

- uses: actions/checkout@v7.0.1

如果你通过 SHA 固定该 action,请使用与 v7.0.1 版本对应的 SHA。

作为临时解决方案,可以固定到已验证可正常工作的 v5.0.1 版本:

- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1

升级后,重新运行工作流。如果持久化的自托管 worktree 中仍残留过时的 includeIf 条目,请检查 .git/config,并移除由 checkout 创建的、指向临时 git-credentials-*.config 文件的 includeIf.gitdir:...path 条目。

来源 Issue #2528
故障排除
如何修复 actions/checkout 使用 URL 编码的代理凭据时出现的代理身份验证失败问题?

将 @actions/http-client 升级到版本 2.2.3(该 bug 已在 2.2.2 中修复)。运行 npm install @actions/http-client@2.2.3,并验证 package-lock.json 引用了更新后的版本。如果您在工作流中使用 actions/checkout,请升级到捆绑了 @actions/http-client 2.2.3 或更高版本的发行版。早期版本会将经过 URL 编码的凭据在未经解码的情况下直接发送到代理,从而导致代理身份验证失败。

来源 Issue #2444
故障排除
如何修复 actions/checkout 的 `assertSafePrCheckout` 在默认 `pull_request_target` 检出时出现的误报问题?

当未显式指定 ref 的同仓库检出在 pull_request_target 事件上运行,且事件 SHA 与 PR 头部 SHA 一致时,就会出现此误报。该问题已在 PR #2518 中修复,此 PR 会跳过对默认检出的不安全 PR 检查。该修复已反向移植到 releases/v2 至 releases/v6;反向移植的强制执行日期已推迟至 2026 年 7 月 20 日(星期一)。使用浮动标签(如 actions/checkout@v6、@v5、@v4、@v3 或 @v2)的工作流将自动获取更新。如果您通过 SHA 固定了 action,请更新到您所使用主版本的已修补提交。actions/checkout@v1 不会获得该反向移植。除非您明确需要该不安全行为并信任所检出的代码,否则请避免启用 allow-unsafe-pr-checkout: true。

来源 Issue #2519
故障排除
如何在 GitHub Actions 中缓存 Git LFS 对象,以避免耗尽 LFS 下载配额?

使用 actions/cache 缓存 Git LFS 的内容寻址对象存储,并在 checkout 期间禁用 LFS 自动下载:

steps:
  - uses: actions/checkout@v4
    with:
      lfs: false

  - name: Restore Git LFS cache
    uses: actions/cache@v4
    with:
      path: .git/lfs/objects
      key: lfs-${{ runner.os }}-${{ github.sha }}
      restore-keys: |
        lfs-${{ runner.os }}-

  - name: Fetch LFS files
    run: |
      git lfs pull
      git lfs checkout

缓存命中时,已有的 LFS 对象已经存在于 .git/lfs/objects 中,因此 git lfs pull 只会下载新增或缺失的对象,而不是全部对象。请勿在此流程中使用 lfs: true,否则 checkout 会在缓存恢复之前就下载 LFS 文件。

如果文件仍为指针文本,请在 git lfs pull 之后运行 git lfs checkout。如需仅拉取仓库的一部分,请使用 git lfs pull --include="path/to/files/*"。

来源 Issue #165
故障排除
如何修复 GitHub Actions 在检出私有仓库时出现的 "repository not found" 错误?

这可能发生在 GitHub 事件期间,此时自动生成的 GITHUB_TOKEN 暂时缺乏访问私有仓库的权限,导致 Git 以退出代码 128 退出。请查看 https://www.githubstatus.com/ 以确认是否有正在进行的事件。如果事件正在进行中,请等待 GitHub 解决该问题,然后重新运行失败的工作流。仓库 URL 和工作流配置可能无需更改。

来源 Issue #254