# Push to Google Artifact Registry through Jenkins Pipeline

**URL:** <https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868>\
**Category:** Ask a question\
**Created:** [June 7, 2023, 1:51pm UTC](https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868 "2023-06-07T13:51:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rendyhz97](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rendyhz97](https://community.jenkins.io/u/Rendyhz97)\
**Post date:** [June 7, 2023, 1:51pm UTC](https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868/1 "2023-06-07T13:51:26Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![poddingue](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/poddingue/32/985_2.png) [@poddingue](https://community.jenkins.io/u/poddingue)\
**Post date:** [June 8, 2023, 2:17pm UTC](https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868/2 "2023-06-08T14:17:14Z")

</div>

Hello @Rendyhz97 and welcome to this community 👋

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Rendyhz97](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rendyhz97](https://community.jenkins.io/u/Rendyhz97)\
**Post date:** [June 8, 2023, 3:02pm UTC](https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868/3 "2023-06-08T15:02:27Z")

</div>

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](https://stackoverflow.com/questions/45800883/jenkins-pipeline-build-with-docker-google-registry-and-google-auth-plugin)

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](https://github.com/jenkinsci/google-container-registry-auth-plugin)

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

---

<div class="post-metadata">

**Author:** ![poddingue](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/poddingue/32/985_2.png) [@poddingue](https://community.jenkins.io/u/poddingue)\
**Post date:** [June 8, 2023, 3:20pm UTC](https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868/4 "2023-06-08T15:20:35Z")

</div>

Best of luck with that approach. 🤞

---

<div class="post-metadata">

**Author:** ![AmerM](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@AmerM](https://community.jenkins.io/u/AmerM)\
**Post date:** [July 9, 2023, 5:51am UTC](https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868/5 "2023-07-09T05:51:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rendyhz97](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rendyhz97](https://community.jenkins.io/u/Rendyhz97)\
**Post date:** [July 10, 2023, 2:18am UTC](https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868/6 "2023-07-10T02:18:47Z")

</div>

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 :

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/jenkins/original/2X/5/563c77c65c48d06ecfcbbe5a0a4623722a53189c.png)

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

```auto
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 👍

Reference :

> **[Set up authentication for Docker  |  Artifact Registry...](https://cloud.google.com/artifact-registry/docs/docker/authentication#json-key)**

> **[Imagens push e pull  |  Documentação do Artifact Registry...](https://cloud.google.com/artifact-registry/docs/docker/pushing-and-pulling?hl=pt-br)**

---

<div class="post-metadata">

**Author:** ![poddingue](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/poddingue/32/985_2.png) [@poddingue](https://community.jenkins.io/u/poddingue)\
**Post date:** [July 10, 2023, 7:22am UTC](https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868/7 "2023-07-10T07:22:00Z")

</div>

Thanks a lot for your feedback. 🙏

---

<div class="post-metadata">

**Author:** ![AmerM](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@AmerM](https://community.jenkins.io/u/AmerM)\
**Post date:** [July 12, 2023, 2:38am UTC](https://community.jenkins.io/t/push-to-google-artifact-registry-through-jenkins-pipeline/7868/8 "2023-07-12T02:38:52Z")

</div>

> [@Rendyhz97](#):
>
> 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

Thank you so much for the details steps.
