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

I have a build script similar to the documentation example : two tasks, one after the other.

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

My job1 is a shell script that builds a package from the extracted git repositoy and displays the version of the built-in package.

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

+5
source share
4 answers

The problem with the simple use of export in the build step of the shell script is that the exported variables disappear when the shell script terminates, they are not propagated until the job.

Use the EnvInject plugin to create environment variables in your assembly. if you write out the properties file as part of your assembly, EnvInject can read the file and enter the variables as an assembly step . The properties file has the simple format KEY=VALUE :

 MY_BUILD_VERSION=some_parsed_value 

Once you have the environment variable set in your task in the plugin assembly thread, you can extract the value of the variable and use it in the following tasks:

 def version = build.environment.get( "MY_BUILD_VERSION" ) out.println String.format("Parameters: version: %s", version) build( "My Second Build", MY_BUILD_VERSION: version ) 
+9
source

When job1 starts job1 export the version with the name as the system property.

 export appVersion="stringOfVersion-123" 

Then it depends, do you know how long is the version (the number of numbers or other characters). If you know this, you can parse the variable from the end in the second assembly as a new variable and use it.

As a line of parsing you can find in this question with good examples .

0
source

If job2 should always get some information from job1, you can use the parameterless approach. job1 can publish an artifact with the version, and job2 will use this artifact (for example, Copy Artifact Plugin ). With this approach, job2 can also be performed as a separate task.

0
source

For others who come to this, another solution is to use a script scripter, where you pass the path to the .properties file, and the script adds properties to the list of job variables:

 Properties properties = new Properties() FilePath workspace = build.getWorkspace() FilePath sourceFile = workspace.child(path) properties.load(sourceFile.read()) properties.each { key, value -> key = key.replace(".", "_").toUpperCase() Job.setVariable(build, key, value) println "Created Variable: " + key + "=" + value } 

This converts any periods to underscores and capital letters of all letters. Using a scriptler script ensures that you have a method that works regardless of the "plugin soup" you use.

0
source

Source: https://habr.com/ru/post/1205271/


All Articles