我正在尝试在 Jenkins 2.7 中以预设字符串作为管道启动所有作业。到目前为止,我设法编写了以下 Groovy 代码:
node {
jobs = hudson.model.Hudson.instance.getAllItems(FreeStyleProject)
for (job in jobs) {
// here was an "if" statement: if (job.getFullName() =~ /my regex/) {
stage job.getFullName()
build job: job.getFullName(), propagate: false
}
}
}
然而,每次启动时我都会得到一个java.io.NotSerializableException
,并且只有一个作业匹配(此时两个中)被执行。
我究竟做错了什么? :(
答案1
下面是一个示例,说明我将如何做到这一点:
import hudson.model.*;
// get all jobs which exists
jobs = Hudson.instance.getAllItems(FreeStyleProject);
// iterate through the jobs
for (j in jobs) {
// define a pattern, which jobs I do not want to run
def pattern = 'trunk';
def m = j.getName() =~ pattern;
// if pattern does not match, then run the job
if (!m) {
// first check, if job is buildable
if (j instanceof BuildableItem) {
// run that job
j.scheduleBuild();
}
}
}
我注释了我的代码,所以这应该对你有帮助。