Jenkins 文本查找器插件,如何将此插件与 jenkinsfile 一起使用?

dms*_*rra 5 jenkins jenkins-pipeline

我正在尝试使用Text Finder 插件编写 jenkinsfile ,但我不知道它到底是如何工作的。

这是我的代码:

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

lvt*_*llo 4

正如 @mghicks 已经提到的,并非每个插件都支持 Jenkins 管道。在这种情况下,文本查找器插件不支持它。例如,您可以为其编写自己的常规函数​​:

例如:

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

    stages {
        stage ('Check logs') {
            steps {
                filterLogs ('ERROR', 2)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我们正在调用函数filterLogs,并提供参数“ERROR”(在日志中搜索“ERROR”一词),并定义“ERROR”一词的出现(当“ERROR”一词出现两次时,会使作业不稳定):

我们的 filterLogs 函数如下所示:

#!/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'
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您不使用共享库或其他东西,您也可以在管道内部实现该函数。