Issue
I have a var
in a groovy library that is trying to match a commit message and set a variable to return however it is not matching properly.
Note that I have tried this in a bare script where I provide message
as a hard coded String.
#!/usr/bin/env groovy
def call(String commitId) {
message = sh(
script: "git show -s --format=%s ${commitId}",
returnStdout: true
)
println "Based on the commit message: \n ${message}"
switch("${message}") {
case ~/.*Feature.*/:
verType = 'minor'
println "The Next version will be a ${verType} type"
break
case ~/^.*(Hot|Bug)fix.*$/:
verType = 'patch'
println "The Next version will be a ${verType} type"
break
case ~/^.*Release.*$/:
verType = 'major'
println "The Next version will be a ${verType} type"
break
default:
verType = 'patch'
println 'No matches using Patch'
break
}
return verType
}
I have tried commit ID's of literal, "Feature" and "Release" and it always returns the default value. I get an output in the Jenkins console like this:
[Pipeline] sh
+ git show -s --format=%s 1c532309b04909dc4164ecf9b3276104bf5c2bc0
[Pipeline] echo
Based on the commit message:
Feature
[Pipeline] echo
No matches using Patch
When I run the bellow similar script on the command line it returns "Release" which shows me that its in how message is passed to the statement.
#!/usr/bin/env groovy
message = 'Release, creating major release'
switch(message) {
case ~/.*Feature.*/:
verType = 'minor'
break
case ~/^.*(Hot|Bug)fix.*$/:
verType = 'patch'
break
case ~/^Release.*$/:
verType = 'major'
break
default:
verType = 'patch'
break
}
println verType
Solution
that's because message
contains multiline string
try message = '\nRelease, creating major release'
on your second script to reproduce an issue
to fix it you could modify your regex like this:
/^\s*Release.*$/
Answered By - daggett