Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the result back from Parameterized Trigger plugin

I have 2 jobs: "Helper" and "Main" and the single jenkins instance (which is the host and the executor).

The helper manages 3rd party resource and makes the preparation for the Main job (to be precise - it creates the environment for the application to be deployed for testing).

The only artifact for the helper job is a single file with IP of the environment prepared especially for the Main job.

How would I pass back the build from the Helper to the Main in this case?

like image 937
zerkms Avatar asked Dec 26 '22 10:12

zerkms


1 Answers

You are saying that you only need to pass a file with an IP to the "Main" job. If all you need is that IP, there are easier ways of doing it (without files), I will describe both.

To pass an artifact from one job to another

In the "Helper" job, you need to archive that file from your workspace.

  1. In post-build actions, choose Archive the artifacts
  2. Put a path relative to the workspace. You can use wildcards, or hardcode the name of the file if it is always same.
  3. Configure this job to automatically trigger your "Main" job using Trigger/Call builds on other projects build step. If you don't have this plugin, you can get it here
  4. For Projects to build, enter the name of your "Main" job

Now, in the "Main" job, you need to copy this artifact from the previous ("Helper") job.

  1. For the first build step, select Copy artifacts from another project build step. If you don't have this plugin, you can get it here
  2. For the Project name, enter the name of your "Helper" job
  3. For Which build, select Latest successful build
  4. For Artifacts to copy, use **/yourartifactname*.* Your artifact name will be what you configured in "Helper" job. Using **/ in front makes sure it will ignore any directory structure before getting to the artifact
  5. For, Target directory, specify a location in your "Main" job's workspace where this file will be copied too.
  6. Checkmark Flatten directories, so the file goes directly to the location specified in Step 5, else it will retain the directory structure that it was archived under (in "Helper" job)

Now, your "Main" job has the file from "Helper" job in it's workspace. Use it like you would any other file in your workspace

To pass a variable from one job to another

Like I mentioned, if all you need is that one IP address, that you have as a variable at one point in time in "Helper" job, you just send it to "Main" job using the Trigger/Call builds on other projects step that you configured in steps 3 and 4 of the "Helper" job. In this case, you don't need any special configuration on "Main" job.

  1. Configure "Helper" job to automatically trigger your "Main" job using Trigger/Call builds on other projects build step. If you don't have this plugin, you can get it here
  2. For Projects to build, enter the name of your "Main" job
  3. Click Add Parameters button
  4. Select Predefined parameters
  5. Type VarForMain=$VarFromHelper, where VarFromHelper is your environment variable from the "Helper" job that contains your IP address, and VarForMain is the environment variable that will be set in your "Main" job to this value. There is no reason why these can't have the same name.

Now, in your "Main" job, you can reference $VarForMain as you would any other environment variable

like image 100
Slav Avatar answered Jan 21 '23 18:01

Slav