Naz*_*han 3 jenkins-pipeline jenkins-declarative-pipeline
我需要让它在遵守以下条件的情况下工作。帮助我定义何时使用块和其他代码以及在哪个阶段使用?
我尝试并观察到了什么? 从上面定义的条件来看,1 和 2 按预期工作,但在我的以下尝试中没有 3 和 4。
在C1和C2,我加了catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')指继续詹金斯管道失败过舞台。
但我观察到的是——
这是你需要的:
stageResultMap = [:]
pipeline {
agent any
stages {
stage('A') {
steps {
println("This is stage: ${STAGE_NAME}")
}
}
stage('BC') {
parallel {
stage ('1'){
stages {
stage('B1') {
steps {
script {
// Catch exceptions, set the stage result as unstable,
// build result as failure, and the variable didB1Succeed to false
try {
sh "exit 1"
stageResultMap.didB1Succeed = true
}
catch (Exception e) {
unstable("${STAGE_NAME} failed!")
currentBuild.result = 'FAILURE'
stageResultMap.didB1Succeed = false
}
}
}
}
stage('C1') {
// Execute only if B1 succeeded
when {
expression {
return stageResultMap.find{ it.key == "didB1Succeed" }?.value
}
}
steps {
// Mark the stage and build results as failure on error but continue pipeline execution
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
sh "echo Hello"
}
}
}
}
}
stage ('2'){
stages {
stage('B2') {
steps {
script {
// Catch exceptions, set the stage result as unstable,
// build result as failure, and the variable didB2Succeed to false
try {
sh "echo Hello"
stageResultMap.didB2Succeed = true
}
catch (Exception e) {
unstable("${STAGE_NAME} failed!")
currentBuild.result = 'FAILURE'
stageResultMap.didB2Succeed = false
}
}
}
}
stage('C2') {
// Execute only if B2 succeeded
when {
expression {
return stageResultMap.find{ it.key == "didB2Succeed" }?.value
}
}
steps {
// Mark the stage and build results as failure on error but continue pipeline execution
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
sh "echo Hello"
}
}
}
}
}
}
}
stage('D') {
// Execute only when C1 or C2 have executed, that is B1 or B2 have succeeded
when {
expression {
return stageResultMap.any {it.value}
}
}
steps {
println("This is stage: ${STAGE_NAME}")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE')将阶段和构建结果标记为 FAILURE 但继续管道执行。stageResultMap = [:]在 Jenkinsfile 顶部初始化一个空映射以捕获每个阶段的结果。现在,对于每个Bn阶段,使用一个try-catch块,以便在成功时将唯一的键值对stageResultsMap.didBnSucceed = true添加到映射中,并且在异常情况下,阶段结果设置为 UNSTABLE,构建结果设置为 FAILURE,然后stageResultsMap.didBnSucceed = false。我们在unstable这里使用该方法是因为没有其他两种方法catchError或warnError让我们添加到地图中。然后,在相应的 Cn 阶段,评估映射并仅在 Bn 成功时才执行。此外,如果 B1 和 B2 都失败,即 C1 和 C2 都没有执行,D 也不会执行,因为when {expression {return stageResultMap.any {it.value}}}将评估为false。但是,如果 C1 或 C2 执行而不管失败,D 都会执行。在上述两种情况下,如果任何阶段失败,整体构建状态将被标记为 FAILURE。
当然,绿色构建没有失败。
| 归档时间: |
|
| 查看次数: |
3062 次 |
| 最近记录: |