Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins + Build Flow, how to pass a variable from one job to another

I have a build flow scenario similar to the documentation example: two jobs, one running after the other.

b = build("job1")
build("job2", param1: b.????)

My job1 is a shell script that builds a package out of a checked out git repositoy and prints out the version of the built package.

I need to extract the version from job1 (parse output??) and make it available somehow as a parameter to job2. How can this be achieved? Please note that I can't know the version before running job1.

like image 781
Unknown Avatar asked Oct 22 '14 13:10

Unknown


People also ask

How do you pass a parameter in Jenkins pipeline script?

Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.

How do you transfer workspace and environment variables in a pipeline to the next job?

Under Build Environment check Set environment variables through a file. give the path of that file here. If the environment variable is created in the first job then again you can save all the environment variable in a file and browse it using the above method. Install this plugin and go to job configuration paeg.


1 Answers

The problem with simply using export in a shell script build step is that the exported variables disappear when the shell script exits, they are not propagated up to the job.

Use the EnvInject plugin to create environment variables in your build. If you write out a properties file as part of your build, EnvInject can read the file and inject variables as a build step. A properties file has a simple KEY=VALUE format:

MY_BUILD_VERSION=some_parsed_value

Once you have an environment variable set in your job, in the Build Flow plugin, you can extract the variable's value and use it in subsequent jobs:

def version = build.environment.get( "MY_BUILD_VERSION" )
out.println String.format("Parameters: version: %s", version)
build( "My Second Build", MY_BUILD_VERSION: version )
like image 136
Dave Bacher Avatar answered Sep 29 '22 05:09

Dave Bacher