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?
Previously I have successfully pushed images to Google Container Registry through Jenkins pipeline as below:

stage('Build docker image') {
    when { expression { true } }
      steps{
        container('docker'){
          dir('Backend/MobileAPI') {
          echo 'Build docker image Start'
          sh 'pwd'
          sh 'docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .'
          withDockerRegistry([credentialsId: "gcr:${PROJECT}", url: "https://asia.gcr.io"]) {
            sh 'docker push ${IMAGE_NAME}:${IMAGE_TAG}'
          }
          sh 'docker rmi ${IMAGE_NAME}:${IMAGE_TAG}'
          echo 'Build docker image Finish'
          }
        }
      }
    }

But I have searched the internet regarding pushing docker images to the Artifact Registry and haven’t found it.

If anyone can help share an example Jenkins script to push images to the Artifact Registry it would be very helpful.

Thank you very much

Hello @Rendyhz97 and welcome to this community :wave:

Have you tried modifying just slightly your script to push to Google Container Registry?
I think most of your instructions are still valid, right?
You have to find your region, but something like docker push us-central1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1 should work.

withDockerRegistry([credentialsId: "gcr:${PROJECT}", url: "https://LOCATION-docker.pkg.dev/${PROJECT}/${REPOSITORY}"]) {
                    sh 'docker tag ${IMAGE_NAME}:${IMAGE_TAG} LOCATION-docker.pkg.dev/${PROJECT}/${REPOSITORY}/${IMAGE_NAME}:${IMAGE_TAG}'
                    sh 'docker push LOCATION-docker.pkg.dev/${PROJECT}/${REPOSITORY}/${IMAGE_NAME}:${IMAGE_TAG}'
                }

LOCATION : The location of your Artifact Registry repository (e.g., asia , europe , us , etc.).

My $0.02.

Hi @poddingue,

I actually thought of that approach but I was a bit hesitant to do it, because I read in one of the articles that it uses the Google Container Registry Auth plugin.
The Article

I’m not sure if Google Container Registry Auth plugin can also be used for Artifact Registry in the future because there is no complete documentation related to the plugin on github.
Github Documentation

Thank you very much for the suggestion :grin: really appreciate it. I will try it first to see the result.

1 Like

Best of luck with that approach. :crossed_fingers:

Hi @Rendyhz97 , How are you doing. Did the suggestion from @poddingue worked. I’m also in the same boat.
Thanks.

Hi @AmerM and @poddingue.

I tried using the Google Container Registry Auth plugin to push images to the Artifact Registry, and it didn’t work.

Fortunately, I found another way to use Docker Login Authentication and it worked.
I created Service Account for Artifact Registry and grant it Artifact Registry Writer IAM Roles. And then i created Service Account JSON Key. I upload the JSON Key into Jenkins Manage Credentials as Secret File. And then i use it as Credential for connect to My Artifact Registry

Secret File :

Below the example of jenkinsfile for push to Artifact Registry. Hope it will help :pray:.

pipeline {
  environment {
    PROJECT = "you-gcp-project"
    APP_NAME = "you-app-name"
    REPO_NAME = "your-repo-name"
    REPO_LOCATION = "your-repo-location"
    IMAGE_NAME = "${REPO_LOCATION}-docker.pkg.dev/${PROJECT}/${REPO_NAME}/${APP_NAME}"
  }

  agent {
    kubernetes {
      yaml '''
        apiVersion: v1
        kind: Pod
        metadata:
          labels:
            app: test
        spec:
          containers:
          - name: docker
            image: gcr.io/cloud-builders/docker
            command:
            - cat
            tty: true
            volumeMounts:
            - mountPath: /var/run/docker.sock
              name: docker-sock
          - name: kubectl
            image: gcr.io/cloud-builders/kubectl
            command:
            - cat
            tty: true
          volumes:
          - name: docker-sock
            hostPath:
              path: /var/run/docker.sock
      '''
    }
  }
  
  stages {
    stage('Pull Git'){
      when { expression { true } }
      steps{
        checkout scm
      }
    }

    stage('Build docker image') {
    when { expression { true } }
      steps{
        container('docker'){
          dir('Backend Net/MyDotnet') {
            echo 'Build docker image Start'
            sh 'pwd'
            sh 'docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .'
            withCredentials([file(credentialsId: "${PROJECT}_artifacts", variable: 'GCR_CRED')]){
              sh 'cat "${GCR_CRED}" | docker login -u _json_key_base64 --password-stdin https://"${REPO_LOCATION}"-docker.pkg.dev'
              sh 'docker push ${IMAGE_NAME}:${IMAGE_TAG}'
              sh 'docker logout https://"${REPO_LOCATION}"-docker.pkg.dev'
            }
            sh 'docker rmi ${IMAGE_NAME}:${IMAGE_TAG}'
            echo 'Build docker image Finish'
          }
        }
      }
    }
  }
}

Thank You :+1:

Reference :

1 Like

Thanks a lot for your feedback. :pray:

Thank you so much for the details steps.