Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-70845

Stage "when changelog" wrong help description or wrong oposite implementation

      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.

          [JENKINS-70845] Stage "when changelog" wrong help description or wrong oposite implementation

          Lumir added a comment -

          Update: After today debug and adding to plugin some test and debug output and run it on our Jenkins latest LTS.

          Code looks correct. Somehow our Jenkins can't find gitChangesetClass.

          This is debug output in our Jenkins:

          ---------Initialize eval---------
          ---------Class for name failed start-------
          java.lang.ClassNotFoundException: hudson.plugins.git.GitChangeSet
          ---------Class for name failed end-------
          gitChangesetClass:null
          changeClass:class hudson.plugins.git.GitChangeSet
          -------- Generic case ---------
          Message:Some title that we don't care about
          false || false = false
           

          and this when it run localy as unit test from maven

          ---------Initialize eval---------
          After class forname: class hudson.plugins.git.GitChangeSet
          gitChangesetClass:class hudson.plugins.git.GitChangeSet
          changeClass:class hudson.plugins.git.GitChangeSet
          -------- gitChangeSetClass ---------
          Title:ENC-2742 dfhah:
          Comment:ENC-2742 dfhah
          
          - fhafh
          
          [NO_IT]
          [NO_SETUP]
          :
          false || true = true
          

          How is it posible that Class.forName in initializeEval() failed but the entry itself is of this class?

          Mentioned file is: ChangeLogConditionalScript.groovy

           

          Our jenkins is 2.387.1

          Git plugin installed 5.0

          job is Multibranch pipeline and in job configuration is set under Branch Sources Git.

          Lumir added a comment - Update: After today debug and adding to plugin some test and debug output and run it on our Jenkins latest LTS. Code looks correct. Somehow our Jenkins can't find gitChangesetClass. This is debug output in our Jenkins: ---------Initialize eval--------- --------- Class for name failed start------- java.lang.ClassNotFoundException: hudson.plugins.git.GitChangeSet --------- Class for name failed end------- gitChangesetClass: null changeClass: class hudson.plugins.git.GitChangeSet -------- Generic case --------- Message:Some title that we don't care about false || false = false and this when it run localy as unit test from maven ---------Initialize eval--------- After class forname: class hudson.plugins.git.GitChangeSet gitChangesetClass: class hudson.plugins.git.GitChangeSet changeClass: class hudson.plugins.git.GitChangeSet -------- gitChangeSetClass --------- Title:ENC-2742 dfhah: Comment:ENC-2742 dfhah - fhafh [NO_IT] [NO_SETUP] : false || true = true How is it posible that Class.forName in initializeEval() failed but the entry itself is of this class? Mentioned file is: ChangeLogConditionalScript.groovy   Our jenkins is 2.387.1 Git plugin installed 5.0 job is Multibranch pipeline and in job configuration is set under Branch Sources Git.

            Unassigned Unassigned
            rimuln Lumir
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: