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

CC BY-SA 4.0
2022-02-01T01:36:16.967Z
You can use dynamic code in the environment block.  https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#setting-environment-variables-dynamically
Steve Mitchell
CC BY-SA 4.0
2020-01-14T00:11:38.437Z
2020-01-14T00:11:38.437Z
modle13
<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>

CC BY-SA 4.0
2019-05-14T22:08:14.713Z
2023-02-28T06:00:01.000Z
2023-02-28T06:00:01.000Z
plmaheu
<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>

CC BY-SA 4.0
2019-03-07T03:14:39.443Z
You may have to enclose the shell script command within the 'Step' similar to 'Stage'. Refer: https://jenkins.io/doc/book/pipeline/syntax/
Karthik Venkatesan
CC BY-SA 4.0
2023-11-15T07:45:03.670Z
same question post as: https://stackoverflow.com/questions/39832862/cannot-define-variable-in-pipeline-stage
Nor.Z
<jenkins><jenkins-pipeline>
How to define variable in Jenkins declarative pipeline?
Jay
# 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  

---

