gpt4 book ai didi

jenkins - 在Jenkinsfile中的哪里放置try/catch

转载 作者:行者123 更新时间:2023-12-03 17:35:38 29 4
gpt4 key购买 nike

在哪里放置try/catch,使其可以按预期工作,特别是在存在并行分支的情况下工作? (此外,还有蓝海插件)

在有关Jenkinsfile的官方文档中,没有明确地与此主题相关的内容,但确实存在示例:

示例1:尝试在阶段块内

Jenkinsfile (Scripted Pipeline)
node {
stage('Example') { //It's inside the stage block
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed, I should sound the klaxons!'
throw
}
}
}

示例2:尝试在节点块内部
Jenkinsfile (Scripted Pipeline)
stage('Build') {
/* .. snip .. */
}

stage('Test') {
parallel linux: {
node('linux') {
checkout scm
try {
unstash 'app'
sh 'make check'
}
finally {
junit '**/target/*.xml'
}
}
},
windows: {
node('windows') {
/* .. snip .. */
}
}
}

因此,就我而言,它是否以此方式工作(这是最外面的街区)?基本上,这是一个嵌套的并行构建。我观察到,有时当其中一个分支发生故障时,整个构建会继续进行,并且管线图仍然在蓝海中为绿色 :(如果try/catch这样工作,那么我可以尝试其他可能性)
try{
parallel 'b0': {
node('parallel'){
....
}
}, 'b1': {
node('parallel'){
....
}
}, 'b2': {
parallel 'b2-0': {
node('parallel'){
....
}
}, 'b2-1': {
node('parallel'){
....
}
}, failFast: true

parallel 'anotherb0': {
node('parallel'){
.....
}
}, 'anotherb1': {
node('parallel'){
....
}
}, failFast: true
}, failFast: true
}catch(err){
print err
currentBuild.result = 'FAILURE'
}finally{
}

最佳答案

建议使用声明性管道而不是脚本化管道。

您将专注于编写要使用的每个插件提供的特定管道语法支持。每个人都有相似但特定的语法以实现其特定目的。

也就是说,有时您必须在脚本块中编写少量脚本。我从来没有尝试过,如果您遵循每个插件提供的语法支持(应该在插件站点上分别为每个文档提供文档说明),您将永远不需要这样做。

pipeline {
stages {
stage ("Do Something") {
steps {
bat "..."
script {
//Code that requires a script tag to be present if it can't be done by a plugin's native pipeline support.
}
}
}
}
}

关于jenkins - 在Jenkinsfile中的哪里放置try/catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48331473/

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