-
Bug
-
Resolution: Unresolved
-
Minor
-
None
Hi,
last three days I'm trying to add when changelog <regex> to our pipeline script.
Goal is skip stage if contains in log at the "comment" part [NO_IT] or in another stage [NO_SETUP]
Based on the documentation I added to the stage this code:
stage("Integration tests") { when { not { changelog '.*\\[NO_IT\\].*' } }
As from docu I understood that stage is invoked when changelog is match. And as I want skip the stage so the opposite I added the not.
Problem is that stage is always invoked.
So I start find where could be problem. At the end I trying the code that I found in github repo
1. in Groovy web console (http://groovyconsole.appspot.com/edit/5077290488692736):
def pattern = '.*\\[NO_IT\\].*' def multiLinePattern = '(?m)(?s)^[^\\r\\n]*?' + pattern + '[^\\r\\n]*?$' def singlePattern = java.util.regex.Pattern.compile(pattern) def multiPattern = java.util.regex.Pattern.compile(multiLinePattern, java.util.regex.Pattern.MULTILINE | java.util.regex.Pattern.DOTALL) String title = "ENC-2742 dfhah[NO_IT]" String comment = """ENC-2742 dfhah - fhafh [NO_IT] [NO_SETUP]""" def result1 = singlePattern.matcher(title).matches() def result2 = multiPattern.matcher(comment).matches() def result = result1 || result2 println "${result1} ${result2} => ${result}" def jenkinsPattern = '.*^\\[DEPENDENCY\\] .+$' def jenkinsMultiLinePattern = '(?m)(?s)^[^\\r\\n]*?' + jenkinsPattern + '[^\\r\\n]*?$' singlePattern = java.util.regex.Pattern.compile(jenkinsPattern ) multiPattern = java.util.regex.Pattern.compile(jenkinsMultiLinePattern, java.util.regex.Pattern.MULTILINE | java.util.regex.Pattern.DOTALL) title = "Some title that we don't care about" comment = """Some title that we don't care about\n\nSome explanation\n[DEPENDENCY] some-app#45""" result1 = singlePattern.matcher(title).matches() result2 = multiPattern.matcher(comment).matches() result = result1 || result2 println "${result1} ${result2} => ${result}"
Output:
true true => true false true => true
here it looks that matches and overall code is correct.
Next I tried add to our pipeline stage code that I found in test folders of project:
stage("Test Conditional") { when { //Perhaps we should use the /../ syntax directly so we don't suffer from ugly escaping? //TODO verify the /../ syntax in the #174 parser changelog '.*^\\[DEPENDENCY\\] .+$' } steps { script { echo "Dull World" } } } stage("Test Conditional negative") { when { //Perhaps we should use the /../ syntax directly so we don't suffer from ugly escaping? //TODO verify the /../ syntax in the #174 parser not { changelog '.*^\\[DEPENDENCY\\] .+$' } } steps { script { echo "Dull World" } } }
and output in jenkins log is:
[Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Test Conditional) Stage "Test Conditional" skipped due to when conditional [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Test Conditional negative) [Pipeline] script [Pipeline] { [Pipeline] echo Dull World [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage
From docu for me looks opposite logic first stage should output Dull world and the 2nd stage should write that was skipped.
Interesting for me is that WhenStageTest#whenChangelog expects that log do not contains the skip message. => Test expects when regex is found then skip the stage.
But as I wrote at the begin documentation says stage is invoked when regex is match.