我尝试从参数化触发器启动作业,并从给定变量计算名称。是否可以在字段中设置:构建触发器项目以构建像这样的值 ${RELEASE}-MAIN-${PROJECT}-LOAD_START
?
不幸的是,构建触发器无法做到这一点。我为这个“高阶构建作业”寻找一种解决方案,它允许您使用参数化构建插件之一创建动态构建名称,但我找不到。
但是,使用Groovy Postbuild Plugin,您可以做很多强大的事情。下面是一个脚本,可以修改它来做你想做的事。特别是,请注意它使用build.buildVariables.get("MY_ENV_VAR")
. 环境变量TARGET_BUILD_JOB
指定要构建的构建作业的名称。在您的情况下,您可能希望TARGET_BUILD_JOB
使用这两个环境变量进行构建:
build.buildVariables.get("RELEASE")
build.buildVariables.get("PROJECT")
该脚本已被注释,因此如果您不熟悉基于 Java 的 Groovy,那么它应该是有意义的!
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")