Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins and Groovy and Regex

Tags:

jenkins

groovy

I am very new to using groovy. Especially when it comes to Jenkins+Groovy+Pipelines.

I have a string variable that can change from time to time and want to apply a regex to accomodate the 2 or 3 possible results the string may return.

In my groovy code I have:

r = "Some text that will always end in either running, stopped, starting." def regex = ~/(.*)running(.*)/ assert regex.matches(r)

But I receive an error in the jenkins output:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.util.regex.Pattern.matches() is applicable for argument types: (java.lang.String)

UPDATE: I was able to create a pretty nifty jenking groovy while loop in a pipeline job i am creating to wait for a remote process using the regex info here and a tip in a different post (do .. while() in Groovy with inputStream?).

            while({
                def r = sh returnStdout: true, script: 'ssh "Insert your remote ssh command that returns text'
                println "Process still running.  Waiting on Stop"
                println "Status returned: $r"
                r =~ /running|starting|partial/
            }());
like image 751
JuanD Avatar asked Dec 21 '16 14:12

JuanD


People also ask

Does Groovy support regex?

A regular expression is a pattern that is used to find substrings in text. Groovy supports regular expressions natively using the ~”regex” expression.

Does Jenkins support Groovy?

Jenkins features a Groovy script console which allows one to run arbitrary Groovy scripts within the Jenkins controller runtime or in the runtime on agents.

How Groovy is used in Jenkins?

It can be used to orchestrate your pipeline in Jenkins and it can glue different languages together meaning that teams in your project can be contributing in different languages. Groovy can seamlessly interface with the Java language and the syntax of Java and Groovy is very similar.

How do I integrate Groovy script in Jenkins?

To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name. In the second case path taken is relatively from the project workspace directory.


1 Answers

Straight-forward would be:

String r = "Some text that will always end in either running, stopped, starting."
assert r =~ /(.*)running(.*)/
like image 64
injecteer Avatar answered Nov 04 '22 16:11

injecteer