Github 将容器推送到 Github Container Registry 的操作工作流失败并显示 "unauthenticated"

Github Actions workflow for pushing a container to Github Container Registry fails with "unauthenticated"

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

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.

我查找了很多解决方案,但 none 有效。我错过了什么吗?

问题是 docker 登录在一个作业中,而构建、推送操作在另一个作业中。对于每个作业,一个单独的 GitHub 运行ner 是 运行,一旦完成,它就会退出。此外,除非由 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

现在您登录、构建映像并成功推送,因为您已登录到您正在推送的机器上。