How to Push to Google Artifact Registry through Jenkins Pipeline

Hi Everyone.

Is there anyone here who can share experiences related to pushing images to Google Artifact Registry with Jenkins Pipeline?

Currently looking to transition form Google Container Registry to Google Artifact Registry.
I was able to copy the images from GCR to AR successfully.

Question
I have build a new project for an image the build gets complied successfully, when it comes to Pushing image. us-docker.pkg.dev/Project-Name/Repository-Name us.gcr.io/image:version

gives me an error as below

**" Error: Build Step failed with exception**

**com.github.docker.java.api.exception.DockerClientException: Could not push image: An image does not exist locally with the tag: us.docker.pkg.dev/Project-Name/Repo-Name us.gcr.io/image"**

Appreciate for help and direction for this issue. I have updated the path in Jenkins for Execute Docker Push command.

Hello @maheshd and welcome to this community. :wave:

The error you’re encountering suggests that the Docker client is unable to find a local image with the tag you specified. This issue can occur if the image was not built or if there is a mismatch between the image tag used during the build and the tag used when attempting to push the image to Google Artifact Registry (AR).

Here are some steps you could take to try and resolve this issue:

  1. Check Docker Build Step: Make sure that the Docker build step in your Jenkins Pipeline is correctly building the image with the desired tag. Verify that the image tag specified in your Jenkinsfile matches the tag you are trying to push to Artifact Registry.Example Jenkinsfile snippet for building and tagging an image:
pipeline {
    agent any
    stages {
        stage('Build and Push Docker Image') {
            steps {
                script {
                    def dockerImageTag = "us-docker.pkg.dev/Project-Name/Repository-Name:latest" // or your desired tag
                    docker.build(dockerImageTag, "-f Dockerfile .")
                    docker.withRegistry('https://us-docker.pkg.dev', 'your-registry-credentials') {
                        docker.image(dockerImageTag).push()
                    }
                }
            }
        }
    }
}
  1. Check Image Name and Tag: Ensure that the image name and tag specified in your Jenkinsfile match exactly with what you intend to use when pushing the image. Any slight variation in the name or tag could result in Docker not finding the image.
  2. Local Image Existence: Verify that a Docker image with the specified tag exists locally on the Jenkins agent where the build is running. You can use the docker images command to list all locally available images.
  3. Authentication: Ensure that you have the necessary authentication and authorization to push images to Google Artifact Registry. Check that your Jenkins credentials (registry username and password) are correctly configured, and that the credentials have the appropriate permissions to push to the Artifact Registry.
  4. Docker Configuration: Make sure that the Docker configuration on your Jenkins agent is correctly set up to use Google Artifact Registry as the container registry. Ensure that Docker is configured to use the same registry URL (e.g., us-docker.pkg.dev) as specified in your Jenkinsfile.
  5. Docker Daemon Restart: In some cases, restarting the Docker daemon on your Jenkins agent can help resolve image-related issues. You can restart Docker with a command like sudo systemctl restart docker.
  6. Artifact Registry Permissions: Ensure that the Google Cloud project associated with your Artifact Registry has the necessary permissions for pushing images. You may need to configure IAM roles to grant permissions for Artifact Registry.

As you may have guessed, I don’t know much about the Docker plugin.
If I were to write such a process from scratch, I would definitely use sh steps with the docker command. :person_shrugging:

Thank you so much for providing me with the detailed steps. Appreciated @poddingue

1 Like