Jenkins Kubernetes Plugin: declarative pipeline and pod template inheritance

11/5/2019

The documentation of the Jenkins Kubernetes Plugin states:

Unlike scripted k8s template, declarative templates do not inherit from parent template. You need to explicitly declare the inheritance if necessary. Plugin Readme

Unfortunately there is no example of how to explicitly state the inheritance from the build's main template. I tried using the label, but then the inheritance seems to be ignored.

def parentLabel = "my-project-${UUID.randomUUID().toString()}"

pipeline {
    agent {
        kubernetes {
            label parentLabel
            yamlFile "jenkins-agent.yml"
            // a global template in the cloud configuration
            inheritFrom "docker"
        }
    }
    stages {
        // .. stages using the above agent
        stage( 'Test Container' ) {
            agent {
                kubernetes {
                    label "xcel-spring-stage-${UUID.randomUUID().toString()}"
                    inheritFrom parentLabel
                    yaml """
                    apiVersion: v1
                    kind: Pod
                    metadata:
                      namespace: build
                      labels:
                        project: x-celerate-spring-application
                    spec:
                      containers:
                        - name: spring-application
                          # defined in previous stages, skipped for brevity
                          image: ${env.IMAGE_NAME}:${version}.${env.BUILD_NUMBER}
                    """
                }
            }
        }
    }
}

How / by what template name can I reference the template declared at the top of the pipeline in an inheritFrom statement in the stage agent declaration to actually define the inheritance explicitly?

-- rainerfrey
jenkins
kubernetes

0 Answers