Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return parameters/results from a job(triggered by pipeline) back to the same pipeline

Tags:

Jenkins pipeline: I have a pipeline p1 that triggers a job j1 and then job j2. I want some parameters that are set by j1 and passed to j2 in pipeline p1. How do I implement this functionality using Jenkins pipeline plugin? Thanks in advance

like image 312
Sid.R Avatar asked Sep 16 '16 20:09

Sid.R


People also ask

How do you trigger a Jenkins pipeline from another pipeline?

You can follow the below steps to trigger a Jenkins pipeline in another Jenkins pipeline. Select a job that triggers a remote one and then go to Job Configuration > Build section > Add Build Step > Trigger builds on remote/local projects option.


2 Answers

It can be done using "env". If you manage to make j1 add its information into the build's env.

If j1 was a pipeline you could to env.MYKEY=MYVALUE. For a freestyle-job it should work using the EnvInject plugin (didn't try). In p1 then you will get a map with that information if you out of the build result.

So, if you do in p1 something line this:

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

then j1EnvVariables will be a map containing the variables you set in j1.

PS: how to pass that info to as parameters p2 is e.g. covered here.

like image 141
tobi42 Avatar answered Oct 05 '22 00:10

tobi42


I had a similar issue. I had to do it by having the jobs J1, J2 create properties files then acquire those files using "Copy Artifact" in the main pipeline P1. Then convert the properties into Java properties (which may require some script approval in Jenkins). It would be nice if the Jenkins Pipeline could return parameters directly in code (perhaps there is away to do this, but I don't know it). The return from a build step is a RunWrapper, it does not seem to have a way to return a custom result that I can see (unless we used some existing property like build 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'))) 
like image 35
macg33zr Avatar answered Oct 05 '22 01:10

macg33zr