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

Environment variables defined in pipeline not expanding in docker args

      If you run something like this, you won't get variables expanded in docker args.

      pipeline {
          agent none
          environment {
              MY_VAR_1 = "/bar/foo"
              MY_VAR_2 = "/hello"
          }
          stages {
              stage ('Build') {
                  agent {
                      docker {
                          image 'my-docker-image:1.0'
                          args "-v ${MY_VAR_1}:/foo -v ${MY_VAR_2}:/hello"
                          label 'linux'
                      }
                  }
                  steps {
                      sh "./runMe.sh"
                  }
              }
          }
      }
      

      Output:

      groovy.lang.MissingPropertyException: No such property: MY_VAR_1 for class: groovy.lang.Bindinggroovy.lang.MissingPropertyException: No such property: MY_VAR_1 for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:242) at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) at WorkflowScript.run(WorkflowScript:16) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:59) at WorkflowScript.run(WorkflowScript:1) 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.GeneratedMethodAccessor630.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:83) at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174) at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163) at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122) at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261) at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58) at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232) at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131) at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59) 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:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)Finished: FAILURE
      

          [JENKINS-52769] Environment variables defined in pipeline not expanding in docker args

          perhaps you can try:

          args "-v ${env.MY_VAR_1}:/foo -v ${env.MY_VAR_2}:/hello"
          

          I saw that solution here: https://stackoverflow.com/questions/53520622/parametrized-docker-image-in-jenkins-declarative-pipeline

          Sven Finsterwalder added a comment - perhaps you can try: args "-v ${env.MY_VAR_1}:/foo -v ${env.MY_VAR_2}:/hello" I saw that solution here: https://stackoverflow.com/questions/53520622/parametrized-docker-image-in-jenkins-declarative-pipeline

          Lars Röglin added a comment -

          Had the same issue. In this pipeline the "environment" step does not seem to get executed as above "agent none" is declared. Switching the top level "agent none" to "agent any" solved that issue for me and I could even use scripts to declare variables

          pipeline {
              
              agent any
              
              environment {
                  //Random name for docker volume mounts
                  VOL_NAME = "${sh(script:'cat /dev/urandom | tr -cd \'a-z0-9\'| head -c 32', returnStdout: true).trim()}"
              }
              ...
          

          Lars Röglin added a comment - Had the same issue. In this pipeline the "environment" step does not seem to get executed as above "agent none" is declared. Switching the top level "agent none" to "agent any" solved that issue for me and I could even use scripts to declare variables pipeline { agent any environment { //Random name for docker volume mounts VOL_NAME = "${sh(script: 'cat /dev/urandom | tr -cd \' a-z0-9\ '| head -c 32' , returnStdout: true ).trim()}" } ...

            Unassigned Unassigned
            wlaszlo William Laszlo
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: