Passing data between build steps in Jenkins

2019-03-18 07:31发布

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)

8条回答
我命由我不由天
2楼-- · 2019-03-18 08:14

One thing remains between shells: the workspace.
Simple and stupid solution: use file(s)!

Huge additional advantage: it works when you split your job in several jobs and use the Clone Workspace plugin

Build Step #1 - Execute shell

START=timestamp
...
echo $START > env_start.txt

...

Build Step #3 - Execute Shell

START=`cat env_start.txt`
END=timestamp
TIME_LAPSED=$END-$START
查看更多
霸刀☆藐视天下
3楼-- · 2019-03-18 08:20

@Gurubaran's solution works for me, you need to install "Environment Injector" plugin in jenkins, then

Step1: use shell/powershell/windows batch/etc. to create generate a properties(key=value) file. for example: the file path is $WORKSPACE/env.properties.

Step2: Add an "Inject environment variables" component and set "Properties File Path" to $WORKSPACE/env.properties

After Step2: You can use these environment variables in the following steps.

See the example picture here

查看更多
登录 后发表回答