使用docker buildx github操作缓存,无需官方操作



TL;DR:

如何在GitHub操作中手动查找ACTIONS_RUNTIME_TOKENACTIONS_CACHE_URL

上下文

我正在尝试在GitHub中的buildkit构建过程中缓存docker层。

理论上,docker/setup-buildx-actiondocker/build-push-actioncrazy-max/ghaction-github-runtime作用很容易。问题是,我不能使用它们(组织策略)。

我的工作流程的相关部分现在是:

$repo_url= "<ECR repo in aws>"
docker buildx create --use --driver=docker-container
docker buildx build --tag "${repo_url}:latest"  --file docker/Dockerfile . --cache-to "type=gha,mode=max" --cache-from type=gha

缓存需要2个变量/配置:ACTIONS_RUNTIME_TOKENACTIONS_CACHE_URL。它们将由ghaction-github-runtime设置,因此我无法使用它。查看代码,它似乎从环境中导出了2个变量,但我找不到它们。

如果没有其他操作的帮助,我如何手动找到它们?

这有点恶心,但这是我想出的解决方案:

首先,向工作流添加权限

permissions:
id-token: write # Important for at least docker gha cache
contents: read

这将为您提供环境变量ACTIONS_ID_TOKEN_REQUEST_URLACTIONS_ID_TOKEN_REQUEST_TOKEN

Docker gha缓存需要2个变量:

  • ACTIONS_RUNTIME_TOKEN,实际上是ACTIONS_ID_TOKEN_REQUEST_TOKEN
  • 可以从ACTIONS_ID_TOKEN_REQUEST_URL推断出ACTIONS_CACHE_URL。GitHub变量看起来像https://pipelines.actions.githubusercontent.com/<a long id>/<a lot of things>,docker变量ACTIONS_CACHE_URL应该是https://artifactcache.actions.githubusercontent.com/<the long id from above>/

所以我的最终解决方案是:

export ACTIONS_CACHE_URL=$(echo "$ACTIONS_ID_TOKEN_REQUEST_URL" | grep -Po 'https://[^/]+/[^/]+/' | sed  's/pipelines/artifactcache/')
export ACTIONS_RUNTIME_TOKEN=$ACTIONS_ID_TOKEN_REQUEST_TOKEN
docker buildx build --load --file docker/Dockerfile . --cache-to "type=gha,mode=max" --cache-from type=gha

现在我可以不用外部操作就可以使用缓存了。

最新更新