12

如果控制台输出上出现模式,是否有插件使构建失败?

例如:

Build Action - success
Build Action - success
Build Action - error_pattern
Build Action - success

让我们假设 Jenkins 构建过程不会在 error_pattern 上构建失败,我需要有某种外部失败触发器。

编辑

寻找在构建过程中失败的解决方案,而不是构建后任务。

4

3 回答 3

9

您应该尝试Post Build Task插件。您可以搜索模式,然后启动脚本。

编辑:还有文本查找器插件,看起来更适合您的问题

于 2012-12-10T15:04:17.490 回答
1

我已将此答案用作管道脚本的基础。在“构建”阶段,我有两个并行的子阶段 - 其中一个正在执行实际构建并输出到日志,而另一个子阶段正在获取相同的日志。我已经否定了退出代码 ( ! grep),以便在找到“错误:”字符串时它会出错。由于failFast设置,一旦找到字符串,这将导致整个“构建”阶段失败。grep在第一个子阶段的末尾有一个额外的,以防在最后产生错误。

我正在使用变量 ( BUILD_COMPLETE) 跟踪构建状态。

pipeline {
    agent {
        label 'master'
    }
    environment {
        BUILD_COMPLETE = false
    }
    stages {
        stage('Build') {
            failFast true
            parallel {
                stage('Building') {
                    steps {
                        sh 'echo foo | tee output.log'
                        sleep 5
                        sh 'echo bar | tee -a output.log'
                        sleep 5
                        sh 'echo ERROR: baz | tee -a output.log'
                        sleep 5
                        sh 'echo qux | tee -a output.log'
                        sleep 5

                        sh '! grep "^ERROR:" output.log'

                        script {
                            BUILD_COMPLETE = true
                        }
                    }
                }
                stage('Monitoring the logs') {
                    steps {
                        script {
                            while (BUILD_COMPLETE != true) {
                                sh '! grep "^ERROR:" output.log'
                                sleep 1
                            }
                        }
                    }
                }
            }
        }
    }
}
于 2019-09-12T09:18:23.443 回答
1

As an ugly workaround I do the following: in the build script redirect all the output to some resulting .log file, then you can grep through this file in the background the way you like (personally I do the freezing check additionally - calculate the checksum and compare with previous, if the same - start counting for timeout until threshold), etc...

Disadvantage is the output goes to some file instead of Jenkins console, but I guess you can do both using tee (I don't care, because my goal is to archive the log anyways and send it via email, - so I just gzip my resulting .log file and attach it as an artifact to the build record + to the email).

Advantage is you have full control on what happens in the build output and can interrupt the build using your own return code / message.

于 2017-07-28T12:47:14.287 回答