Jenkins and Groovy and Regex - jenkins

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 (How do I iterate over all bytes in an inputStream using Groovy, given that it lacks a do-while statement?).
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/
}());

Straight-forward would be:
String r = "Some text that will always end in either running, stopped, starting."
assert r =~ /(.*)running(.*)/

If you are only using this regex here you can try the following:
r = "Some text that will always end in either running, stopped, starting."
assert r ==~ /(.*)(running|stopped|starting)\.?$/, "String should end with either running, started or stopped"
Explanation:
(.*) - matches anything
(running|stopped|starting) - matches either running, stopped or starting
\.? - optionally end with a dot expect zero or one occurrence of a dot, but you need to escape it, because the dot is a regex special character
$ - end of the line, so nothing should come after
the ==~ operator is the groovy binary match operator. This will return true if it matches, else false
See this example on regex 101

matches does not receive String.
Try
Pattern.compile("your-regex").matcher("string-to-check").find()

Related

I have a Jenkins global variable in a string - how do I evaluate it?

I need to accept all kinds of global Jenkins variables as strings (basically as parameters to ansible like system - a template stored in \vars).
def proof = "\"${params.REPOSITORY_NAME}\""
echo proof
def before = "\"\${params.REPOSITORY_NAME}\""
echo before
def after = Eval.me(before)
echo after
The result is:
[Pipeline] echo
"asfd"
[Pipeline] echo
"${params.REPOSITORY_NAME}"
groovy.lang.MissingPropertyException: No such property: params for class: Script1
the first echo proves that the param value actually exists.
the second echo is the what the input actually looks like.
the third echo should have emitted asdf instead I get the exception.
Any ideas? I'm hours into this :-(
You may want to check:
groovy: Have a field name, need to set value and don't want to use switch
1st Variant
In case you have: xyz="REPOSITORY_NAME" and want the value of the parameter REPOSITORY_NAME you can simply use:
def xyz = "REPOSITORY_NAME"
echo params."$xyz" // will print the value of params.REPOSITORY_NAME
In case if your variable xyz must hold the full string including params. you could use the following solution
#NonCPS
def split(string) {
string.split(/\./)
}
def xyz = "params.REPOSITORY_NAME"
def splitString = split(xyz)
echo this."${splitString[0]}"."${splitString[1]}" // will print the value of params.REPOSITORY_NAME
2nd Variant
In case you want to specify an environment variable name as parameter you can use:
env.“${params.REPOSITORY_NAME}”
In plain groovy env[params.REPOSITORY_NAME] would work but in pipeline this one would not work inside the sandbox.
That way you first retrieve the value of REPOSITORY_NAME and than use it as key to a environment variable.
Using directly env.REPOSITORY_NAME will not be the same as it would try to use REPOSITORY_NAME itself as the key.
E.g. say you have a job named MyJob with the following script:
assert(params.MyParameter == "JOB_NAME")
echo env."${params.MyParameter}"
assert(env."${params.MyParameter}" == 'MyJob')
This will print the name of the job (MyJob) to the console assuming you did set the MyParameter parameter to JOB_NAME. Both asserts will pass.
Please don’t forget to open a node{} block first in case you want to retrieve the environment of that very node.
After trying all those solutions, found out that this works for my problem (which sounds VERY similar to the question asked - not exactly sure though):
${env[REPOSITORY_NAME]}

Access project parameter in multi line shell script in jenkins pipeline [duplicate]

def a = "a string"
def b = 'another'
Is there any difference? Or just like javascript to let's input ' and " easier in strings?
Single quotes are a standard java String
Double quotes are a templatable String, which will either return a GString if it is templated, or else a standard Java String. For example:
println 'hi'.class.name // prints java.lang.String
println "hi".class.name // prints java.lang.String
def a = 'Freewind'
println "hi $a" // prints "hi Freewind"
println "hi $a".class.name // prints org.codehaus.groovy.runtime.GStringImpl
If you try templating with single quoted strings, it doesn't do anything, so:
println 'hi $a' // prints "hi $a"
Also, the link given by julx in their answer is worth reading (esp. the part about GStrings not being Strings about 2/3 of the way down.
My understanding is that double-quoted string may contain embedded references to variables and other expressions. For example: "Hello $name", "Hello ${some-expression-here}". In this case a GString will be instantiated instead of a regular String. On the other hand single-quoted strings do not support this syntax and always result in a plain String. More on the topic here:
http://docs.groovy-lang.org/latest/html/documentation/index.html#all-strings
I know this is a very old question, but I wanted to add a caveat.
While it is correct that single (or triple single) quotes prevent interpolation in groovy, if you pass a shell command a single quoted string, the shell will perform parameter substitution, if the variable is an environment variable. Local variables or params will yield a bad substitution.

Comparing 2 parameters in Jenkins pipeline in a single command

what is wrong with below code, comparing 2 strings in groovy
I am trying do the comparison between the 2 parameters in a single line to make it look tidier
if (params.dirname == ((params.path =~ ~/${params.dirname}/).with { matches() ? it[0] : null })) {
print success
}
Throwing Exception -
java.lang.NoSuchMethodError: No such DSL method 'matches' found among steps
There is no need to over-complicate your use case. According to:
params.dirname = hde, params.path = /usr/tmp/jenkins/hde/filename.txt or /usr/hde/jenkins/ing/filename.txt or any random path which has hde in it
you are trying to find if given string a contains substring b. It can be done using Java's method String.contains(String substring). Alternatively you can use regular expression for that, but String.contains() just looks a few times simpler to understand what is your intention. Consider following Groovy script:
def params = [
dirname: 'hde',
path: '/usr/tmp/jenkins/hde/filename.txt'
]
// Using String.contains()
if (params.path.contains(params.dirname)) {
println "Path '${params.path}' contains '${params.dirname}'"
}
// Using regular expression
if (params.path ==~ /(.*)${params.dirname}(.*)/) {
println "Path '${params.path}' contains '${params.dirname}'"
}
When you run it both if statements evaluates to true:
Path '/usr/tmp/jenkins/hde/filename.txt' contains 'hde'
Path '/usr/tmp/jenkins/hde/filename.txt' contains 'hde'

Trying to run a groovy script to populate a string's description, but the script returns no output, what could be the reason?

I'd like to configure a choice parameter in Jenkins.
The parameter I'd like to configure is called CIDR.
I tried using "Extended choice parameter" plugin but to no avail.
What I'm trying to do, is to let the user manually insert the chosen CIDR, considering the CIDRs which are already in use -> I want to run a groovy script to populate the string description with CIDRs which are already in use.
In order to list the already in use CIDRs, I wrote the following Groovy code:
#!/usr/local/bin/groovy
def p = ['/usr/local/bin/aws', 'ec2', 'describe-vpcs'].execute() | 'grep CidrBlock'.execute() | ['awk', '{print $2}'].execute() | ['tr', '-d', '"\\"\\|,"'].execute()
p.waitFor()
println p.text
The script runs properly in terminal:
itai#Itais-MacBook-Pro ~ - $ groovy cidrs.groovy
172.31.0.0/16
172.51.0.0/16
172.51.0.0/16
I even accepted a suspicious signature in Jenkins in-script approvals to allow the script to run.
But when I insert it to the Groovy script section of the string description and run the "build the job with parameters", the string dropdown stays empty.
What am I doing wrong?
Looks trivial issue. Try below.
Change From :
println p.text
To:
return p.text
The reason why the parameter kept being empty is that as it seems, the "Extended Choice Parameter" plugin expects the output to be an array.
Changing the script to the following code solved the issue:
#!/opt/groovy-2.4.12/bin/groovy
def p = ['/usr/bin/aws', 'ec2', 'describe-vpcs'].execute() | 'grep CidrBlock'.execute() | ['awk', '{print $2}'].execute() | ['tr', '-d', '"\\"\\|,"'].execute()
p.waitFor()
def output = []
p.text.eachLine { line ->
output << line
}
output.each {
println it
}
Now the parameter is populated with the available CIDRs.

Insert % character in the middle of the string in Groovy

My Code snippet below,(running from Jenkins)
def mainUrl = "http://localhost:8080/job/"
...
jobsName.each(){
println "Jobs: ${it}"
println "${mainUrl}${it}/config.xml"
}
Which gives output like below:
Jobs: Env_test
Jobs: Dev_test
Jobs: Model test
Jobs: Prod test
I'm trying to replace the space character with % and used replaceAll method too, still no luck.
println "${mainUrl}${it}.replaceAll("//s","%")/config.xml"
Output I got:
http://localhost:8080/job/Model test.replaceAll(
http://localhost:8080/job/Prod test.replaceAll(
I'm looking for a Output like,
http://localhost:8080/job/Model%test/config.xml
http://localhost:8080/job/Prod%test/config.xml
Any suggestions . Thanks.
Try:
println "${mainUrl}${it.replaceAll('\\s','%')}/config.xml"
Change your code to:
println "${mainUrl}${it}".replaceAll("\\s","%") + "/config.xml"
Taking this apart, it means:
join mainUrl and it (you missed a double quote char after {it}),
replace each space (regex reguires a backslash (not forward slash), but here it should be doubled),
and add /config.xml, but as a separate string.

Resources