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

Can't access Jenkins Global Properties from Pipeline DSL

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • pipeline
    • None
    • workflow-plugin: v1.8
      jenkins: v1.616

      When I try to access the Global Properties environment variables (Manage Jenkins > Configure System > Global Properties) I either get null or an error (details below)

      To replicate issue:

      • Browse to Manage Jenkins > Configure System
      • under the "Global Properties" make sure "Environment variables" is checked
      • under the "Global Properties" > "Environment variables" add a new variable
      • name = TEST_VAR
      • value = some value
      • In left navigation add "New Item"
      • my job was named "workflow-test"
      • select "Workflow" job type
      • under the "Workflow" heading
        • Definition: "Groovy CPS DSL"
        • Script:
      echo "JOB_NAME from env: ${env.JOB_NAME}"
      echo "global property from env: ${env.TEST_VAR}"
      echo "global property: ${TEST_VAR}"
      

      Build workflow job and go to Console output
      from the output can see that the above script evaluates to:

      echo "JOB_NAME from env: ${env.JOB_NAME}" -> "JOB_NAME from env: workflow-test" (worked)
      echo "global property from env: ${env.TEST_VAR}" -> "global property from env: null" (didn't work, but didn't error)
      echo "global property: ${TEST_VAR}" -> ERROR 
      
      groovy.lang.MissingPropertyException: No such property: TEST_VAR for class: WorkflowScript
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:454)
      	at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:25)
      	at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:17)
      	at WorkflowScript.run(WorkflowScript:25)
      
      

          [JENKINS-28921] Can't access Jenkins Global Properties from Pipeline DSL

          Daniel Beck added a comment -

          morgan_goose I would be very surprised if this exact thing ever worked. Provide steps to reproduce.

          Daniel Beck added a comment - morgan_goose I would be very surprised if this exact thing ever worked. Provide steps to reproduce.

          Morgan Goose added a comment - - edited

          In Jenkins 1 setting a global variable would set a global variable for all builds to access. Steps to replicate are the same as jglick provided (which are identical in Jenkins 1.X as 2.X):

          • Browse to Manage Jenkins > Configure System
          • under the "Global Properties" make sure "Environment variables" is checked
          • under the "Global Properties" > "Environment variables" add a new variable
          • name = TEST_VAR
          • value = some value

           

          Which in Jenkins 1.0 world will populate these vars and values in every build, and you can view them even on a per build basis with the link:

           
           

          http://${JENKINS_URL}/job/${JOB_NAME}/${BUILD_NUMBER}/injectedEnvVars/

          Morgan Goose added a comment - - edited In Jenkins 1 setting a global variable would set a global variable for all builds to access. Steps to replicate are the same as jglick provided (which are identical in Jenkins 1.X as 2.X): Browse to Manage Jenkins > Configure System under the "Global Properties" make sure "Environment variables" is checked under the "Global Properties" > "Environment variables" add a new variable name = TEST_VAR value = some value   Which in Jenkins 1.0 world will populate these vars and values in every build, and you can view them even on a per build basis with the link:     http: //${JENKINS_URL}/job/${JOB_NAME}/${BUILD_NUMBER}/injectedEnvVars/

          Daniel Beck added a comment -

          in every build

          Note that this issue is about Pipeline specifically, which is somewhat related to Jenkins 2, in that it's bundled with it, but otherwise independent.

          So if what you're saying is "This worked with freestyle jobs but not Pipeline", that's true, but not a regression.

          If what you're saying is "This worked in older releases of Pipeline (back when it was called "workflow"), but not now", please provide more details.

          Daniel Beck added a comment - in every build Note that this issue is about Pipeline specifically, which is somewhat related to Jenkins 2, in that it's bundled with it, but otherwise independent. So if what you're saying is "This worked with freestyle jobs but not Pipeline", that's true, but not a regression. If what you're saying is "This worked in older releases of Pipeline (back when it was called "workflow"), but not now", please provide more details.

          Morgan Goose added a comment - - edited

          It works in Jenkins 1.628, with versions:

          Name Version
          Pipeline 1.9
          Pipeline: API 1.10
          Pipeline: Basic Steps 1.9
          Pipeline: Groovy 1.9
          Pipeline: Job 1.10
          Pipeline: Nodes and Processes 1.9
          Pipeline: SCM Step 1.9
          Pipeline: Shared Groovy Libraries 1.9
          Pipeline: Step API 1.10
          Pipeline: Supporting APIs 1.10

          And a test job that's of type "workflow" with no configuration other than:

          Groovy CPS DSL
          node {
             sh 'env'
             echo "${env.GLOBAL_PARAM_NAME}"
          }
          

          Will output the env var I've set with the previous instructions.

          What I have presently that it does not work for is Jenkins version 2.60 and

          Plugin Version
          Pipeline 2.5
          Pipeline Graph Analysis Plugin 1.3
          Pipeline REST API for Blue Ocean 1.0.1
          Pipeline: API 2.13
          Pipeline: Basic Steps 2.4
          Pipeline: Build Step 2.5
          Pipeline: Declarative Agent API 1.1.1
          Pipeline: Declarative Extension Points API 1.1.4
          Pipeline: GitHub Groovy Libraries 1.0
          Pipeline: Groovy 2.30
          Pipeline: Input Step 2.7
          Pipeline: Job 2.10
          Pipeline: Milestone Step 1.3.1
          Pipeline: Model API 1.1.4
          Pipeline: Model Definition 1.1.4
          Pipeline: Multibranch 2.14
          Pipeline: Nodes and Processes 2.11
          Pipeline: REST API Plugin 2.6
          Pipeline: SCM Step 2.4
          Pipeline: Shared Groovy Libraries 2.8
          Pipeline: Stage Step 2.2
          Pipeline: Stage Tags Metadata 1.1.4
          Pipeline: Stage View Plugin 2.6
          Pipeline: Step API 2.9
          Pipeline: Supporting APIs 2.14

          Morgan Goose added a comment - - edited It works in Jenkins 1.628, with versions: Name Version Pipeline 1.9 Pipeline: API 1.10 Pipeline: Basic Steps 1.9 Pipeline: Groovy 1.9 Pipeline: Job 1.10 Pipeline: Nodes and Processes 1.9 Pipeline: SCM Step 1.9 Pipeline: Shared Groovy Libraries 1.9 Pipeline: Step API 1.10 Pipeline: Supporting APIs 1.10 And a test job that's of type "workflow" with no configuration other than: Groovy CPS DSL node { sh 'env' echo "${env.GLOBAL_PARAM_NAME}" } Will output the env var I've set with the previous instructions. What I have presently that it does not work for is Jenkins version 2.60 and Plugin Version Pipeline 2.5 Pipeline Graph Analysis Plugin 1.3 Pipeline REST API for Blue Ocean 1.0.1 Pipeline: API 2.13 Pipeline: Basic Steps 2.4 Pipeline: Build Step 2.5 Pipeline: Declarative Agent API 1.1.1 Pipeline: Declarative Extension Points API 1.1.4 Pipeline: GitHub Groovy Libraries 1.0 Pipeline: Groovy 2.30 Pipeline: Input Step 2.7 Pipeline: Job 2.10 Pipeline: Milestone Step 1.3.1 Pipeline: Model API 1.1.4 Pipeline: Model Definition 1.1.4 Pipeline: Multibranch 2.14 Pipeline: Nodes and Processes 2.11 Pipeline: REST API Plugin 2.6 Pipeline: SCM Step 2.4 Pipeline: Shared Groovy Libraries 2.8 Pipeline: Stage Step 2.2 Pipeline: Stage Tags Metadata 1.1.4 Pipeline: Stage View Plugin 2.6 Pipeline: Step API 2.9 Pipeline: Supporting APIs 2.14

          Morgan Goose added a comment -

          Do you need any more information from me to verify this bug/regression danielbeck? I'm happy and available to assist. I can also chat more realtime in the #jenkins irc channel if that's preferable.

          Morgan Goose added a comment - Do you need any more information from me to verify this bug/regression danielbeck ? I'm happy and available to assist. I can also chat more realtime in the #jenkins irc channel if that's preferable.

          Daniel Beck added a comment - - edited

          morgan_goose Pinged you (or rather goosemo assuming that's you), but we may not have great time zone overlap. Still, respond when you can, maybe I'm around (typically UTC noon to evening).

          Regarding plugins, is Environment Injector installed in either of the instances, but perhaps not (in the same version) on the other? What happens when you install it where it's missing / remove it where it's present?

          Daniel Beck added a comment - - edited morgan_goose Pinged you (or rather goosemo assuming that's you), but we may not have great time zone overlap. Still, respond when you can, maybe I'm around (typically UTC noon to evening). Regarding plugins, is Environment Injector installed in either of the instances, but perhaps not (in the same version) on the other? What happens when you install it where it's missing / remove it where it's present?

          Morgan Goose added a comment -

          It's installed on both, but note that I didn't have to check the box to inject vars in Jenkins 1.x land. Versions are:

          Jenkins Version Plugin Version
          1.628 Environment Injector Plugin 2.1
          2.60 Environment Injector Plugin 2.1

          Morgan Goose added a comment - It's installed on both, but note that I didn't have to check the box to inject vars in Jenkins 1.x land. Versions are: Jenkins Version Plugin Version 1.628 Environment Injector Plugin 2.1 2.60 Environment Injector Plugin 2.1

          Morgan Goose added a comment -
          Jenkins 1.628 Test Job Config
          <?xml version='1.0' encoding='UTF-8'?>
          <flow-definition plugin="workflow-job@1.10">
            <actions/>
            <description></description>
            <keepDependencies>false</keepDependencies>
            <properties>
              <org.datadog.jenkins.plugins.datadog.DatadogJobProperty plugin="datadog@0.5.2">
                <emitOnCheckout>false</emitOnCheckout>
              </org.datadog.jenkins.plugins.datadog.DatadogJobProperty>
              <com.sonyericsson.rebuild.RebuildSettings plugin="rebuild@1.25">
                <autoRebuild>false</autoRebuild>
                <rebuildDisabled>false</rebuildDisabled>
              </com.sonyericsson.rebuild.RebuildSettings>
              <jenkins.plugins.slack.SlackNotifier_-SlackJobProperty plugin="slack@1.8">
                <teamDomain></teamDomain>
                <token></token>
                <room></room>
                <startNotification>false</startNotification>
                <notifySuccess>false</notifySuccess>
                <notifyAborted>false</notifyAborted>
                <notifyNotBuilt>false</notifyNotBuilt>
                <notifyUnstable>false</notifyUnstable>
                <notifyFailure>false</notifyFailure>
                <notifyBackToNormal>false</notifyBackToNormal>
                <notifyRepeatedFailure>false</notifyRepeatedFailure>
                <includeTestSummary>false</includeTestSummary>
                <showCommitList>false</showCommitList>
                <includeCustomMessage>false</includeCustomMessage>
                <customMessage></customMessage>
              </jenkins.plugins.slack.SlackNotifier_-SlackJobProperty>
            </properties>
            <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@1.9">
              <script>node { sh 'env' echo "${env.GITHUB_OAUTH_TOKEN}" }</script>
              <sandbox>false</sandbox>
            </definition>
            <triggers/>
            <concurrentBuild>false</concurrentBuild>
          </flow-definition>
          
          Jenkins 2.60 Test Job Config
          <?xml version='1.0' encoding='UTF-8'?>
          <flow-definition plugin="workflow-job@2.10">
            <actions/>
            <keepDependencies>false</keepDependencies>
            <properties>
              <org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty plugin="workflow-multibranch@2.14">
                <branch plugin="branch-api@2.0.9">
                  <sourceId>org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator::https://api.github.com::MyORG::test-jenkins-jira</sourceId>
                  <head class="org.jenkinsci.plugins.github_branch_source.BranchSCMHead" plugin="github-branch-source@2.0.5">
                    <name>success</name>
                  </head>
                  <scm class="hudson.plugins.git.GitSCM" plugin="git@3.3.0">
                    <configVersion>2</configVersion>
                    <userRemoteConfigs>
                      <hudson.plugins.git.UserRemoteConfig>
                        <name>origin</name>
                        <refspec>+refs/heads/*:refs/remotes/origin/*</refspec>
                        <url>https://github.com/MyORG/test-jenkins-jira.git</url>
                        <credentialsId>MyORG-jenkins oauth</credentialsId>
                      </hudson.plugins.git.UserRemoteConfig>
                      <hudson.plugins.git.UserRemoteConfig>
                        <name>origin</name>
                        <refspec>+refs/pull/*/head:refs/remotes/origin/pr/*</refspec>
                        <url>https://github.com/MyORG/test-jenkins-jira.git</url>
                        <credentialsId>MyORG-jenkins oauth</credentialsId>
                      </hudson.plugins.git.UserRemoteConfig>
                    </userRemoteConfigs>
                    <branches class="singleton-list">
                      <hudson.plugins.git.BranchSpec>
                        <name>success</name>
                      </hudson.plugins.git.BranchSpec>
                    </branches>
                    <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
                    <browser class="hudson.plugins.git.browser.GithubWeb">
                      <url>https://github.com/MyORG/test-jenkins-jira</url>
                    </browser>
                    <submoduleCfg class="empty-list"/>
                    <extensions/>
                  </scm>
                  <properties/>
                  <actions>
                    <org.jenkinsci.plugins.github__branch__source.GitHubLink plugin="github-branch-source@2.0.5">
                      <iconClassName>icon-github-branch</iconClassName>
                      <url>https://github.com/MyORG/test-jenkins-jira/tree/success</url>
                    </org.jenkinsci.plugins.github__branch__source.GitHubLink>
                    <jenkins.scm.api.metadata.ObjectMetadataAction plugin="scm-api@2.1.1">
                      <objectDisplayName>success</objectDisplayName>
                      <objectUrl>https://github.com/MyORG/test-jenkins-jira/tree/success</objectUrl>
                    </jenkins.scm.api.metadata.ObjectMetadataAction>
                  </actions>
                </branch>
              </org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty>
            </properties>
            <definition class="org.jenkinsci.plugins.workflow.multibranch.SCMBinder" plugin="workflow-multibranch@2.14"/>
            <triggers/>
          </flow-definition>
          

          Morgan Goose added a comment - Jenkins 1.628 Test Job Config <?xml version= '1.0' encoding= 'UTF-8' ?> <flow-definition plugin= "workflow-job@1.10" > <actions/> <description></description> <keepDependencies> false </keepDependencies> <properties> <org.datadog.jenkins.plugins.datadog.DatadogJobProperty plugin= "datadog@0.5.2" > <emitOnCheckout> false </emitOnCheckout> </org.datadog.jenkins.plugins.datadog.DatadogJobProperty> <com.sonyericsson.rebuild.RebuildSettings plugin= "rebuild@1.25" > <autoRebuild> false </autoRebuild> <rebuildDisabled> false </rebuildDisabled> </com.sonyericsson.rebuild.RebuildSettings> <jenkins.plugins.slack.SlackNotifier_-SlackJobProperty plugin= "slack@1.8" > <teamDomain></teamDomain> <token></token> <room></room> <startNotification> false </startNotification> <notifySuccess> false </notifySuccess> <notifyAborted> false </notifyAborted> <notifyNotBuilt> false </notifyNotBuilt> <notifyUnstable> false </notifyUnstable> <notifyFailure> false </notifyFailure> <notifyBackToNormal> false </notifyBackToNormal> <notifyRepeatedFailure> false </notifyRepeatedFailure> <includeTestSummary> false </includeTestSummary> <showCommitList> false </showCommitList> <includeCustomMessage> false </includeCustomMessage> <customMessage></customMessage> </jenkins.plugins.slack.SlackNotifier_-SlackJobProperty> </properties> <definition class= "org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin= "workflow-cps@1.9" > <script>node { sh 'env' echo "${env.GITHUB_OAUTH_TOKEN}" }</script> <sandbox> false </sandbox> </definition> <triggers/> <concurrentBuild> false </concurrentBuild> </flow-definition> Jenkins 2.60 Test Job Config <?xml version= '1.0' encoding= 'UTF-8' ?> <flow-definition plugin= "workflow-job@2.10" > <actions/> <keepDependencies> false </keepDependencies> <properties> <org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty plugin= "workflow-multibranch@2.14" > <branch plugin= "branch-api@2.0.9" > <sourceId>org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator::https: //api.github.com::MyORG::test-jenkins-jira</sourceId> <head class= "org.jenkinsci.plugins.github_branch_source.BranchSCMHead" plugin= "github-branch-source@2.0.5" > <name>success</name> </head> <scm class= "hudson.plugins.git.GitSCM" plugin= "git@3.3.0" > <configVersion>2</configVersion> <userRemoteConfigs> <hudson.plugins.git.UserRemoteConfig> <name>origin</name> <refspec>+refs/heads/*:refs/remotes/origin/*</refspec> <url>https: //github.com/MyORG/test-jenkins-jira.git</url> <credentialsId>MyORG-jenkins oauth</credentialsId> </hudson.plugins.git.UserRemoteConfig> <hudson.plugins.git.UserRemoteConfig> <name>origin</name> <refspec>+refs/pull/*/head:refs/remotes/origin/pr/*</refspec> <url>https: //github.com/MyORG/test-jenkins-jira.git</url> <credentialsId>MyORG-jenkins oauth</credentialsId> </hudson.plugins.git.UserRemoteConfig> </userRemoteConfigs> <branches class= "singleton-list" > <hudson.plugins.git.BranchSpec> <name>success</name> </hudson.plugins.git.BranchSpec> </branches> <doGenerateSubmoduleConfigurations> false </doGenerateSubmoduleConfigurations> <browser class= "hudson.plugins.git.browser.GithubWeb" > <url>https: //github.com/MyORG/test-jenkins-jira</url> </browser> <submoduleCfg class= "empty-list" /> <extensions/> </scm> <properties/> <actions> <org.jenkinsci.plugins.github__branch__source.GitHubLink plugin= "github-branch-source@2.0.5" > <iconClassName>icon-github-branch</iconClassName> <url>https: //github.com/MyORG/test-jenkins-jira/tree/success</url> </org.jenkinsci.plugins.github__branch__source.GitHubLink> <jenkins.scm.api.metadata.ObjectMetadataAction plugin= "scm-api@2.1.1" > <objectDisplayName>success</objectDisplayName> <objectUrl>https: //github.com/MyORG/test-jenkins-jira/tree/success</objectUrl> </jenkins.scm.api.metadata.ObjectMetadataAction> </actions> </branch> </org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty> </properties> <definition class= "org.jenkinsci.plugins.workflow.multibranch.SCMBinder" plugin= "workflow-multibranch@2.14" /> <triggers/> </flow-definition>

          Morgan Goose added a comment -

          Ok so I followed danielbeck's advice in IRC to try and "bisect" via plugin downgrades. I ran a jar of the service and setup an Org to mimic my job setup as close as possible, starting with all the same versions of plugins, and then also made the global param env variables.

          It worked first time w/o issue :|

          So instead of a bug in the code pushing these vars, it instead seems that there is a half-write cache sorta issue that's going on here. But I'm not sure how best to proceed in debugging the issue.

          Morgan Goose added a comment - Ok so I followed danielbeck 's advice in IRC to try and "bisect" via plugin downgrades. I ran a jar of the service and setup an Org to mimic my job setup as close as possible, starting with all the same versions of plugins, and then also made the global param env variables. It worked first time w/o issue :| So instead of a bug in the code pushing these vars, it instead seems that there is a half-write cache sorta issue that's going on here. But I'm not sure how best to proceed in debugging the issue.

          Miguelángel Fernández added a comment - - edited

          I was facing the same issue. To test I declared a global property/variable called "GREET" and ran this script.

           

          echo("env.GREET says '${env.GREET}'")
          stage('test'){
              echo("stage env.GREET says '${env.GREET}'")
          }
          node{
              echo("node env.GREET says '${env.GREET}'")
          }
          

          Using the Pipeline plugin version 2.4 I was getting this:

          env.GREET says 'null'
          [Pipeline] stage
          [Pipeline] { (test)
          [Pipeline] echo
          stage env.GREET says 'null'
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] node
          Running on master in C:\Program Files (x86)\Jenkins\jobs\global-envbar-test\workspace
          [Pipeline] {
          [Pipeline] echo
          node env.GREET says 'Hi, I'm here!'
          

           
          I upgraded the Pipeline plugin to version 2.5 and the issue was solved:

          env.GREET says 'Hi, I'm here!'
          [Pipeline] stage
          [Pipeline] { (test)
          [Pipeline] echo
          stage env.GREET says 'Hi, I'm here!'
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] node
          Running on master in C:\Program Files (x86)\Jenkins\jobs\global-envbar-test\workspace
          [Pipeline] {
          [Pipeline] echo
          node env.GREET says 'Hi, I'm here!'
          

          Miguelángel Fernández added a comment - - edited I was facing the same issue. To test I declared a global property/variable called "GREET" and ran this script.   echo( "env.GREET says '${env.GREET}' " ) stage( 'test' ){     echo( "stage env.GREET says '${env.GREET}' " ) } node{     echo( "node env.GREET says '${env.GREET}' " ) } Using the Pipeline plugin version 2.4 I was getting this: env.GREET says ' null ' [Pipeline] stage [Pipeline] { (test) [Pipeline] echo stage env.GREET says ' null ' [Pipeline] } [Pipeline] // stage [Pipeline] node Running on master in C:\Program Files (x86)\Jenkins\jobs\global-envbar-test\workspace [Pipeline] { [Pipeline] echo node env.GREET says 'Hi, I' m here!'   I upgraded the Pipeline plugin to version 2.5 and the issue was solved: env.GREET says 'Hi, I' m here!' [Pipeline] stage [Pipeline] { (test) [Pipeline] echo stage env.GREET says 'Hi, I' m here!' [Pipeline] } [Pipeline] // stage [Pipeline] node Running on master in C:\Program Files (x86)\Jenkins\jobs\global-envbar-test\workspace [Pipeline] { [Pipeline] echo node env.GREET says 'Hi, I' m here!'

            jglick Jesse Glick
            saan800 Sandra Greenhalgh
            Votes:
            2 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: