Jenkins Text finder Plugin, How can I use this plugin with jenkinsfile?

送分小仙女□ 提交于 2019-12-14 01:28:56

问题


I am trying write a jenkinsfile using Text Finder plugin, but I don't know exactly how it works.

Here's my code:

pipeline {
    agent {
        label {
            label "master"
        }            
    }
    stages {
        stage('CHECKOUT') {
            steps{
                script{
                    echo "##[1 / 4] ERROR"
                }
                publishers {
                    textFinder("*ERROR*", '', true, false, true)
                }
            }
        }
    }
}

回答1:


As @mghicks already mentions, not every plugin supports Jenkins pipelines. In this case the Text Finder plugin is not supporting it. You can for example write your own groovy function for it:

For example:

pipeline {
    agent {
        label {
            label "master"
        }            
    }

    stages {
        stage ('Check logs') {
            steps {
                filterLogs ('ERROR', 2)
            }
        }
    }
}

We are calling a function filterLogs and we provide the parameters 'ERROR' (search for the word ERROR in your logs) and we define the occurence of the word 'ERROR' (when the word ERROR is there 2 times, than make the job unstable):

Our filterLogs function looks like:

#!/usr/bin/env groovy

import org.apache.commons.lang.StringUtils

def call(String filter_string, int occurrence) {
    def logs = currentBuild.rawBuild.getLog(10000).join('\n')
    int count = StringUtils.countMatches(logs, filter_string);
    if (count > occurrence -1) {
        currentBuild.result='UNSTABLE'
    }
}

You can also just implement the function just inside your pipeline if you are not using shared libraries or something.




回答2:


pipeline {
    agent {
        label {
            label "master"
        }            
    }
    stages {
        stage('CHECKOUT') {
            steps{
                script{
                    echo "##[1 / 4] ERROR"
                }
                publishers {
                    findText regexp: failure, alsoCheckConsoleOutput: true notBuiltIfFound: true
                }
            }
        }
    }
}



回答3:


Plugins need to enable pipeline support--it's not automatic. Given the Text Finder plugin hasn't been updated since Jan 2014, and I don't see it in the list of pipeline steps, it simply may not be possible. For a potential workaround see How to use Jenkins plugins with no pipeline support?



来源:https://stackoverflow.com/questions/48450640/jenkins-text-finder-plugin-how-can-i-use-this-plugin-with-jenkinsfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!