How can I template my Pod definition with the Jenkins kubernetes plugin?

4/2/2019

I am using the Jenkins kubernetes plugin to run pipeline builds:

pipeline {
  agent {
    kubernetes {
      label 'kind'
      defaultContainer 'jnlp'
      yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: dind
... 

I want to template a particular field of the yaml with an integer between 0 and 5 that is rotated in a round robin fashion (i.e. first build is templated with 0, second build templated with 1 etc. and goes back to 0 again after 4).

How can I achieve this?

-- dippynark
jenkins
kubernetes

1 Answer

4/2/2019

You can use podTemplates next code is from https://github.com/jenkinsci/kubernetes-plugin, you can use variables to prepare any kind of pods you need. If this is not what you need, can you provide an example of what you are trying to do?

def label = "mypod-${UUID.randomUUID().toString()}"
podTemplate(label: label, containers: [
  containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
  containerTemplate(name: 'golang', image: 'golang:1.8.0', ttyEnabled: true, command: 'cat')
]) {

node(label) {
    stage('Get a Maven project') {
        git 'https://github.com/jenkinsci/kubernetes-plugin.git'
        container('maven') {
            stage('Build a Maven project') {
                sh 'mvn -B clean install'
            }
        }
    }

    stage('Get a Golang project') {
        git url: 'https://github.com/hashicorp/terraform.git'
        container('golang') {
            stage('Build a Go project') {
                sh """
                mkdir -p /go/src/github.com/hashicorp
                ln -s `pwd` /go/src/github.com/hashicorp/terraform
                cd /go/src/github.com/hashicorp/terraform && make core-dev
                """
            }
        }
    }

}

}

-- wolmi
Source: StackOverflow