Answers: <p>You can also use environment block to inject an environment variable.</p>
  
  <p>(Side note: <code>sh</code> is not needed for echo)</p>
  
  <pre><code>pipeline {
      agent none
      environment {
          FOO = "bar"
      }
      stages {
          stage("first") {
              steps {
                  echo "${env.FOO}"
                  // or echo "${FOO}"
              }
          }
      }
  }
  </code></pre>
  
  <p>You can even define the env var inside the stage block to limit the scope:</p>
  
  <pre><code>pipeline {
      agent none
      stages {
          stage("first") {
              environment {
                  FOO = "bar"
              }
              steps {
                  // prints "bar"
                  echo "${env.FOO}"
                  // or echo "${FOO}"
              }
          }
          stage("second") {
              steps {
                  // prints "null"
                  echo "${env.FOO}"
                  // or echo "${FOO}", pipeline would fail here
              }
          }
      }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-02-01T01:36:16.967Z
Answers: You can use dynamic code in the environment block.  https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#setting-environment-variables-dynamically
Answers: Steve Mitchell
Answers: CC BY-SA 4.0
Answers: 2020-01-14T00:11:38.437Z
Answers: 2020-01-14T00:11:38.437Z
Answers: modle13
Answers: <p>The variable must be defined in a <code>script</code> section.</p>
  <pre><code>pipeline {
      agent none
      stages {
          stage(&quot;first&quot;) {
              steps {
                   script {
                        foo = &quot;bar&quot;
                   }
                   sh &quot;echo ${foo}&quot;
              }
          }
      }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-05-14T22:08:14.713Z
Answers: 2023-02-28T06:00:01.000Z
Answers: 2023-02-28T06:00:01.000Z
Answers: plmaheu
Body: <p>I defined variable in declarative Jenkins pipeline script but having issues with simple variable declaration.</p>
  
  <p>Here is my script:</p>
  
  <pre><code>pipeline {
      agent none
      stages {
          stage("first") {
              def foo = "foo" // fails with "WorkflowScript: 5: Expected a step @ line 5, column 13."
              sh "echo ${foo}"
          }
      }
  }
  </code></pre>
  
  <p>but it's shows error:</p>
  
  <pre><code>org.codehaus.groovy.control.MultipleCompilationErrorsException:
  startup failed:
  WorkflowScript: 5: Expected a step @ line 5, column 13.
      def foo = "foo"
      ^
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2019-03-07T03:14:39.443Z
Comments: You may have to enclose the shell script command within the 'Step' similar to 'Stage'. Refer: https://jenkins.io/doc/book/pipeline/syntax/
Comments: Karthik Venkatesan
Comments: CC BY-SA 4.0
Comments: 2023-11-15T07:45:03.670Z
Comments: same question post as: https://stackoverflow.com/questions/39832862/cannot-define-variable-in-pipeline-stage
Comments: Nor.Z
Tags: <jenkins><jenkins-pipeline>
Title: How to define variable in Jenkins declarative pipeline?
UserDisplayName: Jay
ThreadText: # How to define variable in Jenkins declarative pipeline?
  
  (Asked by: username_0 on 2019-01-28)
  
  I defined variable in declarative Jenkins pipeline script but having issues with simple variable declaration.
  
  Here is my script:
  
  ```
  pipeline {
      agent none
      stages {
          stage("first") {
              def foo = "foo" // fails with "WorkflowScript: 5: Expected a step @ line 5, column 13."
              sh "echo ${foo}"
          }
      }
  }
  
  ```
  
  but it's shows error:
  
  ```
  org.codehaus.groovy.control.MultipleCompilationErrorsException:
  startup failed:
  WorkflowScript: 5: Expected a step @ line 5, column 13.
      def foo = "foo"
      ^
  
  ```
  
  **Tags:** jenkins, jenkins-pipeline
  
  > **Comments:**
  > **username_1**: You may have to enclose the shell script command within the 'Step' similar to 'Stage'. Refer: https://jenkins.io/doc/book/pipeline/syntax/  
  > **username_2**: same question post as: https://stackoverflow.com/questions/39832862/cannot-define-variable-in-pipeline-stage  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_5 on 2019-05-14. Score: 26
  
  The variable must be defined in a `script` section.
  
  ```
  pipeline {
      agent none
      stages {
          stage("first") {
              steps {
                   script {
                        foo = "bar"
                   }
                   sh "echo ${foo}"
              }
          }
      }
  }
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2020-01-14. Score: 7
  
  You can also use environment block to inject an environment variable.
  
  (Side note: `sh` is not needed for echo)
  
  ```
  pipeline {
      agent none
      environment {
          FOO = "bar"
      }
      stages {
          stage("first") {
              steps {
                  echo "${env.FOO}"
                  // or echo "${FOO}"
              }
          }
      }
  }
  
  ```
  
  You can even define the env var inside the stage block to limit the scope:
  
  ```
  pipeline {
      agent none
      stages {
          stage("first") {
              environment {
                  FOO = "bar"
              }
              steps {
                  // prints "bar"
                  echo "${env.FOO}"
                  // or echo "${FOO}"
              }
          }
          stage("second") {
              steps {
                  // prints "null"
                  echo "${env.FOO}"
                  // or echo "${FOO}", pipeline would fail here
              }
          }
      }
  }
  
  ```
  
  > **Comments:**
  > **username_4**: You can use dynamic code in the environment block.  https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#setting-environment-variables-dynamically  
  
  ---
  
  