Jenkins Text finder插件,如何将此插件与jenkinsfile一起使用? [英] Jenkins Text finder Plugin, How can I use this plugin with jenkinsfile?

查看:581
本文介绍了Jenkins Text finder插件,如何将此插件与jenkinsfile一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用文本查找器插件编写一个jenkinsfile,但我不知道它是如何工作的.

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

这是我的代码:

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

推荐答案

正如@mghicks所提到的,并不是每个插件都支持Jenkins管道.在这种情况下,Text Finder插件不支持它. 例如,您可以为此编写自己的groovy函数:

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:

例如:

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

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

我们正在调用filterLogs函数,并提供参数"ERROR"(在您的日志中搜索ERROR),并定义单词"ERROR"的出现(当ERROR出现2次时,比make工作不稳定):

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

我们的filterLogs函数如下所示:

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.

这篇关于Jenkins Text finder插件,如何将此插件与jenkinsfile一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆