繁体   English   中英

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

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

使用jenkins管道,当在构建日志中找到一个单词(例如“ FATAL”)时,我想使构建失败。

有一个Text Finder插件可以在自由式作业中执行此操作,但不能在管道中使用它。

您可以使用Log Parser插件 ,该插件也支持Jenkins Pipeline作业。 它为我们工作。

全球规则

我们在Manage Jenkins->Configure System->Console Output Parsing配置了一些全局规则。 在那里配置的文件放置在Jenkins Master可以访问的位置,例如

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

例如,有一个名为“默认”的规则集,它引用/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])
}

项目规则

原来,不需要全局配置条目。 您还可以使用工作空间中的规则,例如:

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])
}

尽管上面的示例是针对脚本化管道的,但也应该很容易将它们应用于声明性管道。

这有效

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)
                    }
                }
            }
        }
    }
}

但需要获得詹金斯(Jenkins)管理员的一些脚本批准。

并且有一个批准请求,其中指出“批准此签名可能会引入安全漏洞! 建议您拒绝它。

暂无
暂无

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

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