You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Docker版GitHub Action自动拉取ghcr.io镜像?

问题描述

我有一个基于Docker的GitHub Action,依赖存储在ghcr.io的基础镜像,该镜像作为GitHub包托管在Action的代码仓库中。

Dockerfile内容如下:

FROM ghcr.io/ourcorp-actions/my-action-image:latest

COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

action.yaml仅引用该Dockerfile:

name: Some Action
author: Our Team
description: Some Description

runs:
  using: docker
  image: Dockerfile

调用该Action时出现401未授权错误:

#0 building with "default" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 167B done
#1 DONE 0.0s

#2 [internal] load .dockerignore
#2 transferring context: 2B done
#2 DONE 0.1s

#3 [internal] load metadata for ghcr.io/ourcorp-actions/my-action-image:latest
#3 ERROR: failed to authorize: failed to fetch anonymous token: unexpected status from GET request to https://ghcr.io/token?scope=repository%3Aourcorp-actions%2Fmy-action-image%3Apull&service=ghcr.io: 401 Unauthorized
------
 > [internal] load metadata for ghcr.io/gdcorp-actions/switchboard-cache-fallback:latest:
------
Dockerfile:1
--------------------
   1 | >>> FROM ghcr.io/gdcorp-actions/switchboard-cache-fallback:latest
   2 |     
   3 |     COPY entrypoint.sh /entrypoint.sh
--------------------
ERROR: failed to solve: failed to fetch anonymous token: unexpected status from GET request to https://ghcr.io/token?scope=repository%3Agdcorp-actions%2Fswitchboard-cache-fallback%3Apull&service=ghcr.io: 401 Unauthorized

核心需求:无需让Action使用者手动登录ghcr.io,Action自动完成私有基础镜像的拉取。

解决方案

方法1:预构建完整Action镜像(推荐)

直接在Action的代码仓库中提前构建好包含基础镜像和entrypoint.sh的完整镜像,推送到ghcr.io,然后修改action.yaml直接引用该预构建镜像:

  1. 本地或通过CI构建完整镜像并推送:
docker build -t ghcr.io/ourcorp-actions/my-action-final:latest .
docker push ghcr.io/ourcorp-actions/my-action-final:latest
  1. 更新action.yaml:
name: Some Action
author: Our Team
description: Some Description

runs:
  using: docker
  image: ghcr.io/ourcorp-actions/my-action-final:latest

GitHub Actions会自动使用运行上下文的GITHUB_TOKEN拉取同组织/仓库下的私有GHCR镜像,使用者无需任何额外配置,同时还能减少Action运行时的构建时间。

方法2:改为复合Action,自动登录GHCR

如果必须保留运行时构建Dockerfile的逻辑,可以将Action改为复合类型,先执行GHCR登录步骤,再构建运行镜像:

修改action.yaml如下:

name: Some Action
author: Our Team
description: Some Description

runs:
  using: composite
  steps:
    - name: 登录GHCR
      uses: docker/login-action@v3
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ github.token }}
    - name: 构建并运行Action镜像
      run: |
        docker build -t my-action-runner .
        docker run my-action-runner
      shell: bash

当使用者调用这个Action时,会自动用GitHub提供的GITHUB_TOKEN完成GHCR登录,后续构建镜像即可正常拉取私有基础镜像。

方法3:将基础镜像设为公开(仅适用于非敏感镜像)

如果基础镜像没有敏感内容,可以在GHCR的镜像设置中开启公开权限,这样无需登录就能匿名拉取镜像,彻底避免授权问题。

注意事项
  • GITHUB_TOKEN默认拥有当前仓库下GHCR包的拉取权限,如果基础镜像属于其他组织/仓库,需要确保该token被授予对应镜像的读取权限。
  • 预构建镜像的方式性能最优,建议优先采用。

内容的提问来源于stack exchange,提问作者Jacob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 23:21:10