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条回答
Animai°情兽
2楼-- · 2019-03-18 07:58

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.

查看更多
一夜七次
3楼-- · 2019-03-18 07:59

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. enter image description here

查看更多
对你真心纯属浪费
4楼-- · 2019-03-18 07:59

One way to work with Jenkins variables is to use jenkins-cli.jar in the build step, it takes some work but this will add FOO=1 in the parameters list, since it's running in a build step it knows which build to set the parameter for.

java -jar ${JENKINS_HOME}/war/WEB-INF/jenkins-cli.jar -s ${JENKINS_URL} set-build-parameter FOO 1
查看更多
时光不老,我们不散
5楼-- · 2019-03-18 08:06

We use inject environment variables plugin extensively and it works great. The solution is:

  1. Set your variable myenv=value1
  2. print to file in workspace: echo "myenv=$myenv" > tmp.myenv
  3. Inject after every change: Use envinject to read environment from file tmp.myenv -> myenv is now known as part of the job environment.
查看更多
萌系小妹纸
6楼-- · 2019-03-18 08:10

If you are using the declarative pipeline syntax defining a variable in the environment section and using a script 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):

pipeline {
        agent any
        environment {
            variable = ''
        }
        stages {
            stage('Some stage') { 
                steps {
                    script {
                        if (some condition){
                            variable = 'some value'
                        } else { variable = 'else value' }
                    }                
                    sh '${somepath}/bin/script ' + 
                        "-parameter=${variable}"
                    }
                }
            }
            stage('Dummy print') {
                steps {
                    sh "echo ${variable}"
                }
            }
[...]
查看更多
迷人小祖宗
7楼-- · 2019-03-18 08:11

None of:

  • String Parameter of a Parameterized Build
  • pre-build Inject environment variables to the build process
  • in-build step Inject environment variables

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.

查看更多
登录 后发表回答