Jenkins 管道正则表达式不匹配任何东西

标签 jenkins groovy

我有一个带有示例文本 that is working on regex101 的正则表达式,但似乎在我的 Jenkins 管道场景中不起作用。所以我假设我的管道脚本有错误,但我看不到哪里。

这是一个再现:

pipeline {
    agent any

    stages {
        stage ('Test') {
            steps {
                script {
                    echo ("Test")

                    output = "Some text. \n\n 12 scenarios (3 failed, 2 success) plus text \n\n and some more text"
                    def hasSummaryMatch = (output ==~ /\d+ scenarios \([^()]+\)/)

                    echo ("hasSummaryMatch = " + hasSummaryMatch)

                    if (!hasSummaryMatch) {
                        error ("No summary!")
                    }
                }
            }
        }
    }
}

我已经使用在官方 Docker 容器中运行的 Jenkins 2.60.2 对此进行了测试。

这提供了以下(缩写)输出:

Started by user Administrator
Running on master in /var/jenkins_home/workspace/Test001
Test
hasSummaryMatch = false
ERROR: No summary!
Finished: FAILURE

预期的输出没有错误,因为应该有匹配项。

我究竟做错了什么?

最佳答案

  • 只需使用 =~ (the find operator ) 而不是 ==~ (the match operator ):
    def hasSummaryMatch = (output =~ /\d+ scenarios \([^()]+\)/)
    
  • 当使用匹配运算符 ==~ 时,则需要输入字符串的严格匹配
  • 关于 Jenkins 管道正则表达式不匹配任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46070923/

    相关文章:

    具有 Google API 的 Android 模拟器未在 Ubuntu Server 16.04 上以 Jenkins 启动

    java - Webdriver 访问数据交换元素

    groovy - 使用 Groovy 进行选择排序

    java - Groovy 脚本错误 : java. io.NotSerializedException : com. xlson.groovycsv.CsvIterator

    jenkins - 什么是 Jenkins 的种子工作,它是如何运作的?

    jenkins - 如何比较不同工作的 Jenkins 结果

    Groovy:如何从另一个闭包调用顶级范围内的闭包

    javascript - webrtc 通过 websocket 客户端/服务器连接问题

    groovy - 执行 Nexus 3 嵌入式 groovy

    Groovy 转换行为不一致