Empty ENV variable giving error

I am trying to initialise the env variable in jenkinsfile as empty but in attempting that it is giving error. Is there any work around to it. As I wanna pass the dynamic value to it and use it downstream job.

Hi there,

“it doesn’t work” is always hard to remote debug. Try to always include these 3 things.

  1. what did you try? (Code samples, command lines, screenshots, videos, etc)
  2. what did happen? (Error messages, description, outputs, stuff)
  3. what did you expect to happen?

Otherwise, it’s hard for us to get into your context and you’ll have to have someone who is exactly knowledgeable come along.

pipeline {
agent any
environment {
stages {
stage(‘Assign value to variable’) {
steps {
script {
env.MY_VARIABLE = ‘Hello’
echo “MY_VARIABLE is now set to ${env.MY_VARIABLE}”

I was expecting the output as Hello but instead i got

any update or solution will be really appreciable

After playing around with it a bit, I have no real conclusion for you, except

  1. It might be how declarative pipelines are parsed, so the environment variable might get shoved into the string before the script {} block runs. You might be able to get something like it working in scripted syntax, but then you wouldn’t have environment {} to deal with anyways.
  2. generally while updating env.$THING works, its not officially supported
  3. I would either use the environment {} block, or set something via env.$THING, but not both