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

Passing variables to a Jenkinsfile shell script within "sh"

    Details

    • Similar Issues:

      Description

      I have a Jenkinsfile that looks roughly like this:

       

      ...
      stage('Build') {
           steps {
               script {
                   FOO = sh (script: "echo bar", returnStdout: true)
               }
           }
      }
      stage('Plan') {
           steps {
               sh "FOO=${FOO} ./tests/run.sh"
           }
      }
      ...
      

       

      Why am I not seeing the variable $FOO within run.sh? This works in a regular shell and bash. Furthermore it's not like Jenkins isn't picking up the value of $FOO. It correctly prints the value of $FOO as "bar" in other places that I've used it.

       

        Attachments

          Activity

          Hide
          abayer Andrew Bayer added a comment -

          Then I honestly have no idea. Moving to workflow-durable-task-step-plugin.

          Show
          abayer Andrew Bayer added a comment - Then I honestly have no idea. Moving to workflow-durable-task-step-plugin .
          Hide
          luhkevin Kevin Lu added a comment -

          Actually when the "Plan" stage runs, the value of FOO is picked up correctly, but somehow the FOO variable itself is not passed into the environment of ./tests/run.sh, so I don't think it's an issue of scope

          Show
          luhkevin Kevin Lu added a comment - Actually when the "Plan" stage runs, the value of FOO is picked up correctly, but somehow the FOO  variable itself is not passed into the environment of ./tests/run.sh , so I don't think it's an issue of scope
          Hide
          abayer Andrew Bayer added a comment -

          You're setting a variable named FOO in the script block, but it's only in that scope. In general, if you need to do this kind of variable-passing, I'd recommend you use Scripted Pipeline, not Declarative.

          Show
          abayer Andrew Bayer added a comment - You're setting a variable named FOO in the script block, but it's only in that scope. In general, if you need to do this kind of variable-passing, I'd recommend you use Scripted Pipeline, not Declarative.

            People

            • Assignee:
              Unassigned
              Reporter:
              luhkevin Kevin Lu
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: