The variable must be defined in a script
section.
pipeline { agent none stages { stage("first") { steps { script { foo = "bar" } sh "echo ${foo}" } } }}