【Pipeline】Jenkins流水线parallel并行构建
位于parallel块下的阶段都将并行执行,而且并行阶段还可以被分到不同的Jenkins agent上执行。
在默认情况下,Jenkins pipeline要等待parallel块下所有的阶段都执行完成,才能确定结果。如果希望所有并行阶段中的某个阶段失败后,就让其他正在执行的阶段都中止,那么只需要在与parallel块同级的位置加入failFast true就可以了。
并行步骤
pipeline {
agent any
stage("Parallel Build And Test") {
failFast true // true 设置并发任务一旦有一个失败,其他并行任务都终止
parallel {
// stage("1.Xxxx Build and Test") 和 stage("2.XxxBuild and Test111")是并行
stage("1.Xxxx Build and Test"){
stages {
// stage("1.1 Xxx Build")和stage('1.2 Xxxxtest Test')是串行
stage("1.1 Xxx Build"){
steps{
script{ //启动的子job的名称、参数设置
downstreamBuild = build job: 'xxxxx', parameters: [
string(name: 'xxxx', value: env.xxxx),
string(name: 'xxxx', value: 'xxxx')],
wait: true
// 获取启动的子job的构建号,方便精准的获取构建产物
def downstreamBuildNumber = downstreamBuild.getNumber()
// buildNumber: downstreamBuildNumber.toString()这里需要string类型的
copyArtifacts filter: '*.zip, *.tgz', projectName: 'xxxx', selector: [$class: 'SpecificBuildSelector', buildNumber: downstreamBuildNumber.toString()]
} // end script
} // end steps
} // end stage("1.1 Xxx Build")
// stage("1.1 Xxx Build")和stage('1.2 Xxxxtest Test')是串行
stage('1.2 Xxxxtest Test') {
agent { label 'xxxx' }
steps {
cleanWs() //debug
} //steps
} //end stage('1.2 Xxxxtest Test')
} // end stages
}// end stage("1.Xxxx Build and Test")
// stage("1.Xxxx Build and Test") 和 stage("2.XxxBuild and Test111")是并行
stage("2.XxxBuild and Test111"){
stages {
stage("2.1 xxx Build"){
steps{
script{
downstreamBuild = build job: 'xxxx', parameters: [
string(name: 'xxxx', value: env.xxxx),
string(name: 'xxxxx', value: 'xxxxx')],
wait: true
} // end script
} // end steps
} // end stage("2.1 xxx Build")
stage('2.2 Test111') {
agent { label 'xxx' }
steps {
cleanWs() //debug
} //steps
post {
failure {
script {
echo "xxxxx"
}
}
success {
script {
echo "xxxxx"
}
}
} // end post
} //end stage('2.2 Test111')
} // end stages
}// end stage("2.XxxBuild and Test111")
} // end parallel
} // end stage("Parallel Build And Test")
} // end pipeline