我已经在DSL中编写了我们项目的大部分作业/管道,没有任何以前时髦的经验,但现在我陷入了我无法弄清楚的更高级的问题。
我正在尝试实现一种可以为工作添加 1,2,n 次晋升的方法。下面你可以看到一个完全工作的方法,可以添加一个促销,我希望它能以这样的方式工作,如果我需要另一个,我只会调用该方法两次,但后来我遇到了我的问题 - 只有最新的推广才会生成。
/**
* @param job DSL job object
* @param promotionName Name of the promotion
* @param nextJobs Comma seperated string of jobs to trigger when promotion is executed
* @param deployers Comma seperated string IDs that can execute promotion
* @param params Array of parameters to pass to the next job [0] = key, [1] = value.
*/
static void addPromotion(def job, String promotionName, String nextJobs, String deployers, String[][] params){
job.properties {
promotions {
promotion {
name(promotionName)
icon("star-gold")
conditions {
manual(deployers)
}
actions {
downstreamParameterized {
trigger(nextJobs) {
parameters {
for (String[] param : params){
predefinedProp(param[0]+"=",param[1])
}
}
}
}
}
}
}
}
}
但是,如果我添加另一个这样的"促销"关闭,那么它的工作方式将产生几乎相同的(名称和名称-1(促销:
static void addPromotion(def job, String promotionName, String nextJobs, String deployers, String[][] params){
job.properties {
promotions {
promotion {
name(promotionName)
icon("star-gold")
conditions {
manual(deployers)
}
actions {
downstreamParameterized {
trigger(nextJobs) {
parameters {
for (String[] param : params){
predefinedProp(param[0]+"=",param[1])
}
}
}
}
}
}
promotion {
name("${promotionName}-1")
icon("star-gold")
conditions {
manual(deployers)
}
actions {
downstreamParameterized {
trigger(nextJobs) {
parameters {
for (String[] param : params){
predefinedProp(param[0]+"=",param[1])
}
}
}
}
}
}
}
}
}
是否可以以某种方式重用闭包并从不同的方法填充变量?还是其他想法?
这就是我解决它的方式。
通用促销对象部分:
/**
* Adds 'promoted-builds' plugin configuration to job
**/
class Promotions {
public def job
public String promotionName
public String nextJobs
public String deployers
public String [][] params
/**
* @param job DSL job object
* @param promotionName Name of the promotion
* @param nextJobs Comma seperated string of jobs to trigger when promotion is executed
* @param deployers Comma seperated string IDs that can execute promotion
* @param params Array of parameters to pass to the next job [0] = key, [1] = value.
*/
public Promotions(Object jobName, String promotionName, String nextJobs, String deployers, String[][] params){
this.job = jobName
this.promotionName = promotionName
this.nextJobs = nextJobs
this.deployers = deployers
this.params = params
}
static void addPromotions(Promotions ... jobPromotions){
// Assuming the same job is provided as arguments
jobPromotions[0].job.properties {
promotions {
for (Promotions jobPromotion : jobPromotions){
promotion {
name(jobPromotion.promotionName)
// star-gold, star-silver
icon("star-gold")
conditions {
manual(jobPromotion.deployers)
}
actions {
downstreamParameterized {
trigger(jobPromotion.nextJobs) {
parameters {
for (String[] param : jobPromotion.params){
predefinedProp(param[0],param[1])
}
}
}
}
}
}
}
}
}
}
}
然后我准备我的参数并将它们传递给提升构造函数,最后我调用 addPromotions(( 并将我所有构造的对象传递给它:
def nextJobs = "${Configuration.repoName}-${branchName}-deploy-to-perf"
def deployers = "developer"
def params = [["VERSION", "${VERSION}"],
["SOURCE_GIT_COMMIT", "${SOURCE_GIT_COMMIT}"]] as String[][]
def promo1 = new Promotions(job, "Promote to PERF", nextJobs, deployers, params)
def nextJobs2 = "../master/${Configuration.repoName}-${branchName}-to-prod-dtr"
def deployers2 = "admin"
def params2 = [["VERSION", "${VERSION}"],
["SOURCE_GIT_COMMIT", "${SOURCE_GIT_COMMIT}"]] as String[][]
def promo2 = new Promotions(job, "Promote to PROD", nextJobs2, deployers2, params2)
Promotions.addPromotions(promo1, promo2)