Jenkins 将任何非零退出代码视为错误
Jenkins treats any non-zero exit code as error
这是一个问题,因为我们有一个可执行文件 returns 2 作为警告。我们不想仅仅因为这个而使 Jenkins 构建管道失败。我们如何修改管道以接受退出代码 2,并根据退出代码打印出合理的警告消息?
D:\Stage>c:\bin\mycommand
script returned exit code 2
当您在 Jenkins 管道中 运行 sh
或 bat
时,对于任何非零退出代码,它总是会导致构建失败(并抛出异常)——这不可能改变了。
您可以做的是使用 sh step(或 cmd)的 returnStatus
,这将 return 脚本的退出代码而不是构建失败,然后您可以使用类似的东西:
pipeline {
agent any
stages {
stage('Run Script') {
steps {
script {
def exitCode = sh script: 'mycommand', returnStatus: true
if (exitCode == 2) {
// do something
}
else if (exitCode){
// other non-zero exit codes
}
else {
// exit code 0
}
}
}
}
}
}
此方法的唯一缺点是 returnStatus
不能与 returnStdout
一起使用,因此如果您需要获得 returned 输出,则需要将其放入另一种方式(例如写入文件然后读取它)。
这是一个问题,因为我们有一个可执行文件 returns 2 作为警告。我们不想仅仅因为这个而使 Jenkins 构建管道失败。我们如何修改管道以接受退出代码 2,并根据退出代码打印出合理的警告消息?
D:\Stage>c:\bin\mycommand
script returned exit code 2
当您在 Jenkins 管道中 运行 sh
或 bat
时,对于任何非零退出代码,它总是会导致构建失败(并抛出异常)——这不可能改变了。
您可以做的是使用 sh step(或 cmd)的 returnStatus
,这将 return 脚本的退出代码而不是构建失败,然后您可以使用类似的东西:
pipeline {
agent any
stages {
stage('Run Script') {
steps {
script {
def exitCode = sh script: 'mycommand', returnStatus: true
if (exitCode == 2) {
// do something
}
else if (exitCode){
// other non-zero exit codes
}
else {
// exit code 0
}
}
}
}
}
}
此方法的唯一缺点是 returnStatus
不能与 returnStdout
一起使用,因此如果您需要获得 returned 输出,则需要将其放入另一种方式(例如写入文件然后读取它)。