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

Global environment variables are not accessible outside of node { }

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Minor Minor
    • pipeline
    • None

      When configuring Global Environment Variables in the "Manage Jenkins" view, the environment variables only seem to be accessible via the env global variable if it is inside the context of a node step.

      I find this to be rather confusing, and would expect global variables defined in Jenkins to be visible in any scope within a Jenkinsfile.

      See attached screenshot and the console output below for a reproduction case

      node {
          sh 'printenv | grep -i foo'
          echo "env.FOO_PIPELINE is: ${env.FOO_PIPELINE}"
      }
      
      echo "Outside of a node: env.FOO_PIPELINE is: ${env.FOO_PIPELINE}"
      

      Results in:

      Started by user admin
      [Pipeline] node
      Running on master in /tmp/jenkins/workspace/printenv
      [Pipeline] {
      [Pipeline] sh
      [printenv] Running shell script
      + printenv
      + grep -i foo
      FOO_PIPELINE=beepboop
      [Pipeline] echo
      env.FOO_PIPELINE is: beepboop
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] echo
      Outside of a node: env.FOO_PIPELINE is: null
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

          [JENKINS-40455] Global environment variables are not accessible outside of node { }

          Patrick Wolf added a comment - - edited

          If you run this without the env prefix you get a stack trace because the Groovy variable is not defined.

          Started by user Admin
          [Pipeline] node
          [Pipeline] {
          [Pipeline] sh
          [env] Running shell script
          + grep -i foo
          + printenv
          FOO_PIPELINE=BARRRRRRR
          [Pipeline] echo
          FOO_PIPELINE is: BARRRRRRR
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          groovy.lang.MissingPropertyException: No such property: FOO_PIPELINE for class: groovy.lang.Binding
          	at groovy.lang.Binding.getVariable(Binding.java:63)
          	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)
          	at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
          	at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
          	at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28)
          	at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
          	at WorkflowScript.run(WorkflowScript:6)
          	at ___cps.transform___(Native Method)
          	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
          	at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
          	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
          	at sun.reflect.GeneratedMethodAccessor473.invoke(Unknown Source)
          	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          	at java.lang.reflect.Method.invoke(Method.java:498)
          	at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
          	at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
          	at com.cloudbees.groovy.cps.Next.step(Next.java:58)
          	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
          	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
          	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
          	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
          	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
          	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
          	at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:163)
          	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:324)
          	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:78)
          	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236)
          	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:224)
          	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:63)
          	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
          	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
          	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
          	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
          	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
          	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          	at java.lang.Thread.run(Thread.java:745)
          Finished: FAILURE
          

          Patrick Wolf added a comment - - edited If you run this without the env prefix you get a stack trace because the Groovy variable is not defined. Started by user Admin [Pipeline] node [Pipeline] { [Pipeline] sh [env] Running shell script + grep -i foo + printenv FOO_PIPELINE=BARRRRRRR [Pipeline] echo FOO_PIPELINE is: BARRRRRRR [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline groovy.lang.MissingPropertyException: No such property: FOO_PIPELINE for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224) at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) at WorkflowScript.run(WorkflowScript:6) at ___cps.transform___(Native Method) at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74) at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30) at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66) at sun.reflect.GeneratedMethodAccessor473.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72) at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21) at com.cloudbees.groovy.cps.Next.step(Next.java:58) at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30) at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:163) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:324) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:78) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:224) at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:63) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112) at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang. Thread .run( Thread .java:745) Finished: FAILURE

          Colin PUY added a comment -

          Indeed this is so confusing !

          We often use variables for private informations such as login/password/API key etc.... in our scripts that are committed in public github repositories.
          These variables are set as global jenkins variable in our master node and we need to retrieve them in our pipeline scripts from master node.

          Colin PUY added a comment - Indeed this is so confusing ! We often use variables for private informations such as login/password/API key etc.... in our scripts that are committed in public github repositories. These variables are set as global jenkins variable in our master node and we need to retrieve them in our pipeline scripts from master node.

            cloudbees CloudBees Inc.
            rtyler R. Tyler Croy
            Votes:
            6 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: