2012-07-20 87 views

回答

4

不幸的是,这对于构建触发器来说是不可能的。我寻找这个“高阶构建作业”的解决方案,它可以让你用一个参数化的构建插件创建一个动态构建名称,但是我找不到一个。

但是,使用Groovy Postbuild Plugin,你可以做很多强大的事情。下面是一个脚本,可以进行修改以执行您想要的操作。尤其要注意的是,它使用build.buildVariables.get("MY_ENV_VAR")来获取环境变量。环境变量TARGET_BUILD_JOB指定要构建的构建作业的名称。在你的情况,你会希望使用这两个环境变量来构建TARGET_BUILD_JOB

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

的脚本注释,这样,如果你不熟悉使用Groovy,它是基于Java的关闭,它应该有希望有意义!

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")