Jenkins 嵌套阶段管道中的条件执行
conditional execution in Jenkins nested stages pipelines
我已经看过 并且我对这个问题有所了解:如果我们有一个简化的工作流程,事情会是什么样子
顺序模式下的 A-BC-{B1-B2-B3}-D?
我想要实现的是只有当 B1 成功时才执行阶段 B2,并且 B3 应该在 B1 失败的条件下执行,最后继续 D。
感谢您的输入。
以下对我有用 - 在异常阶段定义一个额外的映射
stageResultMap.ObjectdircreationFailed = true
并调用它在失败时应调用的阶段。示例如下。
stage ('Build'){
stages {
stage ('Objectdircreation'){
steps {
script {
try {
sh'''#!/usr/bin/env bash
PATH=/sdev/user/bin:/opt/rational/clearcase/bin:$PATH
export PATH
mkdir -p $OBJECTDIR
'''
stageResultMap.didObjectdircreationSucceed = true
}
catch (Exception e) {
unstable("${STAGE_NAME} failed!")
currentBuild.result = 'FAILURE'
stageResultMap.didObjectdircreationSucceed = false
//set an extra map to true and test it
stageResultMap.ObjectdircreationFailed = true
}
}
}
} // end of Objectdircreation stage
stage ('Inject objects under an objdir'){
//
// execute only if Objectdircreation succeeded
//
when {
expression {
return stageResultMap.find{ it.key == "didObjectdircreationcreationSucceed" }?.value
}
}
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE')
{
sh """#!/usr/bin/env bash
make ${ARGS} $OBJDIRPREFIX=$OBJDIR build
"""
}
}
} // end of build under an object tree
stage ('Inject build under current objdir') {
//
// execute only if object dir creation failed
//
when {
expression {
return stageResultMap.find{ it.key == "didProjecttreecreationFailed" }?.value
}
}
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
sh """#!/usr/bin/env bash
make ${ARGS} build
"""
}
}
} // end of build under root
} // end of internal build stages
//
} // end of build nested stages
} // end of main stages section
//
//
} //pipeline end
我已经看过
以下对我有用 - 在异常阶段定义一个额外的映射
stageResultMap.ObjectdircreationFailed = true
并调用它在失败时应调用的阶段。示例如下。
stage ('Build'){
stages {
stage ('Objectdircreation'){
steps {
script {
try {
sh'''#!/usr/bin/env bash
PATH=/sdev/user/bin:/opt/rational/clearcase/bin:$PATH
export PATH
mkdir -p $OBJECTDIR
'''
stageResultMap.didObjectdircreationSucceed = true
}
catch (Exception e) {
unstable("${STAGE_NAME} failed!")
currentBuild.result = 'FAILURE'
stageResultMap.didObjectdircreationSucceed = false
//set an extra map to true and test it
stageResultMap.ObjectdircreationFailed = true
}
}
}
} // end of Objectdircreation stage
stage ('Inject objects under an objdir'){
//
// execute only if Objectdircreation succeeded
//
when {
expression {
return stageResultMap.find{ it.key == "didObjectdircreationcreationSucceed" }?.value
}
}
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE')
{
sh """#!/usr/bin/env bash
make ${ARGS} $OBJDIRPREFIX=$OBJDIR build
"""
}
}
} // end of build under an object tree
stage ('Inject build under current objdir') {
//
// execute only if object dir creation failed
//
when {
expression {
return stageResultMap.find{ it.key == "didProjecttreecreationFailed" }?.value
}
}
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
sh """#!/usr/bin/env bash
make ${ARGS} build
"""
}
}
} // end of build under root
} // end of internal build stages
//
} // end of build nested stages
} // end of main stages section
//
//
} //pipeline end