Step 2:

ARTIFACTORY: How to use Kaniko to build and deploy docker images through Jenkins available on a kubernetes environment

AuthorFullName__c
Prajyot Pawase
articleNumber
000005271
ft:sourceType
Salesforce
FirstPublishedDate
2022-05-17T10:44:45Z
lastModifiedDate
2024-03-10T07:43:58Z
VersionNumber
3
Create a Jenkins job using the below scripts in the Jenkins, we can modify the scripts as per our requirement.

For this job, we are using a pod-template with a kaniko executor container to build a docker image and a supporting maven container for the git checkout to the Kaniko executor.
pipeline {
    agent {
    kubernetes {
      yaml '''
    apiVersion: v1
    kind: Pod
    spec:
      containers:
      - name: maven
        image: maven:3.8.1-jdk-8
        command:
        - sleep
        args:
        - 99d
      - name: kaniko
        image: gcr.io/kaniko-project/executor:debug
        command:
        - sleep
        args:
        - 9999999
        volumeMounts:
        - name: kaniko-secret
          mountPath: /kaniko/.docker
      restartPolicy: Never
      volumes:
      - name: kaniko-secret
        secret:
            secretName: saas-credentials
            items:
            - key: .dockerconfigjson
              path: config.json
'''
    }
  }
    stages {
        stage ('Clone') {
            steps {container('maven')  {
                git url: 'https://github.com/scriptcamp/kubernetes-kaniko.git', branch: 'main'
            }
        }
        }

        stage ('Exec Kaniko') {
            steps { container('kaniko') {
                sh '''
            cat /kaniko/.docker/config.json
            /kaniko/executor --context `pwd` --destination test-docker.jfrog.io/hello-kaniko --image-name-with-digest-file=image-file
          '''
                    }
                }
        }
            

        stage ('Run create Docker build') {
            steps {
                rtCreateDockerBuild (
                    serverId: 'SERVER_ID',
                    sourceRepo: 'docker',
                    kanikoImageFile: "image-file"
                )
            }
        }

        stage ('Publish build info') {
            steps {
                rtPublishBuildInfo (
                    serverId: 'SERVER_ID'
                )
            }
        }
    }
}