Issue
I have a Jenkins variable BUILDVERSION_DATE in stage which is calculated and formatted correctly. Everything works perfectly.
script {
def now = new Date();
def inOneHour = new Date(now.getTime() + 1 * 3600 * 1000);
println inOneHour.format("yyyy-MM-dd-HH-mm-ss", TimeZone.getTimeZone('UTC'))
def BUILDVERSION_DATE=inOneHour.format("yyyy-MM-dd-HH-mm-ss", TimeZone.getTimeZone('UTC'))
}
Now I would like to use this calculated variable in multiple stages (without code repetition).
I have tried to put this code into environment {...}
section but it fails.
If it was static variable I know I can define it in environment segment. But how this calculated variable be defined and used in multiple stages? Thanks!
Solution
If you want to add this value to an environment variable named BUILDVERSION_DATE
, then you can assign it to the env
object intrinsic to Jenkins Pipeline:
env.BUILDVERSION_DATE=inOneHour.format("yyyy-MM-dd-HH-mm-ss", TimeZone.getTimeZone('UTC'))
Answered By - Matt Schuchard
Answer Checked By - David Marino (JavaFixing Volunteer)