简体   繁体   English

Jenkins管道:无法在构建控制台中找到基于字符串的构建

[英]Jenkins pipeline: fail build on string found in build console

Using jenkins pipeline, I want to fail build when a word (for example, "FATAL") is found in build log. 使用jenkins管道,当在构建日志中找到一个单词(例如“ FATAL”)时,我想使构建失败。

There is a Text Finder plugin that can do it in freestyle jobs, but one cannot use it in pipelines. 有一个Text Finder插件可以在自由式作业中执行此操作,但不能在管道中使用它。

You can use the Log Parser Plugin which also support Jenkins Pipeline jobs. 您可以使用Log Parser插件 ,该插件也支持Jenkins Pipeline作业。 It's working for us. 它为我们工作。

Global Rules 全球规则

We configured some global rules in Manage Jenkins->Configure System->Console Output Parsing . 我们在Manage Jenkins->Configure System->Console Output Parsing配置了一些全局规则。 The files configured there are placed at a location accessible to the Jenkins Master, eg 在那里配置的文件放置在Jenkins Master可以访问的位置,例如

# /home/jenkins/default.rules (on the jenkins master machine)
error /ERROR/

Eg having a rule set called 'default' which refers to /home/jenkins/default.rules' you could use the following (the parsingRulesPath you can get using the snippet generator selecting default` rules): 例如,有一个名为“默认”的规则集,它引用/home/jenkins/default.rules' you could use the following (the you can get using the snippet generator selecting默认规则you can get using the snippet generator selecting parsingRulesPath):

echo "ERROR: Some error"
node {
    step([$class: 'LogParserPublisher',
        failBuildOnError: true,
        parsingRulesPath: '/home/jenkins/default.rules',
        useProjectRule: false])
}

Project Rules 项目规则

Turns out that there's no need to have a global configuration entry. 原来,不需要全局配置条目。 You may also use rules located in your workspace, like: 您还可以使用工作空间中的规则,例如:

echo "ERROR: Some error"
node {
    // Usually I assume a project rules file will be stored in
    // some repository and are not generated on the fly
    writeFile(file: 'project.rules', text:
'''
error /ERROR/
''')
    step([$class: 'LogParserPublisher',
        failBuildOnError: true,
        projectRulePath: 'project.rules',
        useProjectRule: true])
}

Although the examples above are for scripted pipeline it should be easy to apply them to declarative pipeline as well. 尽管上面的示例是针对脚本化管道的,但也应该很容易将它们应用于声明性管道。

This works 这有效

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh '''
                    echo Blabla
                    echo FATAL error
                '''
            }
        }

        stage('Results') {
            steps {
                script {
                    def logz = currentBuild.rawBuild.getLog(10000);
                    def result = logz.find { it.contains('FATAL') }
                    if (result) {
                        error ('Failing due to ' + result)
                    }
                }
            }
        }
    }
}

But requires several script approvals from Jenkins admins. 但需要获得詹金斯(Jenkins)管理员的一些脚本批准。

And there is an approval request that states "Approving this signature may introduce a security vulnerability! You are advised to deny it. " 并且有一个批准请求,其中指出“批准此签名可能会引入安全漏洞! 建议您拒绝它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM