Passing data between build steps in Jenkins

后端 未结 8 1664
臣服心动
臣服心动 2020-12-31 00:09

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 vari

相关标签:
8条回答
  • 2020-12-31 00:27

    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}"
                    }
                }
    [...]
    
    0 讨论(0)
  • 2020-12-31 00:27

    @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.

    Example:

    0 讨论(0)
  • 2020-12-31 00:34

    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
    
    0 讨论(0)
  • 2020-12-31 00:34

    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.

    0 讨论(0)
  • 2020-12-31 00:36

    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.
    0 讨论(0)
  • 2020-12-31 00:40

    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.

    0 讨论(0)
提交回复
热议问题