如何根据脚本退出代码将 Jenkins 管道阶段设置为不稳定?
How to set a Jenkins pipeline stage to unstable based on a script exit code?
我的 Jenkins 管道中的一个阶段应设置为不稳定,具体取决于其步骤中脚本的退出代码:2 应将阶段状态设置为不稳定,而 1 应将阶段结果设置为失败。
如何实现?我检查了“catchError”,但它似乎没有区分失败状态,只提供了一种捕获非 0 退出 (1,2...) 的方法。
pipeline {
agent any
parameters {
string(name: 'FOO', defaultValue: 'foo', description: 'My foo param')
string(name: 'BAR', defaultValue: 'bar', description: 'My bar param')
}
stages {
stage('First') {
steps {
// If "script.py" exit code is 2, set stage to unstable
// If "script.py" exit code is 1, set stage to failed
sh """
. ${WORKSPACE}/venv/bin/activate
python ${WORKSPACE}/script.py --foo ${FOO} --bar ${BAR}
deactivate
"""
}
}
}
stage('Second') {
steps {
echo('Second step')
}
}
}
}
解决方案
您将需要执行以下步骤
- 将所有代码放入
script
块
- 修改
sh
为return状态码
- 使用条件检查状态码
- 抛出并捕获
catchError
的错误。相应地将构建设置为 SUCCESS 并将阶段设置为 FAILURE 或 UNSTABLE
I am using error
to force the exception but you can also use sh 'exit 1'
or throw new Exception('')
pipeline {
agent any
parameters {
string(name: 'FOO', defaultValue: 'foo', description: 'My foo param')
string(name: 'BAR', defaultValue: 'bar', description: 'My bar param')
}
stages {
stage('First') {
steps {
script {
def pythonScript = """
. ${WORKSPACE}/venv/bin/activate
python ${WORKSPACE}/script.py --foo ${FOO} --bar ${BAR}
deactivate
"""
def statusCode = sh( script:pythonScript, returnStatus:true )
if(statusCode == 2) {
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
error 'STAGE UNSTABLE'
}
} else if(statusCode == 1) {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
error 'STAGE FAILURE'
}
}
}
}
}
}
}
我的 Jenkins 管道中的一个阶段应设置为不稳定,具体取决于其步骤中脚本的退出代码:2 应将阶段状态设置为不稳定,而 1 应将阶段结果设置为失败。
如何实现?我检查了“catchError”,但它似乎没有区分失败状态,只提供了一种捕获非 0 退出 (1,2...) 的方法。
pipeline {
agent any
parameters {
string(name: 'FOO', defaultValue: 'foo', description: 'My foo param')
string(name: 'BAR', defaultValue: 'bar', description: 'My bar param')
}
stages {
stage('First') {
steps {
// If "script.py" exit code is 2, set stage to unstable
// If "script.py" exit code is 1, set stage to failed
sh """
. ${WORKSPACE}/venv/bin/activate
python ${WORKSPACE}/script.py --foo ${FOO} --bar ${BAR}
deactivate
"""
}
}
}
stage('Second') {
steps {
echo('Second step')
}
}
}
}
解决方案
您将需要执行以下步骤
- 将所有代码放入
script
块 - 修改
sh
为return状态码 - 使用条件检查状态码
- 抛出并捕获
catchError
的错误。相应地将构建设置为 SUCCESS 并将阶段设置为 FAILURE 或 UNSTABLE
I am using
error
to force the exception but you can also usesh 'exit 1'
orthrow new Exception('')
pipeline {
agent any
parameters {
string(name: 'FOO', defaultValue: 'foo', description: 'My foo param')
string(name: 'BAR', defaultValue: 'bar', description: 'My bar param')
}
stages {
stage('First') {
steps {
script {
def pythonScript = """
. ${WORKSPACE}/venv/bin/activate
python ${WORKSPACE}/script.py --foo ${FOO} --bar ${BAR}
deactivate
"""
def statusCode = sh( script:pythonScript, returnStatus:true )
if(statusCode == 2) {
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
error 'STAGE UNSTABLE'
}
} else if(statusCode == 1) {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
error 'STAGE FAILURE'
}
}
}
}
}
}
}