Java 我是否应该在阶段中重新抛出异常';什么是挡块?(脚本化管道文件)
我的脚本管道如下所示:Java 我是否应该在阶段中重新抛出异常';什么是挡块?(脚本化管道文件),java,jenkins,groovy,jenkins-pipeline,jenkins-groovy,Java,Jenkins,Groovy,Jenkins Pipeline,Jenkins Groovy,我的脚本管道如下所示: 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) {
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中抛出错误,管道会立即退出吗
文件非常不清楚,在某些情况下相互矛盾 如果要停止管道,则应引发异常,如果需要自定义错误消息,则可使用
错误
管道函数
见:
echo“Gradle Clean and Test failed:${err.getMessage()}”
将把错误消息放在日志中,以便以后检索
在Gradle Clean And Test期间,
error“失败…检查日志”
将发送一个错误,由节点的全局捕获块捕获,如果在最终捕获块中使用err.message
,则可以很容易地在松弛消息中看到该错误。如果要停止管道,则应抛出异常,如果需要自定义错误消息,可以使用error
管道功能
见:
echo“Gradle Clean and Test failed:${err.getMessage()}”
将把错误消息放在日志中,以便以后检索
和
错误“梯度清理和测试期间失败…检查日志”
将发送一个错误,由节点的全局catch块捕获,如果在final catch块中使用err.message
,则可以很容易地在Slack消息中看到该错误。try catch final将捕获由stage生成的所有错误,但您不知道错误是在哪个阶段生成的。如果您想知道您应该抛出一个自定义错误,例如“运行测试失败,请阅读日志…”,抛出异常将退出管道。try-catch final将捕获由阶段生成的所有错误,但您不知道在哪个阶段生成错误。如果您想知道您应该抛出一个自定义错误,例如error“runtests Failed,please read logs…”
,抛出异常将退出管道。
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')