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

Environment variables are not exposed in pipeline script

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Minor
    • Resolution: Fixed
    • Component/s: p4-plugin
    • Labels:
      None
    • Similar Issues:

      Description

      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
      

        Attachments

          Issue Links

            Activity

            Hide
            cperthuis 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
            
            Show
            cperthuis 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
            Hide
            allan_burdajewicz 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 ?

            Show
            allan_burdajewicz 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 ?
            Hide
            allan_burdajewicz 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.

            Show
            allan_burdajewicz 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.
            Hide
            wortex17 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.

            Show
            wortex17 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.
            Hide
            allan_burdajewicz Allan BURDAJEWICZ added a comment -

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

            Show
            allan_burdajewicz Allan BURDAJEWICZ added a comment - Paul Allen We could probably raise the priority of this ticket to Major, wdyt ? Also this seems to be the same issue as JENKINS-37584
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Paul Allen
            Path:
            src/main/java/org/jenkinsci/plugins/p4/PerforceScm.java
            src/test/java/org/jenkinsci/plugins/p4/client/WorkflowTest.java
            http://jenkins-ci.org/commit/p4-plugin/0ef5086068dd34198e20839544fe87761d252756
            Log:
            Set SCM Environment for Jenkins 2.60+

            JENKINS-37584
            JENKINS-40885

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Paul Allen Path: src/main/java/org/jenkinsci/plugins/p4/PerforceScm.java src/test/java/org/jenkinsci/plugins/p4/client/WorkflowTest.java http://jenkins-ci.org/commit/p4-plugin/0ef5086068dd34198e20839544fe87761d252756 Log: Set SCM Environment for Jenkins 2.60+ JENKINS-37584 JENKINS-40885
            Hide
            p4paul Paul Allen added a comment -

            Release 1.7.1

            Show
            p4paul Paul Allen added a comment - Release 1.7.1

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: