How to use shared library - dynamic agent as k8s pod

Hi all,
I’m Jenkins newbie.
My Jenkins installed Kubernetes plugin.
The pipeline below works fine

pipeline {
  agent {
    kubernetes {
      yamlFile 'Jenkins-agent-pod.yaml' // this file existed in my repo
      }
    }
 
  environment {
    DOCKERFILE_PATH = 'Dockerfile' // this file existed in my repo
    DOCKER_IMAGE_NAME = 'dockerhub-repo/app:tag'
  }
 
  stages {
    
    stage('Build and Push Docker Image with Kaniko') {
      steps {
      container(name: 'kaniko', shell: '/busybox/sh') {
        withCredentials([file(credentialsId: 'dockerhub', variable: 'DOCKER_CONFIG_JSON')]) {
          withEnv(['PATH+EXTRA=/busybox']) {
            sh '''#!/busybox/sh
              cp $DOCKER_CONFIG_JSON /kaniko/.docker/config.json
              /kaniko/executor --context `pwd` --dockerfile $DOCKERFILE_PATH --destination $DOCKER_IMAGE_NAME
            '''
            }
          }
        }
      }
    }
  }
}

Now I want to write stage(‘Build and Push Docker Image with Kaniko’) as shared library method
What should I do ?
I create a new repo as jack-shared-library , with vars/buildImage2.groovy
How should I write buildImage2.groovy ?
How should I write pipeline to use buildImage2 ?
Please give me some advice, thank you very much.

1 Like

I make it works
My pipeline

@Library('jack-shared-library') _

def DOCKERHUB = 'dockerhub'
def DOCKERFILE_PATH = 'Dockerfile'
def DOCKER_IMAGE_NAME = 'repo/app:tag'

buildImage2(DOCKERHUB: DOCKERHUB, DOCKERFILE_PATH: DOCKERFILE_PATH, DOCKER_IMAGE_NAME: DOCKER_IMAGE_NAME)

vars/buildImage2.groovy

def call(Map config = [:]) {
  def yamlContent = loadFileYaml(name: 'Jenkins-agent-pod.yaml')
    pipeline {
        agent {
            kubernetes {
                //yaml """${libraryResource('com/jack/yaml/pod.yaml')}"""
                yaml yamlContent
            }
        }

        stages {
            stage('Build and Push Docker Image with Kaniko') {
                steps {
                    container(name: 'kaniko', shell: '/busybox/sh') {
                        withCredentials([file(credentialsId: "${config.DOCKERHUB}", variable: 'DOCKER_CONFIG_JSON')]) {
                            withEnv(['PATH+EXTRA=/busybox', "Dockerfile=${config.DOCKERFILE_PATH}", "DOCKER_IMAGE_NAME=${config.DOCKER_IMAGE_NAME}"]) {
                                sh '''#!/busybox/sh
                                    cp $DOCKER_CONFIG_JSON /kaniko/.docker/config.json
                                    /kaniko/executor --context `pwd` --dockerfile $Dockerfile --destination $DOCKER_IMAGE_NAME
                                '''
                            }
                        }
                    }
                }
            }
        }
    }
}

vars/loadFileYaml.groovy

def call(Map config = [:]) { 
  return libraryResource("com/jack/yaml/${config.name}")   
}

resources/com/jack/yaml/Jenkins-agent-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  labels:
    type: jenkins-agent
spec:
  #securityContext:
  #  runAsUser: 1000
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:v1.17.0-debug
    command:
      - sleep
    args:
      - 99d
  - name: kubectl
    image: bitnami/kubectl:latest
    command:
      - "/bin/sh"
      - "-c"
      - "sleep 99d"
    tty: true
    securityContext:
      runAsUser: 0
  restartPolicy: Never
1 Like

Thank you for the content Jack. I’m Jenkins newbie more then you. First of all I have no experience groovy and SharedLibraries but I am at the phase of learning.
I would like to create “dynamic PVC” for multiple K8s pods and I would like to use Shared Library for this. This dynamic PVC should be “sharable between Pods” And when pipeline finish its job at the end this “dynamic PVC” shoul be destroyed automaticaly. I mean I want to automate this process for every pipeline using with shared libraries.
Also I am using jenkins in Kubernetes, and kubenetes pods as jenkins agents ( with the jenkins-kubernetes plugin.

Thanks in advance for anybody in the community who gives any idea