【问题标题】:Jenkins Text finder Plugin, How can I use this plugin with jenkinsfile?Jenkins Text finder Plugin,我如何在 jenkinsfile 中使用这个插件?
【发布时间】:2019-08-14 04:45:21
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

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

    例如:

    pipeline {
        agent {
            label {
                label "master"
            }            
        }
    
        stages {
            stage ('Check logs') {
                steps {
                    filterLogs ('ERROR', 2)
                }
            }
        }
    }
    

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

    我们的 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'
        }
    }
    

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

    【讨论】:

    • TextFinder 现在支持管道:issues.jenkins-ci.org/browse/JENKINS-34983
    • 这与 Jenkins 管道并不真正相关。下面提到的是正确的。此解决方案要求您深入了解 Java,这对于运行其他语言和部署的人来说是一个邪恶的步骤。
    • 现在下面确实有更好的解决方案。但是这个解决方案在 2 年前并不存在。为您的管道编写 groovy 脚本没有任何缺点,并且与您正在构建/部署的语言无关。 Jenkins 支持共享库设置,您可以在其中编写可重用的 .groovy 脚本,这样您就可以在一个地方管理魔法并在多个管道中重用它:jenkins.io/doc/book/pipeline/shared-libraries
    【解决方案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--这不是自动的。鉴于 Text Finder 插件自 2014 年 1 月以来尚未更新,而且我在 pipeline steps 的列表中没有看到它,它可能根本不可能。有关潜在的解决方法,请参阅How to use Jenkins plugins with no pipeline support?

      【讨论】:

      • 解决方法的链接很奇怪。它说“不。你不能”。
      • @AndreyRegentov 我同意。我想知道是否有人能够看到这个问题的历史——它曾经有过另一个答案吗?我认为我不会像今天这样与它联系起来。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 2012-07-02
      • 2017-07-18
      • 1970-01-01
      相关资源
      最近更新 更多