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.

  • Building iOS project with Jenkins on Ubuntu Linux
  • Jenkin's build failing on npm install
  • How to use Jenkins parameterized builds?
  • Insufficient privileges error with sonarRunner
  • Combining Build triggers in Jenkins
  • Run Inspections offline (from Jenkins) Android Studio 1.1 vs 1.5
  • 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.

  • Jenkins pipeline - git branch
  • Jenkins build failed on OSX
  • Android ZBar Exception only with Jenkins build
  • How to create and add users to a group in Jenkins for authentication?
  • Configure Jenkins to use GIT On Windows, Got stderr: Permission denied (publickey)
  • include downstream/child Jenkins job's console output into triggering job's console output
  • 4 Solutions collect form web for “Jenkins + Build Flow, how to pass a variable from one job to another”

    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 )
    

    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.

    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.

    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.

    Git Baby is a git and github fan, let's start git clone.