简体   繁体   中英

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

It looks like you have a mistake in the second regex, that is overriding first one. eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM