Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-49700

nested pod template in library doesn't work as documented

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • kubernetes-plugin
    • None
    • kubernetes-plugin v1.3.1
      jenkins: 2.89.4

      If I setup a external library for different pod templates and use them also as documented it doesn't work:

      Here is the library:

      #!groovy
      package myjenkins
      
      public void mavenTemplate(body) {
      	podTemplate(label: label,
      	containers: [
      		containerTemplate(name: 'maven', image: 'maven:3.5.2-jdk-8', command: 'cat', ttyEnabled: true)
      	],
      	volumes: [
      		secretVolume(secretName: 'maven-settings', mountPath: '/root/.m2'),
      		persistentVolumeClaim(claimName: 'maven-local-repo', mountPath: '/root/.m2nrepo')
      	]) { body() }
      }
      
      public void helmTemplate(body) {
      	podTemplate(label: label,
      		containers: [
      			containerTemplate(name: 'helm', image: 'lachlanevenson/k8s-helm:latest', command: 'cat', ttyEnabled: true)
      		],
      		volumes: []) { body() }
      }
      

      and here is the Jenkinsfile:

      @Library('myjenkins')
      import myjenkins.PodTemplates
      
      label = "worker-${UUID.randomUUID().toString()}"
      
      slaveContainers = new PodTemplates();
      slaveContainers.mavenTemplate {
          slaveContainers.helmTemplate {
      
              node(label) {
                  def myRepo = checkout scm
                  def gitCommit = myRepo.GIT_COMMIT
                  def gitBranch = myRepo.GIT_BRANCH
                  def shortGitCommit = "${gitCommit[0..10]}"
                  def previousGitCommit = sh(script: "git rev-parse ${gitCommit}~", returnStdout: true)
          
                  stage('Test') {
                      try {
                          container('maven') { sh """
                              pwd
                              echo "GIT_BRANCH=${gitBranch}" >> /etc/environment
                              echo "GIT_COMMIT=${gitCommit}" >> /etc/environment
                              mvn -B test
                              """ }
                      }
                      catch (exc) {
                          println "Failed to test - ${currentBuild.fullDisplayName}"
                          throw(exc)
                      }
                  }
                  stage('Build') {
                      container('maven') { sh "mvn -B package" }
                  }
                  stage('Run helm') {
                    container('helm') {
                      sh "helm list"
                    }
                  }
              }
          }
      }
      

      This will result in the following error:

      [Pipeline] End of Pipeline
      groovy.lang.MissingPropertyException: No such property: label for class: com.masergy.jenkins.PodTemplates
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
      

      This all works when I define the podTempaltes directly in the Jenkinsfile so I am pretty sure the plugin itself is setup correctly.
      I just don't know enough about Groovy to fix this issue myself so maybe somebody could fix the documentation so that it works as documented?

      Thanks

          [JENKINS-49700] nested pod template in library doesn't work as documented

            askannon Thorsten Kunz
            askannon Thorsten Kunz
            Votes:
            1 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated: