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

Envrionment section not working when pipeline is inside if statement

XMLWordPrintable

      When trying to have different pipelines based on the branch it was suggested (by Patrick in this thread: https://groups.google.com/forum/#!topic/jenkinsci-users/iXsVIJMgGN0) to put the pipeline inside an if.

      This causes the environment section to be ignored.
      You can see that the withEnv and withCredentials are not invoked.

      Pipeline on its own:

       

      pipeline {
      
          agent none
          environment {
              bar = "bar"
              FOO = credentials('FOO_CREDENTIALS')
          }
      
          stages {
              stage('Feature Build') {
                  agent { label "java-1.8.0_45" }
                  steps {
                      sh 'echo "FOO_USR is $FOO_USR"'
                      sh 'echo "FOO_PSW is $FOO_PSW"'
                  }
              }
          }
      }
      

      Results in the following output (where you can see the withEnv and withCredentials are called)

       

       

      [Pipeline] withEnv
      [Pipeline] {
      [Pipeline] withCredentials
      [Pipeline] {
      [Pipeline] stage
      [Pipeline] { (Feature Build)
      [Pipeline] node
      [Pipeline] {
      [Pipeline] checkout
      ... {snipped out the git checkout output from here}
      [Bitbucket] Notifying commit build result
      [Pipeline] sh
      [le_feature_nio-file-testing-VIDSFO3NITTAFDHZNAVKFW6KXGQWTUPJQNBRSOULGXHLKDYAVTTQ] Running shell script
      + echo 'FOO_USR is ****'
      FOO_USR is ****
      [Pipeline] sh
      [le_feature_nio-file-testing-VIDSFO3NITTAFDHZNAVKFW6KXGQWTUPJQNBRSOULGXHLKDYAVTTQ] Running shell script
      + echo 'FOO_PSW is ****'
      FOO_PSW is ****
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // withCredentials
      [Pipeline] }
      [Pipeline] // withEnv
      [Pipeline] End of Pipeline

       

      Pipeline inside simple if:

       

      if(true) { // this is where my branch logic would happen
          pipeline {
      
              agent none
              environment {
                  bar = "bar"
                  FOO = credentials('FOO_CREDENTIALS')
              }
      
              stages {
                  stage('Feature Build') {
                      agent { label "java-1.8.0_45" }
                      steps {
                          sh 'echo "FOO_USR is $FOO_USR"'
                          sh 'echo "FOO_PSW is $FOO_PSW"'
                      }
                  }
              }
          }
      }
      

       

      Results in the following output

       

      [Pipeline] stage
      [Pipeline] { (Feature Build)
      [Pipeline] node
      [Pipeline] {
      [Pipeline] checkout
      ... {snipped out all the git checkout here}
      [Bitbucket] Notifying commit build result
      [Pipeline] sh
      [le_feature_nio-file-testing-VIDSFO3NITTAFDHZNAVKFW6KXGQWTUPJQNBRSOULGXHLKDYAVTTQ] Running shell script
      + echo 'FOO_USR is '
      FOO_USR is 
      [Pipeline] sh
      [le_feature_nio-file-testing-VIDSFO3NITTAFDHZNAVKFW6KXGQWTUPJQNBRSOULGXHLKDYAVTTQ] Running shell script
      + echo 'FOO_PSW is '
      FOO_PSW is 
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] End of Pipeline

      Jenkins version: 2.32.2
      Credentials Plugin: 2.1.13
      Pipeline Model Definition: 1.1.2

       

       

            abayer Andrew Bayer
            kenneth_s_brooks Kenneth Brooks
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: