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.
- what did you try? (Code samples, command lines, screenshots, videos, etc)
- what did happen? (Error messages, description, outputs, stuff)
- 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 {
MY_VARIABLE = ââ
}
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
- 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.
- generally while updating env.$THING works, its not officially supported
- I would either use the environment {} block, or set something via env.$THING, but not both
Hello,
This is a year later, but, I came upon the same issue. The solution is NOT TO DECLARE the MY_VARIABLE in the environment section! Donât ask me why. Beats meâŚ
Example:
pipeline {
agent any
environment {
OTHER_VARIABLE = ''
}
stages {
stage(âAssign value to variableâ) {
steps {
script {
env.MY_VARIABLE = 'Hello'
echo "MY_VARIABLE is now set to ${env.MY_VARIABLE}"
}
}
}
}
}
Thank you! Works for me!