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

Environment variables are not exposed in pipeline script

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • p4-plugin
    • None

      Hi,

      Following p4-plugin exposed variables - I would expect the following code to work:

      node ('somenode')
      {
          stage ('checkout')
          {
              checkout([
                  $class: 'PerforceScm', 
                  credential: '111111111-1111-1111-1111-111111111111', //not actual value
                  populate: [
                      $class: 'ForceCleanImpl', 
                      have: false,
                      parallel: [
                          enable: false,
                          minbytes: '1024',
                          minfiles: '1', 
                          path: '/usr/local/bin/p4',
                          threads: '4'
                          ], 
                      pin: '', 
                      quiet: true
                      ], 
                  workspace: [
                      $class: 'ManualWorkspaceImpl', 
                      charset: 'none', 
                      name: "jenkins-${NODE_NAME}-${JOB_NAME}", 
                      pinHost: false, 
                      spec: [
                          allwrite: false, 
                          clobber: false, 
                          compress: false, 
                          line: 'LOCAL', 
                          locked: false, 
                          modtime: false, 
                          rmdir: false, 
                          streamName: '', 
                          view: "//Depot/... //jenkins-${NODE_NAME}-${JOB_NAME}/..."
                          ]
                      ]
                  ]
              )
          }
          
          println "P4_CHANGELIST=${env.P4_CHANGELIST}"
          println "P4_CLIENT=${env.P4_CLIENT}"
          println "P4_PORT=${env.P4_PORT}"
          println "P4_USER=${env.P4_USER}"
          println "P4_TICKET=${env.P4_TICKET}"
          println "HUDSON_CHANGELOG_FILE=${env.HUDSON_CHANGELOG_FILE}"
          sh 'echo $P4_CHANGELIST'
      }
      

      But all I get are nulls even though the build does have changes (i.e I purge the build history and reset build # to 1 - then run the job manually for the 1st time).

      ...
      P4 Task: syncing files at change: 1703
      ...
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] echo
      P4_CHANGELIST=null
      [Pipeline] echo
      P4_CLIENT=null
      [Pipeline] echo
      P4_PORT=null
      [Pipeline] echo
      P4_USER=null
      [Pipeline] echo
      P4_TICKET=null
      [Pipeline] echo
      HUDSON_CHANGELOG_FILE=null
      [Pipeline] sh
      [UI-CSR-Dev-tag test] Running shell script
      + echo
      
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

          [JENKINS-40885] Environment variables are not exposed in pipeline script

          Mor L created issue -
          Mor L made changes -
          Description Original: Hi,

          Following this [p4-plugin exposed variables|https://github.com/jenkinsci/p4-plugin/blob/master/SETUP.md#exposed-variables] - I would expect the following code to work:


          {code:java}
          node ('somenode')
          {
              stage ('checkout')
              {
                  checkout([
                      $class: 'PerforceScm',
                      credential: '111111111-1111-1111-1111-111111111111', //not actual value
                      populate: [
                          $class: 'ForceCleanImpl',
                          have: false,
                          parallel: [
                              enable: false,
                              minbytes: '1024',
                              minfiles: '1',
                              path: '/usr/local/bin/p4',
                              threads: '4'
                              ],
                          pin: '',
                          quiet: true
                          ],
                      workspace: [
                          $class: 'ManualWorkspaceImpl',
                          charset: 'none',
                          name: "jenkins-${NODE_NAME}-${JOB_NAME}",
                          pinHost: false,
                          spec: [
                              allwrite: false,
                              clobber: false,
                              compress: false,
                              line: 'LOCAL',
                              locked: false,
                              modtime: false,
                              rmdir: false,
                              streamName: '',
                              view: "//Depot/... //jenkins-${NODE_NAME}-${JOB_NAME}/..."
                              ]
                          ]
                      ]
                  )
              }
              
              println "P4_CHANGELIST=${env.P4_CHANGELIST}"
              println "P4_CLIENT=${env.P4_CLIENT}"
              println "P4_PORT=${env.P4_PORT}"
              println "P4_USER=${env.P4_USER}"
              println "P4_TICKET=${env.P4_TICKET}"
              println "HUDSON_CHANGELOG_FILE=${env.HUDSON_CHANGELOG_FILE}"
              sh 'echo $P4_CHANGELIST'
          }
          {code}

          But all I get are nulls even though the build does have changes (i.e I purge the build history and reset build # to 1 - then run the job manually for the 1st time).

          {code:java}
          ...
          P4 Task: syncing files at change: 1703
          ...
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] echo
          P4_CHANGELIST=null
          [Pipeline] echo
          P4_CLIENT=null
          [Pipeline] echo
          P4_PORT=null
          [Pipeline] echo
          P4_USER=null
          [Pipeline] echo
          P4_TICKET=null
          [Pipeline] echo
          HUDSON_CHANGELOG_FILE=null
          [Pipeline] echo
          null
          [Pipeline] sh
          [UI-CSR-Dev-tag test] Running shell script
          + echo

          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          Finished: SUCCESS
          {code}
          New: Hi,

          Following [p4-plugin exposed variables|https://github.com/jenkinsci/p4-plugin/blob/master/SETUP.md#exposed-variables] - I would expect the following code to work:


          {code:java}
          node ('somenode')
          {
              stage ('checkout')
              {
                  checkout([
                      $class: 'PerforceScm',
                      credential: '111111111-1111-1111-1111-111111111111', //not actual value
                      populate: [
                          $class: 'ForceCleanImpl',
                          have: false,
                          parallel: [
                              enable: false,
                              minbytes: '1024',
                              minfiles: '1',
                              path: '/usr/local/bin/p4',
                              threads: '4'
                              ],
                          pin: '',
                          quiet: true
                          ],
                      workspace: [
                          $class: 'ManualWorkspaceImpl',
                          charset: 'none',
                          name: "jenkins-${NODE_NAME}-${JOB_NAME}",
                          pinHost: false,
                          spec: [
                              allwrite: false,
                              clobber: false,
                              compress: false,
                              line: 'LOCAL',
                              locked: false,
                              modtime: false,
                              rmdir: false,
                              streamName: '',
                              view: "//Depot/... //jenkins-${NODE_NAME}-${JOB_NAME}/..."
                              ]
                          ]
                      ]
                  )
              }
              
              println "P4_CHANGELIST=${env.P4_CHANGELIST}"
              println "P4_CLIENT=${env.P4_CLIENT}"
              println "P4_PORT=${env.P4_PORT}"
              println "P4_USER=${env.P4_USER}"
              println "P4_TICKET=${env.P4_TICKET}"
              println "HUDSON_CHANGELOG_FILE=${env.HUDSON_CHANGELOG_FILE}"
              sh 'echo $P4_CHANGELIST'
          }
          {code}

          But all I get are nulls even though the build does have changes (i.e I purge the build history and reset build # to 1 - then run the job manually for the 1st time).

          {code:java}
          ...
          P4 Task: syncing files at change: 1703
          ...
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] echo
          P4_CHANGELIST=null
          [Pipeline] echo
          P4_CLIENT=null
          [Pipeline] echo
          P4_PORT=null
          [Pipeline] echo
          P4_USER=null
          [Pipeline] echo
          P4_TICKET=null
          [Pipeline] echo
          HUDSON_CHANGELOG_FILE=null
          [Pipeline] echo
          null
          [Pipeline] sh
          [UI-CSR-Dev-tag test] Running shell script
          + echo

          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          Finished: SUCCESS
          {code}
          Mor L made changes -
          Description Original: Hi,

          Following [p4-plugin exposed variables|https://github.com/jenkinsci/p4-plugin/blob/master/SETUP.md#exposed-variables] - I would expect the following code to work:


          {code:java}
          node ('somenode')
          {
              stage ('checkout')
              {
                  checkout([
                      $class: 'PerforceScm',
                      credential: '111111111-1111-1111-1111-111111111111', //not actual value
                      populate: [
                          $class: 'ForceCleanImpl',
                          have: false,
                          parallel: [
                              enable: false,
                              minbytes: '1024',
                              minfiles: '1',
                              path: '/usr/local/bin/p4',
                              threads: '4'
                              ],
                          pin: '',
                          quiet: true
                          ],
                      workspace: [
                          $class: 'ManualWorkspaceImpl',
                          charset: 'none',
                          name: "jenkins-${NODE_NAME}-${JOB_NAME}",
                          pinHost: false,
                          spec: [
                              allwrite: false,
                              clobber: false,
                              compress: false,
                              line: 'LOCAL',
                              locked: false,
                              modtime: false,
                              rmdir: false,
                              streamName: '',
                              view: "//Depot/... //jenkins-${NODE_NAME}-${JOB_NAME}/..."
                              ]
                          ]
                      ]
                  )
              }
              
              println "P4_CHANGELIST=${env.P4_CHANGELIST}"
              println "P4_CLIENT=${env.P4_CLIENT}"
              println "P4_PORT=${env.P4_PORT}"
              println "P4_USER=${env.P4_USER}"
              println "P4_TICKET=${env.P4_TICKET}"
              println "HUDSON_CHANGELOG_FILE=${env.HUDSON_CHANGELOG_FILE}"
              sh 'echo $P4_CHANGELIST'
          }
          {code}

          But all I get are nulls even though the build does have changes (i.e I purge the build history and reset build # to 1 - then run the job manually for the 1st time).

          {code:java}
          ...
          P4 Task: syncing files at change: 1703
          ...
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] echo
          P4_CHANGELIST=null
          [Pipeline] echo
          P4_CLIENT=null
          [Pipeline] echo
          P4_PORT=null
          [Pipeline] echo
          P4_USER=null
          [Pipeline] echo
          P4_TICKET=null
          [Pipeline] echo
          HUDSON_CHANGELOG_FILE=null
          [Pipeline] echo
          null
          [Pipeline] sh
          [UI-CSR-Dev-tag test] Running shell script
          + echo

          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          Finished: SUCCESS
          {code}
          New: Hi,

          Following [p4-plugin exposed variables|https://github.com/jenkinsci/p4-plugin/blob/master/SETUP.md#exposed-variables] - I would expect the following code to work:


          {code:java}
          node ('somenode')
          {
              stage ('checkout')
              {
                  checkout([
                      $class: 'PerforceScm',
                      credential: '111111111-1111-1111-1111-111111111111', //not actual value
                      populate: [
                          $class: 'ForceCleanImpl',
                          have: false,
                          parallel: [
                              enable: false,
                              minbytes: '1024',
                              minfiles: '1',
                              path: '/usr/local/bin/p4',
                              threads: '4'
                              ],
                          pin: '',
                          quiet: true
                          ],
                      workspace: [
                          $class: 'ManualWorkspaceImpl',
                          charset: 'none',
                          name: "jenkins-${NODE_NAME}-${JOB_NAME}",
                          pinHost: false,
                          spec: [
                              allwrite: false,
                              clobber: false,
                              compress: false,
                              line: 'LOCAL',
                              locked: false,
                              modtime: false,
                              rmdir: false,
                              streamName: '',
                              view: "//Depot/... //jenkins-${NODE_NAME}-${JOB_NAME}/..."
                              ]
                          ]
                      ]
                  )
              }
              
              println "P4_CHANGELIST=${env.P4_CHANGELIST}"
              println "P4_CLIENT=${env.P4_CLIENT}"
              println "P4_PORT=${env.P4_PORT}"
              println "P4_USER=${env.P4_USER}"
              println "P4_TICKET=${env.P4_TICKET}"
              println "HUDSON_CHANGELOG_FILE=${env.HUDSON_CHANGELOG_FILE}"
              sh 'echo $P4_CHANGELIST'
          }
          {code}

          But all I get are nulls even though the build does have changes (i.e I purge the build history and reset build # to 1 - then run the job manually for the 1st time).

          {code:java}
          ...
          P4 Task: syncing files at change: 1703
          ...
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] echo
          P4_CHANGELIST=null
          [Pipeline] echo
          P4_CLIENT=null
          [Pipeline] echo
          P4_PORT=null
          [Pipeline] echo
          P4_USER=null
          [Pipeline] echo
          P4_TICKET=null
          [Pipeline] echo
          HUDSON_CHANGELOG_FILE=null
          [Pipeline] sh
          [UI-CSR-Dev-tag test] Running shell script
          + echo

          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          Finished: SUCCESS
          {code}

          Cedric Perthuis added a comment - - edited

          I can reproduce the same issue on a windows slave with jenkins 2.3 (https://hub.docker.com/_/jenkins/, 2.32.1-alpine) and P4 Plugin 1.4.12.

          There's a workaround which worked for me as explained in JENKINS-37584.
          To get the workaround to run, you have to run the script several times and whitelist some security exceptions in 'Manage Jenkins > In-process Script Approval'.

                  println "${env.P4_CHANGELIST}" //null
                  println "${env.P4_USER}" //null
                  println "${env.P4_PORT}" //null
                  println "${env.P4_TICKET}" //null
          
                  println currentBuild.getRawBuild().getEnvironment()["P4_CHANGELIST"] //correct
                  println currentBuild.getRawBuild().getEnvironment()["P4_USER"] //correct
                  println currentBuild.getRawBuild().getEnvironment()["P4_PORT"] //correct
                  println currentBuild.getRawBuild().getEnvironment()["P4_TICKET"] //correct
          

          Cedric Perthuis added a comment - - edited I can reproduce the same issue on a windows slave with jenkins 2.3 ( https://hub.docker.com/_/jenkins/ , 2.32.1-alpine) and P4 Plugin 1.4.12. There's a workaround which worked for me as explained in JENKINS-37584 . To get the workaround to run, you have to run the script several times and whitelist some security exceptions in 'Manage Jenkins > In-process Script Approval'. println "${env.P4_CHANGELIST}" // null println "${env.P4_USER}" // null println "${env.P4_PORT}" // null println "${env.P4_TICKET}" // null println currentBuild.getRawBuild().getEnvironment()[ "P4_CHANGELIST" ] //correct println currentBuild.getRawBuild().getEnvironment()[ "P4_USER" ] //correct println currentBuild.getRawBuild().getEnvironment()[ "P4_PORT" ] //correct println currentBuild.getRawBuild().getEnvironment()[ "P4_TICKET" ] //correct

          The latest version of P4 plugin is 1.5.1 and I am not able to reproduce the issue with that version. This seems fixed no ?

          Allan BURDAJEWICZ added a comment - The latest version of P4 plugin is 1.5.1 and I am not able to reproduce the issue with that version. This seems fixed no ?

          Allan BURDAJEWICZ added a comment - - edited

          There is actually a strange behavior - noticed also in P4 1.5.1 - when using `env` before the checkout. If I use the `env` variable to access environment variable in my pipeline anywhere before the checkout step, the P4 variables are null. My simplest scenario:

          node ('master') {
              stage('checkout') {
                      env.JOB_BASE_NAME
                      checkout([$class: 'PerforceScm', credential: 'p4Local', populate: [$class: 'AutoCleanImpl', delete: true, modtime: false, parallel: [enable: false, minbytes: '1024', minfiles: '1', path: '/usr/local/bin/p4', threads: '4'], pin: '', quiet: true, replace: true], workspace: [$class: 'ManualWorkspaceImpl', charset: 'none', name: 'jenkins-test46982', pinHost: false, spec: [allwrite: false, clobber: false, compress: false, line: 'LOCAL', locked: false, modtime: false, rmdir: false, streamName: '', view: '//depot/project2/... //jenkins-test46982/...']]])
              }
              stage ('tests') {
                  println "P4_CHANGELIST=${env.P4_CHANGELIST}"
                  println "P4_CLIENT=${env.P4_CLIENT}"
                  println "P4_PORT=${env.P4_PORT}"
                  println "P4_USER=${env.P4_USER}"
                  println "P4_TICKET=${env.P4_TICKET}"
              }
          }
          

          And get:

          [Pipeline] echo
          P4_CHANGELIST=null
          [Pipeline] echo
          P4_CLIENT=null
          [Pipeline] echo
          P4_PORT=null
          [Pipeline] echo
          P4_USER=null
          [Pipeline] echo
          P4_TICKET=null
          [Pipeline] }
          

          If instead I use env. after the checkout step:

          node ('master') {
              stage('checkout') {
                      checkout([$class: 'PerforceScm', credential: 'p4Local', populate: [$class: 'AutoCleanImpl', delete: true, modtime: false, parallel: [enable: false, minbytes: '1024', minfiles: '1', path: '/usr/local/bin/p4', threads: '4'], pin: '', quiet: true, replace: true], workspace: [$class: 'ManualWorkspaceImpl', charset: 'none', name: 'jenkins-test46982', pinHost: false, spec: [allwrite: false, clobber: false, compress: false, line: 'LOCAL', locked: false, modtime: false, rmdir: false, streamName: '', view: '//depot/project2/... //jenkins-test46982/...']]])
              }
              stage ('tests') {
                  env.JOB_BASE_NAME
                  println "P4_CHANGELIST=${env.P4_CHANGELIST}"
                  println "P4_CLIENT=${env.P4_CLIENT}"
                  println "P4_PORT=${env.P4_PORT}"
                  println "P4_USER=${env.P4_USER}"
                  println "P4_TICKET=${env.P4_TICKET}"
              }
          }
          

          It works:

          [Pipeline] echo
          P4_CHANGELIST=113
          [Pipeline] echo
          P4_CLIENT=jenkins-test46982
          [Pipeline] echo
          P4_PORT=localhost:1666
          [Pipeline] echo
          P4_USER=allan
          [Pipeline] echo
          P4_TICKET=C791B911A018354D527B586F593A395D
          

          ****

          The workaround is to only use groovy variable.

          Allan BURDAJEWICZ added a comment - - edited There is actually a strange behavior - noticed also in P4 1.5.1 - when using `env` before the checkout. If I use the `env` variable to access environment variable in my pipeline anywhere before the checkout step, the P4 variables are null . My simplest scenario: node ( 'master' ) { stage( 'checkout' ) { env.JOB_BASE_NAME checkout([$class: 'PerforceScm' , credential: 'p4Local' , populate: [$class: 'AutoCleanImpl' , delete: true , modtime: false , parallel: [enable: false , minbytes: '1024' , minfiles: '1' , path: '/usr/local/bin/p4' , threads: '4' ], pin: '', quiet: true , replace: true ], workspace: [$class: ' ManualWorkspaceImpl ', charset: ' none ', name: ' jenkins-test46982 ', pinHost: false , spec: [allwrite: false , clobber: false , compress: false , line: ' LOCAL ', locked: false , modtime: false , rmdir: false , streamName: ' ', view: ' //depot/project2/... //jenkins-test46982/...']]]) } stage ( 'tests' ) { println "P4_CHANGELIST=${env.P4_CHANGELIST}" println "P4_CLIENT=${env.P4_CLIENT}" println "P4_PORT=${env.P4_PORT}" println "P4_USER=${env.P4_USER}" println "P4_TICKET=${env.P4_TICKET}" } } And get: [Pipeline] echo P4_CHANGELIST= null [Pipeline] echo P4_CLIENT= null [Pipeline] echo P4_PORT= null [Pipeline] echo P4_USER= null [Pipeline] echo P4_TICKET= null [Pipeline] } If instead I use env. after the checkout step: node ( 'master' ) { stage( 'checkout' ) { checkout([$class: 'PerforceScm' , credential: 'p4Local' , populate: [$class: 'AutoCleanImpl' , delete: true , modtime: false , parallel: [enable: false , minbytes: '1024' , minfiles: '1' , path: '/usr/local/bin/p4' , threads: '4' ], pin: '', quiet: true , replace: true ], workspace: [$class: ' ManualWorkspaceImpl ', charset: ' none ', name: ' jenkins-test46982 ', pinHost: false , spec: [allwrite: false , clobber: false , compress: false , line: ' LOCAL ', locked: false , modtime: false , rmdir: false , streamName: ' ', view: ' //depot/project2/... //jenkins-test46982/...']]]) } stage ( 'tests' ) { env.JOB_BASE_NAME println "P4_CHANGELIST=${env.P4_CHANGELIST}" println "P4_CLIENT=${env.P4_CLIENT}" println "P4_PORT=${env.P4_PORT}" println "P4_USER=${env.P4_USER}" println "P4_TICKET=${env.P4_TICKET}" } } It works: [Pipeline] echo P4_CHANGELIST=113 [Pipeline] echo P4_CLIENT=jenkins-test46982 [Pipeline] echo P4_PORT=localhost:1666 [Pipeline] echo P4_USER=allan [Pipeline] echo P4_TICKET=C791B911A018354D527B586F593A395D **** The workaround is to only use groovy variable.
          Staffan Forsell made changes -
          Link New: This issue is related to JENKINS-37584 [ JENKINS-37584 ]

          Worry Darque added a comment -

          I also experience this. This makes it hard to setup a clean pipeline with perforce involved. I would have like to process arguments and set environment variables BEFORE syncing or use the declarative environment block.
          It doesn't even matter if something is assigned to the environment, it is not allowed to be used at all.

          Worry Darque added a comment - I also experience this. This makes it hard to setup a clean pipeline with perforce involved. I would have like to process arguments and set environment variables BEFORE syncing or use the declarative environment block. It doesn't even matter if something is assigned to the environment, it is not allowed to be used at all.

          p4paul We could probably raise the priority of this ticket to Major, wdyt ? Also this seems to be the same issue as JENKINS-37584

          Allan BURDAJEWICZ added a comment - p4paul We could probably raise the priority of this ticket to Major, wdyt ? Also this seems to be the same issue as JENKINS-37584
          Paul Allen made changes -
          Status Original: Open [ 1 ] New: In Progress [ 3 ]

            Unassigned Unassigned
            pyrocks Mor L
            Votes:
            5 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: