【问题标题】:Github Actions workflow for pushing a container to Github Container Registry fails with "unauthenticated"用于将容器推送到 Github Container Registry 的 Github Actions 工作流程因“未经身份验证”而失败
【发布时间】:2022-01-25 16:21:20
【问题描述】:

我想构建一个 docker 容器并将其从我的 Github 存储库推送到 Github 容器注册表。也许值得一提的是存储库位于一个组织中。 这是我的 Github Action 工作流程:

name: <name>

on:
  push:
    branches:
      - server

jobs:
  login:
    runs-on: ubuntu-latest
    steps:
      - name: login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: create image from repository
        run: docker build -t ghcr.io/${{ github.actor }}/<img-name> .
      - name: push image to GitHub Container Registry
        run: docker push ghcr.io/${{ github.actor }}/<img-name>:latest

login 通过,但build 失败并出现以下错误:

The push refers to repository [ghcr.io/<user>/<img-name>]
2059ea815744: Preparing
8e3db2b6bb5e: Preparing
5aaeefe84632: Preparing
908e917b0525: Preparing
dff5b20a51e8: Preparing
407c3ac1f7e9: Preparing
e12889c39beb: Preparing
8d3ac3489996: Preparing
e12889c39beb: Waiting
8d3ac3489996: Waiting
407c3ac1f7e9: Waiting
denied: unauthenticated: User cannot be authenticated with the token provided.
Error: Process completed with exit code 1.

我查找了许多解决方案,但没有一个有效。我错过了什么吗?

【问题讨论】:

    标签: docker continuous-integration github-actions github-container-registry


    【解决方案1】:

    问题是 docker 登录在一个工作中,而构建、推送操作在另一个工作中。对于每个作业,都会运行一个单独的 GitHub 运行器,一旦完成,它就会退出。此外,除非由 needs 键指定,否则默认情况下作业被认为是独立的,并且无论如何都会同时运行,因此构建、推送在您的工作流程中顺序运行和登录在不同的 GitHub 运行器上并行进行。要使您的工作流运行,请按如下方式修改代码:

    name: <name>
    
    on:
      push:
        branches:
          - server
    
    jobs:
      dockerloginbuildpush:
        runs-on: ubuntu-latest
        steps:
          - name: login to GitHub Container Registry
            uses: docker/login-action@v1
            with:
              registry: ghcr.io
              username: ${{ github.actor }}
              password: ${{ secrets.GITHUB_TOKEN }}
          - uses: actions/checkout@v2
          - name: create image from repository
            run: docker build -t ghcr.io/${{ github.actor }}/<img-name> .
          - name: push image to GitHub Container Registry
            run: docker push ghcr.io/${{ github.actor }}/<img-name>:latest
    

    所以现在你登录,构建你的镜像并成功推送,因为你已经登录到你正在推送的机器上。

    【讨论】:

    • 谢谢,这不仅有意义而且是正确答案,遗憾的是我在推送时遇到了另一个(不相关的?)错误:denied: permission_denied: The requested installation does not exist.
    • 最后一步写docker push ghcr.io/${{ github.actor }}/&lt;image-name&gt;,不要放latest标签。看看这是否有效。
    • 我找到了解决方法:因为我在一个组织中,所以我将所有 ${{ github.actor }} 替换为 org.名字,它现在可以工作了,谢谢布拉德
    猜你喜欢
    • 2023-02-05
    • 1970-01-01
    • 2016-07-06
    • 2016-10-25
    • 2020-03-26
    • 2011-06-04
    • 2020-01-17
    • 2021-02-19
    • 2020-01-01
    相关资源
    最近更新 更多