目前,我在Jenkins中定义了两个阶段,因为它们都需要不同的代理。
这是我的概念代码证明
stages {
stage("Run Tests"){
agent docker
stage("Do setup"){}
stage("Testing") {}
post{
always{
echo "always"
}
failure {
echo "failure"
}
}
}
stage("Generate Reports"){
agent node-label
stage("Generate"){
}
}
}
我需要不同代理的“生成报告”,因为某些二进制文件位于节点上,而不是在docker容器中。测试运行在节点上的docker共享卷中,因此我获得在节点上生成报告所需的所有工件。
(我曾尝试在后期运行“生成报告”,但它似乎以某种方式在码头容器中运行。)
现在,如果“运行测试”失败,则会跳过"Generate“,因为以前的阶段失败了。任何知道我如何可以强制“生成报告”阶段运行,即使是在前一个阶段的失败。
发布于 2021-06-01 21:25:35
下面是管道。
pipeline {
agent none
stages {
stage("Run Tests"){
agent { label "agent1" }
stages {
stage("Do setup"){
steps{
sh 'exit 0'
}
}
stage("Testing") {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh "exit 1"
} //catchError
} //steps
post{
always{
echo "always"
}
failure {
echo "failure"
}
} // post
} // Testing
} // stages
} // Run Test
stage("Generate Reports"){
agent { label "agent2" }
steps {
sh 'exit 0'
}
} // Reports
}
}
管道是成功的,但是阶段测试显示为失败,您可以选择buildResult和stageResult的状态,以防您希望它不稳定或失败:
发布于 2021-06-01 22:00:50
如果希望始终运行"Generate“阶段,那么如果出现故障,可以将早期阶段标记为unstable
。
通过这种方式,Jenkins将执行所有阶段,并且不会在特定阶段停止错误。
示例:
stages {
stage("Run Tests"){
agent docker
stage("Do setup"){}
stage("Testing") {
steps {
script {
// To show an example, Execute "set 1" which will return failure
def result = bat label: 'Check bat......', returnStatus: true, script: "set 1"
if (result !=0) {
// If the result status is not 0 then mark this stage as unstable to continue ahead
// and all later stages will be executed
unstable ('Testing failed')
}
}
}
}
}
}
stage("Generate Reports"){
agent node-label
stage("Generate"){
}
}
}
选项2,如果不想通过返回状态处理,可以使用try and catch block
stage("Testing") {
steps {
script {
try {
// To show an example, Execute "set 1" which will return failure
bat "set 1"
}
catch (e){
unstable('Testing failed!')
}
}
}
}
选项3:无论阶段失败,您都可以将返回状态更改为完整构建的成功。
stage("Testing") {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
script {
// To show an example, Execute "set 1" which will return failure
bat "set 1"
}
}
}
注意::选项3有一个缺点,如果有任何错误,它将不会在同一阶段执行进一步的步骤。
示例:
在本例中,由于bat set 1
命令中存在问题,将不会执行打印消息“测试阶段”。
stage("Testing") {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
script {
// To show an example, Execute "set 1" which will return failure
bat "set 1"
// Note : Below step will not be executed since there was failure in prev step
println "Testing stage"
}
}
}
选项4:您已经尝试过将生成报告阶段始终保存在“邮报”中
段,以便始终执行,而不考虑任何失败。
https://stackoverflow.com/questions/67798958
复制