I'd like to do something along the lines of:
This is overly simple and just demonstrates what I'd like to do. Basically, I want to be able to store and access variables within a single job scope between multiple build steps. Also, I can get around by storing the data to a file and reading it later, but I'd like something easier and less 'hacky'
Build Step #1 - Execute shell
$START=timestamp
Build Step #2 - Run another job
Build Step #3 - Execute Shell
$END=timestamp
TIME_LAPSED=$END-$START
(post lapsed time somewhere)
On top of what @Gurubaran suggested (which is what I'd do if had no other option), I'd just opt for joining the build steps to a single one, which will greatly simplify this need.
You will need to care for the error handling logic and exit conditions, but your environment will be solid!
I hope this helps.
Jenkins allows you to inject environment variables to the build process. Maybe all you have to do is Inject the Start time and End Time as environment variables and access them across your build steps.
One way to work with Jenkins variables is to use
jenkins-cli.jar
in the build step, it takes some work but this will addFOO=1
in the parameters list, since it's running in a build step it knows which build to set the parameter for.We use inject environment variables plugin extensively and it works great. The solution is:
If you are using the declarative pipeline syntax defining a variable in the
environment
section and using ascript
step to set its value may be useful.I'm doing something like this with a declarative pipeline and it works for passing a variable (both inside one stage and between stages):
None of:
works (as of v1.656). You are able to read each of them, of course, but new values that are assigned to them are not available in subsequent build steps.
Hence, JediMasterCoder's answer and handling via file like Destroyica's are the only options so far.