jenkins - 为什么詹金斯的工作没有失败 - 即使 npm 脚本在阶段失败?
问题描述
我有以下 Jenkinsfile:
stage('build mod') {
steps {
dir("cli") {
script {
try {
sh('node index.js -m shell -h project -p max4 -i local')
} catch (Exception e) {
currentBuild.result = 'FAILURE'
throw e
}
}
}
}
}
脚本失败:
ERROR Build failed with errors.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/jenkins/.npm/_logs/-debug.log
但是阶段以绿色通过......为什么在这种情况下try-catch不起作用?
解决方案
首先,您需要了解,即使脚本内部的代码失败,jenkinsfile 调用的 shell 函数也是成功的。这就是 jenkinsfile 在脚本返回 0 时无法捕获错误的原因。因此您需要编写一个代码来捕获在脚本内部执行的代码的返回值。类似的东西
result = sh (
script: "node index.js -m shell -h project -p max4 -i local",
returnStatus: true
)
if (result != 0) {
currentBuild.result = 'FAILURE'
break
}
这里的“break”非常重要,因为我们不希望在构建失败后执行其他阶段。
推荐阅读
- ibm-cloud - 在 dropsolve 中带有回调的 docplex
- javascript - 使用地图获取属性是否禁用
- if-statement - 我不想写 60 个 'IF' 命令
- python - TypeError:将形状转换为 TensorShape 时出错:int() 参数必须是字符串或数字,而不是“元组”
- html - 使用 VB.Net 将 Excel 转换为 HTML
- e-commerce - 如何创建 Commerce Repeat Order 链接视图?
- mysql - Laravel 多连接计数
- python - 要锁定并用于运行余额计算的数据框单元格(后续问题)
- go - 与任意切片一起使用的交换实现
- php - 如何在 php 中的 cURL 响应中创建对象的 json