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

Local Variables not available in declarative matrix pipeline

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      This is the same issue described in this comment. Seemed like it would be better to make a new issue though since the previous issue was in reference to a scripted pipeline.

       

      We have a matrix pipeline which is used from a shared library. Sometime between version 1.7.2 and 1.8.4 of this plugin it broke our ability to reference local variables from our matrix pipeline. Here's a shortened version of our pipeline that no longer works.

      def call(Map<String, Object> pipelineParams) {
          def cronString = pipelineParams.getOrDefault('cronString', "")
          def testTimeoutInMinutes = pipelineParams.getOrDefault('testTimeoutInMinutes', 30)
          // add 2 hours to testTimeoutInMinutes to provide a buffer for getting an executor
          def expandedBuildAndTestTimeoutMinutes = testTimeoutInMinutes + 120
          debugEcho("expandedBuildAndTestTimeoutMinutes=$expandedBuildAndTestTimeoutMinutes")
      
          def office365Options = getTeamsOptions(pipelineParams)
          def jenkinsAgentLabel = pipelineParams.getOrDefault('jenkinsAgentLabel', 'linux_docker')
          def dockerImageName = 'customimagename'
          def dockerMaxMemory = pipelineParams.getOrDefault('dockerMaxMemory', "2g")
          def dockerRunArgs = pipelineParams.getOrDefault('dockerRunArgs', "-m $dockerMaxMemory".toString())
      
          pipeline {
              agent none
              options {
                  office365ConnectorWebhooks([office365Options])
                  timestamps()
                  ansiColor('xterm')
              }
              triggers {
                  cron(cronString)
              }
              stages {
                  stage('Windows Tests') {
                      matrix {
                          axes {
                              axis {
                                  name 'PLATFORM'
                                  values 'WINDOWS_10'
                              }
                              axis {
                                  name 'BROWSER'
                                  values 'CHROME', 'FIREFOX', 'EDGE'
                              }
                          }
                          stages {
                              stage('Execute Tests') {
                                  agent {
                                      docker {
                                          label jenkinsAgentLabel
                                          image dockerImageName
                                          args dockerRunArgs
                                      }
                                  }
                                  options { timeout(time: expandedBuildAndTestTimeoutMinutes, unit: 'MINUTES') }
                                  steps {
                                      echo 'here'
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
      

        Attachments

          Activity

          Hide
          cyoung chris added a comment -

          I'm also reprodcing this error after an update.

          Show
          cyoung chris added a comment - I'm also reprodcing this error after an update.
          Hide
          benbrummer Benjamin Brummer added a comment - - edited

          As workaround i assigned the local variable in an environment-block, and than use this in the matrix-stage

          def call() {
          
          def localVariable = '123'
          
          pipeline {
            environment {
              localVariable = "${localVariable}"
            }
          ...
          }
          }
          

          I was not able to use the workaround with @groovy.transform.Field in the call() => "Annotation @Field can only be used within a Script body."

           

          Another workaround is to remove the "def" for the localVariable

          Show
          benbrummer Benjamin Brummer added a comment - - edited As workaround i assigned the local variable in an environment-block, and than use this in the matrix-stage def call() { def localVariable = '123' pipeline { environment { localVariable = "${localVariable}" } ... } } I was not able to use the workaround with @groovy.transform.Field in the call() => "Annotation @Field can only be used within a Script body."   Another workaround is to remove the "def" for the localVariable
          Hide
          jjathman Joe Athman added a comment - - edited

          Thanks Benjamin Brummer that was very helpful. I wasn't able to use the environment variable trick because I have some values that are not just simple strings/values but just removing def did work for me. 

          Show
          jjathman Joe Athman added a comment - - edited Thanks Benjamin Brummer  that was very helpful. I wasn't able to use the environment variable trick because I have some values that are not just simple strings/values but just removing def  did work for me. 

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            jjathman Joe Athman
            Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated: