Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline regex not matching anything

Tags:

jenkins

groovy

I've got a regex with sample text that is working on regex101, but doesn't seem to work in my Jenkins pipeline scenario. So I'm assuming I've got a mistake in my pipeline script, but I fail to see where.

Here's a repro:

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!")
                    }
                }
            }
        }
    }
}

I've tested this with Jenkins 2.60.2 running in the official Docker container.

This provides the following (abbreviated) output:

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

The expected output is no error because there should be a match.

What am I doing wrong?

like image 271
Jeroen Avatar asked Sep 06 '17 09:09

Jeroen


1 Answers

  • Just use =~ (the find operator) instead of ==~ (the match operator):

    def hasSummaryMatch = (output =~ /\d+ scenarios \([^()]+\)/)
    
  • When match operator ==~ is used, then a strict match of the input string required

like image 183
daggett Avatar answered Sep 19 '22 11:09

daggett