java 无法使用github操作推送docker镜像

ozxc1zmp  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(200)

我正在尝试创建一个docker镜像并将其推送到github上。这是我的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,.*/\(.*\),\1,')
          # 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

下面是错误:

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

我已经创建了2个secret,其中一个是dockerhub_username,dockerhub_password,它具有读取,写入和删除权限的访问令牌。我不知道我做错了什么。

sulc1iza

sulc1iza1#

我想出来了!问题是这是错误的登录方式。正确的方法是:

-
        name: Login to 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/

相关问题