Github Actions: Push docker image build by gradle task

2.4k views Asked by At

For building images of my current project, I use the gradle task bootBuildImage. This task creates a OCI image using Cloud Native Buildpacks.

- name: Build image with Gradle
  run: ./gradlew bootBuildImage

With the next step I'm trying to push this docker image to my private GitHub registry using build-push-action.

- name: Push image to Registry
  uses: docker/build-push-action@v1
  with:
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_PASSWORD }}
    registry: docker.pkg.github.com
    repository: sullrich84/wettkampfdb-backend
    tags: latest
  env:
    DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
    DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}

As I can tell from the logs, the problem with this step is that it seems to rely on a Dockerfile located in the workspaces root directory which does not exist.

unable to prepare context: unable to evaluate symlinks in Dockerfile path: 
  lstat /github/workspace/Dockerfile: no such file or directory

Is it possible to push the image created via bootBuildImage to my private GitHub registry without using/creating a dedicated Dockerfile?

3

There are 3 answers

0
Eugen Mayer On

The github-action you are using is not for pushing an image you define by repository and tag but rahter build and push https://github.com/docker/build-push-action#build-push-action

Builds and pushes Docker images and will log in to a Docker registry if required.

Specifically this is also related to https://github.com/docker/build-push-action/issues/17 - so just building without pushing is possible, not vice versa.

This github action does yet not allow just pushing.

This is for now very common for a lot of CI/CD solutions, where build and push are one task.

0
chenrui On

If you are just looking for something to deal with docker push, you can just use the native docker command to do it.

Something like this.

      - name: run docker push
        run: |
          @docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
          @docker push $BUILD_TAG
          @docker push $LATEST_TAG          
        env: 
          DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}  
1
Jay Son On

I use publishRegistry option of gradle bootBuildImage.
Set parameter in your build.gradle (below is gradle.kts)

tasks.bootBuildImage {
    imageName = "${imageName}:${project.version}"
    isPublish = true
    docker {
        publishRegistry {
            url = dockerUrl
            username = dockerUsername
            password = dockerPassword
        }
    }
}

check this document