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.

  • How to export windows batch variables as build parameters to following steps in Jenkins?
  • Sending Cppcheck result/report on email from Jenkins using email-ext plugin
  • Unable to install enterprise app - This provisioning profile cannot be installed on this device
  • Ruby/Cucumber: Integration to Jenkins : Bitbucket
  • How to generate war file using jenkins Build->Execute shell option?
  • How do I get the “changes” (commit message) that triggered a Jenkins build?
  • 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 “Post Build Action” to deploy zip on Maven repository
  • Fixing 'Class is not accessible through the ClassLoader.' warning with Jenkins, SonarQube, and Gradle
  • Jenkins - How to set authorization on project basis
  • Jenkins Email-ext plugin - tokens
  • Jenkins GIT include regions never builds for
  • Obtain Subversion Revision from Previous Jenkins Build
  • 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.