我想触发几个不同的管道作业,具体取决于Controller Pipeline作业的输入参数。
在这个工作中,我构建了其他管道的名称,我想从列表中触发,从python脚本返回。
node {
stage('Get_Clusters_to_Build') {
copyArtifacts filter: params.file_name_var_mapping, fingerprintArtifacts: true, projectName: 'UpdateConfig', selector: lastSuccessful()
script {
cmd_string = 'determine_ci_builds --jobname ' + env.JOB_NAME
clusters = bat(script: cmd_string, returnStdout: true)
output_array = clusters.split('\n')
cluster_array = output_array[2].split(',')
}
echo "${clusters}"
}
jobs = Hudson.instance.getAllItems(AbstractProject.class)
echo "$jobs"
def builders = [:]
for (i=0; i<cluster_array.size(); i++) {
def cluster = cluster_array[i]
def job_to_build = "BuildCI_${cluster}".trim()
echo "### branch${i}"
echo "### ${job_to_build}"
builders["${job_to_build}"] =
{
stage("${job_to_build}") {
build "${job_to_build}"
}
}
}
parallel builders
stage ("TriggerTests") {
echo "Done"
}
}
我的问题是,可能是这样的情况,我从舞台Get_Clusters_to_Build
获得的名字中的一些作业不存在。因此,他们无法触发,我的工作也失败了。
现在我的问题是,有没有办法获取所有管道作业的名称,我如何使用它们来检查是否可以触发构建?
我试过jobs = Hudson.instance.getAllItems(AbstractProject.class)
,但这只给了我“正常”的FreeStyleProject-Jobs。
我想在循环中做这样的事情:
def builders = [:]
for (i=0; i<cluster_array.size(); i++) {
def cluster = cluster_array[i]
def job_to_build = "BuildCI_${cluster}".trim()
echo "### branch${i}"
echo "### ${job_to_build}"
// This part I only want to be executed if job_to_build is found in the jobs list, somehow like:
if job_to_build in jobs: // I know, this is not proper groovy syntax
builders["${job_to_build}"] =
{
stage("${job_to_build}") {
build "${job_to_build}"
}
}
}
parallel builders
答案 0 :(得分:10)
所有管道作业都是org.jenkinsci.plugins.workflow.job.WorkflowJob
的瞬间。因此,您可以使用以下函数获取所有管道作业的名称
@NonCPS
def getPipelineJobNames() {
Hudson.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob)*.fullName
}
然后你可以这样使用它
//...
def jobs = getPipelineJobNames()
if (job_to_build in jobs) {
//....
}
答案 1 :(得分:3)
正如@Vitalii Vitrenko写的那样,工作正常
for (job in Hudson.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob)) {
println job.fullName
}
答案 2 :(得分:2)
尝试使用此语法来获取标准和管道作业:
def jobs = Hudson.instance.getAllItems(hudson.model.Job.class)