sakworld
sakworld

Reputation: 77

Based on a REGEX pattern set a value using Groovy in Jenkins pipeline

Based on a REGEX validation I am trying to set a value which is not happening as expected in Groovy Jenkins pipeline If version = 1.1.1 or 1.5.9 or 9.9.9 like that then it has to be a managed store else like 1.22.9 or 1.99.9 ... tmp store. Below is what I tried with

String artefact_bucket_name
def artefact_version = "1.99.0"
if (artefact_version ==~ /[0-9]{1}\.[0-9]{1}\.[0-9]{1}/) {
 artefact_bucket_name = "managed-artefact-store"
}
if (artefact_version ==~ /[0-9]{1}\.[0-9]{1,2}\.[0-9]{1}/) {
 artefact_bucket_name = "tmp-artefact-store"
}
echo "Application version to deploy is ${artefact_version} from Artefact store ${artefact_bucket_name}"

Upvotes: 0

Views: 415

Answers (1)

ymochurad
ymochurad

Reputation: 999

It looks like you have a mistake in the second regex, that is overriding first one. e.g. when you have artefact_version = 1.1.1 - It matches first regex and second regex as well, so it always will be tmp-artefact-store.

I would change the second regex to match string like:

/[0-9]{1}\.[0-9]{2}\.[0-9]{1}/ - Notice I changed {1,2} to only {2}. This will exclusively match strings like "\d.\d\d.\d", so version like 1.1.1 will match only first regex and version like 1.99.9 - only second

Upvotes: 1

Related Questions