5

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.

Unknown
  • 5,722
  • 5
  • 43
  • 64

4 Answers4

9

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 )
Dave Bacher
  • 15,652
  • 3
  • 63
  • 86
  • Is this working for you? I'm creating a `vars.txt` properties file (containing `MY_BUILD_VERSION=something`) and using "inject variables" as the next build step as part of `job1`. But for some reason, in buildflow the version comes out as `null`, after triggering `job1` (using your example as code). – Unknown Nov 03 '14 at 15:22
  • Nevermind, I figured it out. I was doing `b = build('job-1')` and trying `build.environment.get()`; `b.environment.get()` works properly. Thanks! – Unknown Nov 03 '14 at 15:25
0

When you run job1 export the version with name as system property.

export appVersion="stringOfVersion-123"

Then it depend if you know how long is version (count of numbers or others characters). If you know it you can parse variable from end in second build as new variable and use it.

How parse string you can find in this question with nice examples.

Community
  • 1
  • 1
zorbon.cz
  • 148
  • 6
  • Do you have some example of this actually working? Because in my tests it seems it doesn't; the environment isn't preserved from one job to another. – Unknown Oct 23 '14 at 06:17
  • Actually not, we use Jenkins only occasionally. But if you running some Bash script as build1 then you can export variable as system variable and in second build you can use this variable. Or can you paste more info about that (screenshots from Jenkins, code,...) to question? – zorbon.cz Oct 23 '14 at 06:40
  • If you use `grep`, then you get "version=1.3.4" (for example if you make `export version="1.2.3"`), but when you use `$version` in command (for print use `echo $version`) then you get only number. Try it with `$` statement. – zorbon.cz Oct 23 '14 at 09:07
  • The point of grep was to display the presence of "`VERSION`" in `job2`'s environment and to prove that it's not there. What you are saying is not working, unfortunately. – Unknown Oct 23 '14 at 09:13
0

If job2 always should get some information from job1 you could use approach without parameters. job1 could publish artifact with version and job2 will use that artifact (with Copy Artifact Plugin for example). With that approach job2 could be executed also as standalone job.

ISanych
  • 21,590
  • 4
  • 32
  • 52
0

For anyone else coming upon this, another solution is to use a scriptler script, where you pass in the .properties file path, and the script will add the 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 will convert any periods to underscores, and capitalize all letters. Using a scriptler script ensures that you have a method that works independent of the "plugin soup" you are using.