GitHub Actions 下载 artifact 报 curl 22/404 怎么排查
结论
Error: Process completed with exit code 22 并非 GitHub Actions 独有的错误。22 来自 curl:只要用了 -f/--fail,HTTP 响应码大于等于 400(这次是 404)时,curl 就会以 22 退出。脚本启用了 set -euo pipefail,curl 一失败 step 随即失败,所以 GitHub 上看到的就是 Process completed with exit code 22。
curl: (22) The requested URL returned error: 404 说明请求的 URL 返回了 404 Not Found。脚本里会执行两次 curl:第一次列 artifacts,第二次下载 archive_download_url。先确认是哪一次 404,两次的排查方向不一样。
常见原因
- 第一次 curl 就 404:
SOURCE_REPOSITORY或SOURCE_RUN_ID不对,或者 token 看不到对应仓库/run。SOURCE_RUN_ID要用 workflow run 的id,不是 run number。跨仓库下载时,默认的GITHUB_TOKEN只对当前仓库有效,需要换成有源仓库actions:read权限的 PAT 或 GitHub App token。 - 第二次 curl 404:artifact 过期、被删除,不属于该 run,
archive_download_url失效,或者 token 没有下载权限。私有仓库权限不够时,GitHub 往往返回 404,而不是 403。 - 公司 runner 接入的是 GHES 时,API 基址应使用
https://HOSTNAME/api/v3,不要用https://api.github.com;X-GitHub-Api-Version也要用 GHES 支持的版本。 - 公司网络代理或白名单可能改写响应,不过排查时仍应先看 GitHub API 返回的响应体和状态码。
先定位是哪一次 curl
先把 -s 暂时去掉,或者加上 -w '%{http_code}',并在两次 curl 前打印标记:
set -euo pipefail
: "${GH_TOKEN:?GH_TOKEN is required}"
: "${SOURCE_REPOSITORY:?SOURCE_REPOSITORY is required}"
: "${SOURCE_RUN_ID:?SOURCE_RUN_ID is required}"
api_url="https://api.github.com/repos/${SOURCE_REPOSITORY}/actions/runs/${SOURCE_RUN_ID}/artifacts?per_page=100"
printf 'GET %s' "${api_url}"; echo
http_code=$(curl -sS -L -o /tmp/artifacts.json -w '%{http_code}' -H 'Accept: application/vnd.github+json' -H "Authorization: Bearer ${GH_TOKEN}" -H 'X-GitHub-Api-Version: 2022-11-28' "${api_url}")
printf 'artifacts list HTTP %s' "${http_code}"; echo
if [ "${http_code}" != '200' ]; then
printf 'response body:'; echo
cat /tmp/artifacts.json || true
exit 1
fi
artifact_url=$(jq -r --arg name 'deploy-pipelines-last24h' '[.artifacts[] | select(.name == $name and .expired == false) | .archive_download_url] | first // empty' /tmp/artifacts.json)
if [ -z "${artifact_url}" ] || [ "${artifact_url}" = 'null' ]; then
printf 'No usable artifact. Available artifacts:'; echo
jq -r '.artifacts[] | [.name, .expired, .archive_download_url] | @tsv' /tmp/artifacts.json
exit 1
fi
printf 'Downloading %s' "${artifact_url}"; echo
http_code=$(curl -sS -L -o artifact_contents/artifact.zip -w '%{http_code}' -H 'Accept: application/vnd.github+json' -H "Authorization: Bearer ${GH_TOKEN}" -H 'X-GitHub-Api-Version: 2022-11-28' "${artifact_url}")
printf 'artifact download HTTP %s' "${http_code}"; echo
if [ "${http_code}" != '200' ]; then
exit 1
fi
调试时先别用 -f,否则 404 的响应体会被隐藏,只剩 exit 22。用 -w '%{http_code}' 可以看到最终的 HTTP 状态码。
用 gh 快速验证
在 runner 上或本地用同一个 token 跑一遍:
gh auth status
gh api "repos/${SOURCE_REPOSITORY}" --jq '.full_name'
gh api "repos/${SOURCE_REPOSITORY}/actions/runs/${SOURCE_RUN_ID}" --jq '.id, .status, .conclusion'
gh api "repos/${SOURCE_REPOSITORY}/actions/runs/${SOURCE_RUN_ID}/artifacts" --jq '.artifacts[] | {id,name,expired,archive_download_url}'
如果 repos/${SOURCE_REPOSITORY} 就返回 404,先查仓库名、token 权限、SSO 授权和 GHES API 地址。如果 run 接口 404,确认 SOURCE_RUN_ID 是否来自该仓库,以及是否误填了 run number。如果 artifacts 列表返回 200 但没有目标 artifact,检查 artifact 是否过期、名称是否完全一致。
token 权限
- 同仓库:在 workflow 中加上:
permissions:
actions: read
contents: read
然后使用 ${{ secrets.GITHUB_TOKEN }} 或 ${{ github.token }}。
- 跨仓库:默认的
GITHUB_TOKEN访问不了另一个仓库。改用 fine-grained PAT 或 GitHub App token,并给源仓库Actions: Read权限;私有仓库还需要能看到仓库内容。classic PAT 一般需要reposcope。 - 如果公司启用了 SSO,PAT 还要完成 SSO 授权。
其他检查点
SOURCE_RUN_ID必须是 run id,不是GITHUB_RUN_NUMBER。SOURCE_REPOSITORY要是owner/repo,大小写跟实际仓库一致。- artifact 名称必须就是
deploy-pipelines-last24h,且没有过期。 - 不要在 artifact 上传完成前就下载。
- 公司 runner 连的是 GHES 时,把
https://api.github.com换成 GHES 的/api/v3,并使用 GHES 支持的X-GitHub-Api-Version。 - 下载 artifact 时
-L会跟随 302 到存储地址,注意别把 token 打印到日志里。
备注:内容仅供参考。
