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

can I use changeset in when pipeline syntax

    Details

    • Type: Task
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Component/s: p4-plugin
    • Labels:
      None
    • Similar Issues:

      Description

      Hi,

       

      Is it possible to use changeset syntax ?

       

      For example,

      stage ('test') {
        when {
          changeset "branchA/folder1/**"
        }
        steps {
          ...
        }
      }

        Attachments

          Issue Links

            Activity

            Hide
            p4karl Karl Wirth added a comment -

            Hi Hokwang Lee - Yes but I do not know how useful it will be. The following uses 'changeset' to test for the 'Jenkinsfile':

             

            pipeline {  agent { label 'master' }  stages {
               stage("Test changeset"){
                    when { changeset "**/Jenkinsfile"}
                        steps{ echo "The changeset test worked!!"}
                    }
                stage("Display changeset?") {
                  steps {
                    script {
                       def changeLogSets = currentBuild.changeSets
                       for (int i = 0; i < changeLogSets.size(); i++) {
                       def entries = changeLogSets[i].items
                       for (int j = 0; j < entries.length; j++) {
                           def entry = entries[j]
                           echo "${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}"
                           def files = new ArrayList(entry.affectedFiles)
                           for (int k = 0; k < files.size(); k++) {
                               def file = files[k]
                               echo " ${file.editType.name} ${file.path}"
                           }
                        }
                       }
                    }
                  }
            
            

            Note that the text at the end attempts to display the contents of the changeset. In my case the output was:

            [Pipeline] }
            [Pipeline] // stage
            [Pipeline] withEnv
            [Pipeline] {
            [Pipeline] stage
            [Pipeline] { (Test changeset)
            [Pipeline] echo
            The changeset test worked!!
            [Pipeline] }
            [Pipeline] // stage
            [Pipeline] stage
            [Pipeline] { (Display changeset?)
            [Pipeline] script
            [Pipeline] {
            [Pipeline] echo
            1734 by super on Thu Jul 11 16:39:02 UTC 2019: Edited
            [Pipeline] echo
              edit //streams/main/Jenkinsfile
            [Pipeline] }
            [Pipeline] // script
            [Pipeline] }
            [Pipeline] // stage
            [Pipeline] }
            [Pipeline] // withEnv
            [Pipeline] }
            [Pipeline] // node
            [Pipeline] End of Pipeline
            Finished: SUCCESS
            

            I did not understand the changeset regex syntax so you may have to ask others about that.

             

            Show
            p4karl Karl Wirth added a comment - Hi Hokwang Lee - Yes but I do not know how useful it will be. The following uses 'changeset' to test for the 'Jenkinsfile':   pipeline { agent { label 'master' } stages { stage( "Test changeset" ){ when { changeset "**/Jenkinsfile" } steps{ echo "The changeset test worked!!" } } stage( "Display changeset?" ) { steps { script { def changeLogSets = currentBuild.changeSets for ( int i = 0; i < changeLogSets.size(); i++) { def entries = changeLogSets[i].items for ( int j = 0; j < entries.length; j++) { def entry = entries[j] echo "${entry.commitId} by ${entry.author} on ${ new Date(entry.timestamp)}: ${entry.msg}" def files = new ArrayList(entry.affectedFiles) for ( int k = 0; k < files.size(); k++) { def file = files[k] echo " ${file.editType.name} ${file.path}" } } } } } Note that the text at the end attempts to display the contents of the changeset. In my case the output was: [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Test changeset) [Pipeline] echo The changeset test worked!! [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Display changeset?) [Pipeline] script [Pipeline] { [Pipeline] echo 1734 by super on Thu Jul 11 16:39:02 UTC 2019: Edited [Pipeline] echo edit //streams/main/Jenkinsfile [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS I did not understand the changeset regex syntax so you may have to ask others about that.  
            Hide
            luckyhorang Hokwang Lee added a comment -

            Thanks. You can close this case.

            Show
            luckyhorang Hokwang Lee added a comment - Thanks. You can close this case.
            Hide
            p4karl Karl Wirth added a comment -

            Hi Hokwang Lee - Thanks.

            Show
            p4karl Karl Wirth added a comment - Hi Hokwang Lee - Thanks.
            Hide
            jrapczak Jesse Rapczak added a comment - - edited

            I'm trying to get this exact example working in a multibranch pipeline, and the when test exactly as written above is failing for some reason despite the other stuff looking OK... any ideas?

            [Pipeline] }
            [Pipeline] // stage
            [Pipeline] stage
            [Pipeline] { (Test changeset)
            Stage "Test changeset" skipped due to when conditional
            [Pipeline] }
            [Pipeline] // stage
            [Pipeline] stage
            [Pipeline] { (Display changeset?)
            [Pipeline] script
            [Pipeline] {
            [Pipeline] echo
            66 by jesse on Tue Nov 05 03:40:41 PST 2019: Polling time adjust
            [Pipeline] echo
             edit //streams/Dev-Jenkins/Jenkinsfile
            [Pipeline] }
            [Pipeline] // script
            [Pipeline] }
            [Pipeline] // stage
            
            Show
            jrapczak Jesse Rapczak added a comment - - edited I'm trying to get this exact example working in a multibranch pipeline, and the when test exactly as written above is failing for some reason despite the other stuff looking OK... any ideas? [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Test changeset) Stage "Test changeset" skipped due to when conditional [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Display changeset?) [Pipeline] script [Pipeline] { [Pipeline] echo 66 by jesse on Tue Nov 05 03:40:41 PST 2019: Polling time adjust [Pipeline] echo edit //streams/Dev-Jenkins/Jenkinsfile [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage
            Hide
            p4karl Karl Wirth added a comment -

            See JENKINS-60066 for investigation.

            Show
            p4karl Karl Wirth added a comment - See JENKINS-60066 for investigation.

              People

              • Assignee:
                Unassigned
                Reporter:
                luckyhorang Hokwang Lee
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: