0

I wanted to push the docker image from Jenkins(Running on same GCP project) to GCP Artifact Repo. I am using the maven-jib plugin to build and push the image. Maven-jib-plugin supports the container registry(It's Deprecated) but no documentation for the artifact registry.

Could you please help me with how to achieve this?

Gourav
  • 17
  • 6

1 Answers1

0

Based on Jenkins community:

Here's an example of jenkinsfile to push to Artifact Registry:

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'
          }
        }
      }
    }
  }
}

You may refer to the documentations below for your reference:

Hope this helps.

Robert G
  • 1,583
  • 3
  • 13
  • Thanks, @Robert G for the response. But I wanted to use the maven-jib-plugin for building and pushing images as it skip the requirement to install docker on Jenkins and no need to create a docker file also. – Gourav Jul 24 '23 at 08:04
  • @Gourav, based on this [changelog](https://github.com/GoogleContainerTools/jib/blob/master/jib-gradle-plugin/CHANGELOG.md#310), jib now tries [Google ADC](https://developers.google.com/identity/protocols/application-default-credentials), similar with `gcr.io`. – Robert G Jul 24 '23 at 13:43
  • You may also check this documentation on [building Java containers with Jib](https://cloud.google.com/java/getting-started/jib). Hope this helps. – Robert G Jul 24 '23 at 13:45