简体   繁体   中英

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.

There is a Text Finder plugin that can do it in freestyle jobs, but one cannot use it in pipelines.

You can use the Log Parser Plugin which also support Jenkins Pipeline jobs. It's working for us.

Global Rules

We configured some global rules in Manage Jenkins->Configure System->Console Output Parsing . The files configured there are placed at a location accessible to the Jenkins Master, eg

# /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):

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.

And there is an approval request that states "Approving this signature may introduce a security vulnerability! You are advised to deny it. "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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