groovy - 如何检查方法是否在 Groovy 中返回非零退出代码
问题描述
我有一个 Jenkinsfile 项目,它要求我包含一个'if statement
以确定某些方法中的 shell 命令是否返回0
.
第一种方法method 1
按预期工作。但是我想包括一个if statement
跳过第二阶段,因为该 shell 命令method 2
不会以0
.
def method1(setup){
sh """
echo ${setup}
"""
}
def method2(setup){
sh """
ech ${setup}
"""
}
node {
stage('print method1'){
method1('paul')
}
// I need an if statement to skip this method since the if statement returns non=zero
stage('This method should be skipped'){
if(method2 returns != 0) //This if condition fails but need something similar to this
method1('paul')
}
}
对此的任何帮助都非常感谢。
解决方案
您在示例中使用默认sh
步骤执行,这意味着该命令不会返回退出代码。如果存在状态不是0
这种情况,则管道将失败并出现异常。如果要返回退出状态并允许管道继续,则必须传递returnStatus: true
选项,例如:
int status = sh(script: """
echo ${setup}
""", returnStatus: true)
if (status != 0) {
// do something
}
来源:sh
步骤管道文档
推荐阅读
- android - Android BluetoothSocket.isConnected 不起作用
- javascript - Javascript 如何在 Textarea 表单字段中获取准确的字符数
- java - Jackson objectMapper 反序列化为 TimeZone
- linux - 如何在 open-ssh 和 dropbear 之间匹配 ECDSA 指纹
- javascript - 带有功能反应按钮的文字游戏
- angular - ngrx 文档参考分辨率
- r - 闪亮 - 带有动画传单的传奇
- python - 将新值插入表时出现错误“列计数与第 1 行的值计数不匹配”
- git - 无法“git push”到我自己的分支?“远程包含您在本地没有的工作”
- r - 使用 R 将大量文件从一个大文件夹移动到几个较小的文件夹