无法使用GitHub Actions推送Docker镜像。

huangapple go评论84阅读模式
英文:

unable to push docker image using github actions

问题

这是您的代码部分翻译:

我正在尝试创建一个 Docker 镜像并将其推送到 GitHub。
这是我的 `main.yml` 文件:

name: Docker

on:
push:
branches:
- master

env:

TODO: 更改变量为您的镜像名称。

IMAGE_NAME: java-app

jobs:
push:
runs-on: ubuntu-latest

steps:
  - name: 检出代码
    uses: actions/checkout@v2

  - name: 构建镜像
    run: docker build --file ./docker/Dockerfile --tag ${{ env.IMAGE_NAME }} .

  - name: 登录到注册表
    run: docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} registry.hub.docker.com

  - name: 推送镜像
    run: |
      IMAGE_ID=${{ secrets.DOCKERHUB_USERNAME }}/qfix-test
      # 从版本中去除 git 引用前缀
      VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),,')
      # 去除标签名称中的 "v" 前缀
      [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
      # 使用 Docker 的 'latest' 标签约定
      [ "$VERSION" == "master" ] && VERSION=latest
      echo IMAGE_ID=$IMAGE_ID
      echo VERSION=$VERSION

      docker tag ${{ env.IMAGE_NAME }} $IMAGE_ID:$VERSION
      docker push $IMAGE_ID:$VERSION

这里是错误信息:

推送引用到仓库 [docker.io/***/java-app]
d1eab8b60748: 等待中
未经授权: 访问令牌权限不足
错误: 进程以退出码 1 完成。


我已经创建了两个密钥,一个是 `dockerhub_username`,另一个是 `dockerhub_password`,其中包含具有读、写和删除权限的访问令牌。我不知道我在这里做错了什么。
英文:

I am trying to create a docker image and push it on github.
here's my main.yml

name: Docker

on:
  push:
    branches:
      - master

env:
  # TODO: Change variable to your image's name.
  IMAGE_NAME: java-app

jobs:
  push:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build image
        run: docker build --file ./docker/Dockerfile --tag ${{ env.IMAGE_NAME }} .

      - name: Log into registry
        run: docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} registry.hub.docker.com
  
      - name: Push image
        run: |
          IMAGE_ID=${{ secrets.DOCKERHUB_USERNAME }}/qfix-test
          # Strip git ref prefix from version
          VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),,')
          # Strip "v" prefix from tag name
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
          # Use Docker `latest` tag convention
          [ "$VERSION" == "master" ] && VERSION=latest
          echo IMAGE_ID=$IMAGE_ID
          echo VERSION=$VERSION

          docker tag ${{ env.IMAGE_NAME }} $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION

Here's the error:

The push refers to repository [docker.io/***/java-app]
d1eab8b60748: Waiting
unauthorized: access token has insufficient scopes
Error: Process completed with exit code 1.

I have created 2 secrets one with dockerhub_username, dockerhub_password which has the access token with read, write and delete permissions. I don't know what I am doing wrong here.

答案1

得分: 1

我弄清楚了!
问题是登录方式不正确。
正确的方式是:

   -
        name: 登录到 Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

一旦我用上面的登录方式替换了这个 run: docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} registry.hub.docker.com,就可以正常工作了!

详细信息可以在这里找到:https://docs.docker.com/build/ci/github-actions/

英文:

I figured it out!
The issue was that it was the wrong way to login.
The correct way is:

   -
        name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

As soon as I replaced this run: docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} registry.hub.docker.com with the above login it worked!

Details can be found here: https://docs.docker.com/build/ci/github-actions/

huangapple
  • 本文由 发表于 2023年6月26日 19:09:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定