[英]Reduce the pipeline script for running multiple jobs parallel
I have below snippet for getting the matching job name then trigger all of them run parallelly.我有下面的代码片段来获取匹配的作业名称,然后触发所有它们并行运行。
Shared library file CommonPipelineMethods.groovy
共享库文件CommonPipelineMethods.groovy
import jenkins.instance.*
import jenkins.model.*
import hudson.model.Result
import hudson.model.*
import org.jenkinsci.plugins.workflow.support.steps.*
class PipelineMethods {
def buildSingleJob(downstreamJob) {
return {
result = build job: downstreamJob.fullName, propagate: false
echo "${downstreamJob.fullName} finished: ${result.rawBuild.result}"
}
}
}
return new PipelineMethods();
The main Jenkinsfile script:主要的 Jenkinsfile 脚本:
def commonPipelineMethods;
pipeline {
stages {
stage('Load Common Methods into Pipeline') {
def JenkinsFilePath = '/config/jenkins/jobs'
commonPipelineMethods = load "${WORKSPACE}${JenkinsFilePath}/CommonPipelineMethods.groovy"
}
stage('Integration Test Run') {
steps {
script {
matchingJobs = commonPipelineMethods.getIntegrationTestJobs(venture_to_test, testAgainst)
parallel matchingJobs.collectEntries{downstreamJob-> [downstreamJob.name, commonPipelineMethods.buildSingleJob(downstreamJob)]}
}
}
}
}
}
The script works fine but looking at from Map .... parallel step for parallel
the script is a bit busy and not easy to get it.该脚本工作正常,但从Map .... parallel step for parallel
来看Map .... parallel step for parallel
该脚本有点忙,不容易得到它。 The main purpose of this is I want to reduce the pipeline script to be cleaner and easy for others to help maintain.这样做的主要目的是我想减少管道脚本,使其更清晰,更易于其他人帮助维护。 Something simple like calling the external methods as matchingJobs = commonMethods.getIntegrationTestJobs(venture, environment)
, so others can understand it right away and know what the code does in this context.一些简单的事情,比如将外部方法调用为matchingJobs = commonMethods.getIntegrationTestJobs(venture, environment)
,因此其他人可以立即理解它并知道代码在此上下文中的作用。
I tried several ways to improve it but seem if it put part of them into building the single job outside the pipeline itself but into the external library, for example我尝试了几种方法来改进它,但似乎它是否将其中的一部分用于在管道本身之外但在外部库中构建单个作业,例如
def buildSingleJobParallel (jobFullName) {
String tempPipelineResult = 'SUCCESS'
result = build job: jobFullName, propagate: false
echo "${jobFullName} finished: ${result.rawBuild.result.toString()}"
if (result.rawBuild.result.isWorseThan(Result.SUCCESS)) {
tempPipelineResult = 'FAILURE'
}
}
then Jenkins prompted me that groovy.lang.MissingMethodException: No signature of method: PipelineMethods.build() is applicable for argument types: (java.util.LinkedHashMap) values: [[job:test_1, propagate:false]]
然后詹金斯提示我groovy.lang.MissingMethodException: No signature of method: PipelineMethods.build() is applicable for argument types: (java.util.LinkedHashMap) values: [[job:test_1, propagate:false]]
I can understand that build()
method is from Jenkins Pipeline Build Steps Plugins, but I failed to import it and use it inside that commonMethods
library (this local library I just use load ()
method in the very first phase of my pipeline script.我可以理解build()
方法来自 Jenkins Pipeline Build Steps Plugins,但我未能导入它并在commonMethods
库中使用它(这个本地库我只是在我的管道脚本的第一阶段使用load ()
方法。
So my question is所以我的问题是
Thanks, everybody!谢谢大家!
not sure if it runnable and looks clearer but i just tried to put all together from question and comments不确定它是否可以运行并且看起来更清晰,但我只是尝试将所有问题和评论放在一起
//function that returns closure to be used as one of parallel jobs
def buildSingleJobParallel(steps, mjob){
return {
def result = steps.build job: mjob.fullName, propagate: false
steps.echo "${mjob.fullName} finished: ${steps.result.rawBuild.result}"
if (result.rawBuild.result.isWorseThan(Result.SUCCESS)) {
steps.currentBuild.result = 'FAILURE'
}
}
}
stage('Integration Test Run') {
steps {
script {
//build map<jobName, Closure> and run jobs in parallel
parallel matchingJobs.collectEntries{mjob-> [mjob.name, buildSingleJobParallel(this, mjob)]}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.