vel
vel

Reputation: 1210

Jenkins - use def Variable in multiple stages

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!

Upvotes: 1

Views: 565

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28854

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

Upvotes: 1

Related Questions