gpt4 book ai didi

jenkins - 我何时以及为什么应该在 Jenkinsfiles catch block 中抛出错误? (脚本化管道)

转载 作者:行者123 更新时间:2023-12-02 09:35:10 25 4
gpt4 key购买 nike

我的脚本管道看起来像这样:

Node {
try {


// If tests fail, I want to report the JUNIT results and skip the rest of the pipeline
stage('Run Tests') {
try {
sh './gradlew test'
} catch (err) {
currentBuild.result = 'FAILURE'
echo "Caught err: ${err}"
throw err // Should I throw this error? Will it prevent anything? Do I need it? Is it good to have?
}
}

stage('Build Jar') {
try {
sh './gradlew fatJar'
} catch (err) {
currentBuild.result = 'FAILURE'
echo "Caught err: ${err}"
throw err // Same with this one
}
}

stage('Deploy') {
try {
echo "// if success, deploy"
} catch (err) {
currentBuild.result = "FAILURE"
echo "Error deploying, Caught: ${err}"
throw err // Do I need to rethrow this?
}

}

} catch (err) {
echo "${err}"
throw err // Will this catch all errors? Will I be missing stacktraces?
} finally {
junit '**/TEST-*.xml'
archiveArtifacts artifacts: '**/TEST-*.xml, **/build/libs/*.jar', fingerprint: true
} // end global try/catch
} // end node

我想知道什么时候应该在阶段的 try/catch 中抛出错误,以及对于某些事情(部署)是否需要这种错误,或者对于其他事情(测试)是否应该避免这种情况。

如果我在 try/catch 中抛出错误,管道会立即退出吗?

文档非常不清楚,并且在某些情况下是矛盾的。

最佳答案

如果您想要停止管道,则应该抛出异常,如果您想要自定义错误消息,则可以使用 error 管道函数。

参见:

            stage('Gradle Clean and Test') {
try {
sh './gradlew clean'
sh './gradlew test --tests "*validateFiles*"'
sh 'exit 1' // purposefully throwing this for demonstration
} catch (err) {
currentBuild.result = 'FAILURE' // Set to Failure
echo "Gradle Clean and Test failed: ${err.getMessage()}" // Print Error Log Message
error "Failed during Gradle Clean and Test... Check Logs" // Throw new error to quit out of pipeline, and give a custom message for final catch block
}
} // end stage('Gradle Clean and Test')

echo "Gradle Clean and Test failed: ${err.getMessage()}" 会将错误消息放入日志中,以供以后检索

并且 错误“Gradle Clean 和 Test 期间失败...检查日志” 将发送一个错误,由节点的全局 catch block 捕获,并且如果您使用,可以在 Slack 消息中轻松看到err.message 在最终的 catch block 中。

关于jenkins - 我何时以及为什么应该在 Jenkinsfiles catch block 中抛出错误? (脚本化管道),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57580021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com