【问题标题】:How to handle failed jenkins build jobs with automated python script如何使用自动化 python 脚本处理失败的詹金斯构建作业
【发布时间】:2021-04-12 20:08:43
【问题描述】:

我编写了一个 Python 脚本来启动我们所有微服务的构建工作。我正在做一些测试,并希望确保我能处理在 deploy.jenkins 文件中某处发生错误并失败的作业。

我基本上只是为这个部署构建脚本创建了另一个管道作业,从 jenkins 手动调用它,然后它从 github 签出,构建我的 docker 容器,然后启动我想要部署的所有微服务。

所以现在为了测试,我在微服务中添加了一个 sh "exit 1" 以使其中一项作业失败,当它失败时,它也会使我的整个部署作业脚本失败。其他微服务继续构建,但我的脚本还在它们启动后轮询/跟踪每个作业,直到它们完成,目标是在未来编写一个带有状态的团队 webhook,所以我错过了所有这些一旦脚本由于一次构建失败而退出。

在不终止我的部署脚本以使其不会退出并继续轮询其他构建作业的情况下处理失败作业的最佳方法是什么?

【问题讨论】:

    标签: python jenkins jenkins-pipeline


    【解决方案1】:

    我想我已经明白了,这就是你想要实现的:

    我有一个主管道作业 stackoverflow,还有三个下游管道作业 test1test2test3(如果是你的微服务作业)

    让我们通过添加sh "exit 1" 使作业test1 失败,并将其捕获到主管道中,但不管test1 作业失败,其余所有test2test3 都应该运行。

    这是主管道作业的声明性管道代码:

    pipeline {
        agent any
        stages {
            stage('simple stage') {
                steps {
                    sh 'exit 0'
                }
            }
            stage('test1 job') {
                steps {
                    catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    build propagate: true, job: 'test1'
                }
              }
            }
            stage('test2 job') {
                steps {
                    build propagate: true, job: 'test2'
                }
            }
            stage('test3 job') {
                steps {
                    build propagate: true, job: 'test3'
                }
            }
        }
    }
    

    其他下游作业的流水线代码:

    1. test1 管道作业
    pipeline {
        agent any
        stages {
            stage('1') {
                steps {
                    sh 'exit 1'
                }
            }
            
        }
    }
    
    1. test2 管道作业
    pipeline {
        agent any
        stages {
            stage('1') {
                steps {
                    sh 'exit 0'
                }
            }
            
        }
    }
    
    1. test3 管道作业
    pipeline {
        agent any
        stages {
            stage('1') {
                steps {
                    sh 'exit 0'
                }
            }
            
        }
    }
    

    当我们运行主管道作业时,我们得到的结果如下:

    控制台输出

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      相关资源
      最近更新 更多