Reputation: 649
I am trying to check if my Jenkins parameter contains a hostname. But when I use Regular Expressions to see if it contains the name it doesn't check.
I would guess I have an error in the way I am checking or how I have it wrapped in brackets.
Below is a sample of what I am working with
stage('Release 1') {
when {
expression { params.SECRET_NAME != "" && params.STAGING_ENV != ("*some.host.name*") }
}
steps {
echo "Release 1"
}
}
stage('Release 2') {
when {
expression {params.STAGING_ENV == ("*some.host.name*") && params.SECRET_NAME == ("*+*") }
}
steps {
echo "Release 2"
}
}
}
I want it to skip the stage in my Jenkins pipeline if it does not meet the conditions
Upvotes: 3
Views: 4139
Reputation: 4203
Ok, you need multiple changes here, from inside out:
*
with .*
. Simply put, in regex *
denotes the same (set) of characters any number of times (abc*
matches abccccc
), whereas .*
denotes any character any number of times (abc.*
matches abccccc
, abcdefg
, abcadkhsdalksd
, etc.)."
surrounding the regex patterns; lest you want them to be interpreted as string literals./
to define the string boundary.()
themselves are optional here.==
with the match operator ==~
(strict), which returns a boolean.+
in *+*
should be a literal, then you must escape it as *\+*
.Stitching these together, your pipeline should look like:
stage('Release 1') {
when {
expression {
params.SECRET_NAME != "" && !(params.STAGING_ENV ==~ /.*some.host.name.*/)
}
}
steps {
echo "Release 1"
}
}
stage('Release 2') {
when {
expression {
params.STAGING_ENV ==~ /.*some.host.name.*/ && params.SECRET_NAME ==~ /.*\+.*/
}
}
steps {
echo "Release 2"
}
}
Further reading:
Upvotes: 2