简体   繁体   English

Github 将容器推送到 Github 容器注册表的操作工作流程失败并显示“未经身份验证”

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

I want to build and push a docker container from my Github repsitory to the Github Container Registry.我想从我的 Github 存储库构建 docker 容器并将其推送到 Github 容器注册表。 Maybe it's worth mentioning that the repository lies in an ogranization.也许值得一提的是存储库位于一个组织中。 Here is my Github Action workflow:这是我的 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

The login passes, but the build fails with the following error: 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.

I looked up many solutions but none worked.我查找了许多解决方案,但没有一个有效。 Am I missing something?我错过了什么吗?

The problem is that the docker login is in one job and the build, push actions are in a different job.问题是 docker 登录在一个工作中,而构建、推送操作在另一个工作中。 For each job, a separate GitHub runner is run and once it is finished, it exits.对于每个作业,运行一个单独的 GitHub 运行器,一旦完成,它就会退出。 Furthermore, unless specified by the needs key, jobs are by default considered independent and are run concurrently anyway so the build, push run sequentially in your workflow and login takes place in parallel on a different GitHub runner.此外,除非needs键指定,否则默认情况下作业被认为是独立的,并且无论如何都会同时运行,因此构建、推送在您的工作流程中按顺序运行并登录在不同的 GitHub 运行器上并行进行。 To make your workflow run, modify the code as follows:要使您的工作流运行,请按如下方式修改代码:

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

So now you login, build your image and push successfully since you are logged in on the machine you are pushing on.所以现在你登录,构建你的镜像并成功推送,因为你已经登录到你正在推送的机器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM