Return parameters / results of a task (called by a pipeline) back to the same pipeline

Jenkins pipeline: I have a pipeline p1 that starts job j1 and then j2. I need some parameters given by j1 and passed to j2 in the p1 pipeline. How to realize this ridicule with the Jenkins plugin? thanks in advance

+8
jenkins jenkins-pipeline
source share
3 answers

This can be done using "env". If you manage to make j1 add your information to the env assembly.

If j1 was pieleline, you could env.MYKEY=MYVALUE . For freestyle, I believe that it should work using the EnvInject plugin (not tried). In p1 you will receive a card with this information if you are outside the assembly result.

So, if you do a line in p1 :

 // somewhere in your pipeline, ie p1: def j1BuildResult = build job: 'J1' def j1EnvVariables = j1BuildResult.getBuildVariables(); 

then j1EnvVariables will be a mapping containing the variables set to j1 .

PS: how to transfer this information as parameters p2 , for example. here .

+8
source share

I had a similar problem. I had to do this, having tasks J1, J2 create property files, and then acquire these files using the "Copy artifact" in the main P1 pipeline. Then convert the properties to Java properties (which may require the approval of the script in Jenkins). It would be nice if Jenkins Pipeline could return the parameters directly in the code (maybe this needs to be done, but I don't know that). The return from the build phase is RunWrapper , it seems that it has no way to return the result that I can see (if only we used some existing property, for example, the assembly description).

So I had something like this:

 // Pipeline code in P1 // Build J1 and get result. def j1BuildResult = build job: 'J1', parameters: [string(name: 'J1_PROP', value: 'FOO')], propagate: true, wait: true // Get results of J1 step([$class : 'CopyArtifact', filter: 'j1-result.properties', fingerprintArtifacts: true, flatten : true, projectName : 'J1', selector : [$class : 'SpecificBuildSelector', buildNumber: buildResult.getNumber().toString()]]) // Load J1 properties (you may need to turn off sandbox or approve this in Jenkins) Properties j1Props = new Properties() j1Props.load(new StringReader(readFile('j1-result.properties'))) // Build J2 def j2BuildResult = build job: 'J2', parameters: [string(name: 'J2_PROP', value: j1Props.someProperty)], propagate: true, wait: true // Get results of J2 step([$class : 'CopyArtifact', filter: 'j2-result.properties', fingerprintArtifacts: true, flatten : true, projectName : 'J2', selector : [$class : 'SpecificBuildSelector', buildNumber: buildResult.getNumber().toString()]]) // Load J2 properties (you may need to turn off sandbox or approve this in Jenkins) Properties j2Props = new Properties() j1Props.load(new StringReader(readFile('j2-result.properties'))) 
+6
source share

You can get build options along with environment variables using

 def buildProperties = runWrapper.rawBuild.getEnvironment() 

This is a groovy card. Target parameters can be obtained using

 String someProperty = buildProperties.someProperty 

Limitations: you must enable the method hudson.model.Run getEnvironment in the "In-process Script Approval" and call this code inside the node closure (due to rawBuild ).

I also tried runWrapper.rawBuild.getAction(ParametersAction.class) , but it requires many to import into Jenkinsfile.

Note: runWrapper.getBuildVariables() returns nothing to me.

+3
source share

All Articles