How to correctly push a Docker image using Github actions

Viewed 2332

I am setting up a Github action to push a Docker image to Docker Hub following Github official README.md for docker/build-push-action@v2.

This is my action inside directory .github/workflows/

name: Publish Docker image

on:
  push:
    branches: master

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    steps:
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: user/app:latest
      -
        name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}

Having tested the action worked in my fork branch I then merged the branch to the main repo. To my surprise the login step failed showing the following error inside Github actions log

Run docker/login-action@v1
  with:
    logout: true
Error: Username and password required

At first I thought I could have wrongly defined (or even not defined) the secrets inside the main repo but after a request with octokit I found out that they are there

{
  total_count: 2,
  secrets: [
    {
      name: 'DOCKER_PASSWORD',
      created_at: '2020-11-04T15:28:55Z',
      updated_at: '2020-11-16T13:11:27Z'
    },
    {
      name: 'DOCKER_USERNAME',
      created_at: '2020-11-04T15:28:55Z',
      updated_at: '2020-11-16T13:11:27Z'
    }
  ]
}

I guess that docker/login-action@v1 is not using username and password provided after the with: keyword. I am very puzzled by this as it did work on my fork branch and do not understand why with: is sending the keyword logout: true instead which I did not set.

Does someone have more insight into this ?

2 Answers

Could you check that in the repository's settings the secrets are there like this:

enter image description here

Also, when you say

Having tested the action worked in my fork branch I then merged the branch to the main repo.

does this mean the main repo is also yours? Because secrets don't transfer between repos. If you've forked another user's repository and want to contribute workflow changes requiring secrets, the main repo's owner(s) will have to add secrets with the same name. Additionally, secrets aren't used in Pull Requests workflow runs. Hence you'll only see if it's working after the changes have been merged into the repository.

I have run into the same issue please check where you are giving your secret, now GitHub has two types of secrets, you should use Actions as shown in the image. If you use depependabot.enter image description here enter image description here enter image description here

Related