Adapt pipeline to run on the kubernetes agents
The main documentation is here: https://plugins.jenkins.io/kubernetes/#plugin-content-usage
The first thing that you need to understand that the main template of the kubernetes agent pod does not include any sidecar containers. It includes only one jnlp container based on this image https://hub.docker.com/r/jenkins/inbound-agent. To add any sidecar containers to the pod you should create new podTemplate right in the pipeline, inherit it from main template and add all that you need there. See examples below:
podTemplate(containers: [ containerTemplate(name: 'maven', image: 'maven:3.8.1-jdk-8', command: 'sleep', args: '99d'), containerTemplate(name: 'golang', image: 'golang:1.16.5', command: 'sleep', args: '99d') ]) { node(POD_LABEL) { } }
pipeline { agent { kubernetes { yaml ''' apiVersion: v1 kind: Pod metadata: labels: some-label: some-label-value spec: containers: - name: maven image: maven:alpine command: - cat tty: true - name: busybox image: busybox command: - cat tty: true ''' retries 2 } } stages { stage('Run maven') { steps { container('maven') { sh 'mvn -version' } container('busybox') { sh '/bin/busybox' } } } } }
Sidecar containers in this setup are used for running any custom command that we run before in docker container (terraform, cypress, aws, kubectl, etc). They automatically share workspace with the main container so you can use all files from it. To run anything inside one of the sidecar containers you need to wrap it with container directive as shown below:
container('maven') { stage('Build a Maven project') { sh 'mvn -B -ntp clean install' } }