3

I try to launch a job from a parametrized trigger and I would compute the name from a given variable. Is it possible to set in field : Build Triggers Projects to build a value like this ${RELEASE}-MAIN-${PROJECT}-LOAD_START

?

user1541032
  • 31
  • 1
  • 2

1 Answers1

4

Unfortunately, this isn't possible with the Build Triggers. I looked for a solution for this "higher order build job" that would allow you to create a dynamic build name with a one of the parameterized build plugins, but I couldn't find one.

However, using the Groovy Postbuild Plugin, you can do a lot of powerful things. Below is a script that can be modified to do what you want. In particular, notice that it gets environmental variables using build.buildVariables.get("MY_ENV_VAR"). The environmental variable TARGET_BUILD_JOB specifies the name of the build job to build. In your case, you would want to build TARGET_BUILD_JOB using these two environmental variables:

build.buildVariables.get("RELEASE")
build.buildVariables.get("PROJECT")

The script is commented so that if you're not familiar with Groovy, which is based off Java, it should hopefully make sense!

import hudson.model.*
import hudson.model.queue.*
import hudson.model.labels.*
import org.jvnet.jenkins.plugins.nodelabelparameter.*

def failBuild(msg)
{
     throw new RuntimeException("[GROOVY] User message, exiting with error: " + msg)
}

// Get the current build job
def thr = Thread.currentThread()
def build = thr?.executable

// Get the parameters for the current build job
// For ?:, see "Elvis Operator" (http://groovy.codehaus.org/Operators#Operators-ElvisOperator)
def currentParameters = build.getAction(ParametersAction.class)?.getParameters() ?: 
    failBuild("There are no parameters to pass down.")

def nodeName = build.getBuiltOnStr()
def newParameters = new ArrayList(currentParameters); newParameters << new NodeParameterValue("param_NODE", 
    "Target node -- the node of the previous job", nodeName)

// Retrieve information about the target build job
def targetJobName = build.buildVariables.get("TARGET_BUILD_JOB")
def targetJobObject = Hudson.instance.getItem(targetJobName) ?: 
    failBuild("Could not find a build job with the name $targetJobName. (Are you sure the spelling is correct?)")
println("$targetJobObject, $targetJobName")
def buildNumber = targetJobObject.getNextBuildNumber()

// Add information about downstream job to log
def jobUrl = targetJobObject.getAbsoluteUrl()
println("Starting downstream job $targetJobName ($jobUrl)" +  "\n")
println("======= DOWNSTREAM PARAMETERS =======")
println("$newParameters")

// Start the downstream build job if this build job was successful
boolean targetBuildQueued = targetJobObject.scheduleBuild(5,
      new Cause.UpstreamCause(build),
      new ParametersAction(newParameters)
    );

if (targetBuildQueued)
{
    println("Build started successfully")
    println("Console (wait a few seconds before clicking): $jobUrl/$buildNumber/console")
}
else
    failBuild("Could not start target build job")
Roy
  • 3,574
  • 2
  • 29
  • 39