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

Can't access Jenkins Global Properties from Pipeline DSL

    XMLWordPrintable

Details

    • Bug
    • Status: Reopened (View Workflow)
    • Minor
    • Resolution: Unresolved
    • pipeline
    • None
    • workflow-plugin: v1.8
      jenkins: v1.616

    Description

      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)
      
      

      Attachments

        Activity

          danielbeck 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?

          danielbeck 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 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 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 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 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 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 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.
          mig82 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!'
          
          mig82 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!'

          People

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

            Dates

              Created:
              Updated: