• Icon: New Feature New Feature
    • Resolution: Fixed
    • Icon: Major Major
    • pipeline
    • None

      Workflow scripts would often like to behave differently depending on the SCM changelog(s) for the current build (or perhaps a downstream build). You can use currentBuild.rawBuild.changeSets but this is not accessible from the sandbox.

          [JENKINS-30412] Access to build's own changelog from script

          Jesse Glick added a comment -

          Workaround in the case of Git for a non-sandboxed build:

          def changeLogSets = currentBuild.rawBuild.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}"
                  }
              }
          }
          

          Jesse Glick added a comment - Workaround in the case of Git for a non-sandboxed build: def changeLogSets = currentBuild.rawBuild.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}" } } }

          Jesse Glick added a comment -

          Also requested: to get the aggregate changelog for all builds since the last successful (or stable).

          Jesse Glick added a comment - Also requested: to get the aggregate changelog for all builds since the last successful (or stable).

          Jesse Glick added a comment -

          Aggregate changelog can be computed in terms of the above, so I will leave this to some helper library.

          Jesse Glick added a comment - Aggregate changelog can be computed in terms of the above, so I will leave this to some helper library.

          Andrew Bayer added a comment -

          So, if I'm understanding correctly, the above workaround won't work for multibranch, since that's inherently sandboxed, right?

          Andrew Bayer added a comment - So, if I'm understanding correctly, the above workaround won't work for multibranch, since that's inherently sandboxed, right?

          Jesse Glick added a comment -

          Correct.

          Jesse Glick added a comment - Correct.

          James Dumay added a comment - - edited

          jglick / hrmpw / abayer is there any reason why the change log is sandboxed? Could we offer it in some other way? e.g. request it as a JSON blob or well known array structure?

          James Dumay added a comment - - edited jglick / hrmpw / abayer is there any reason why the change log is sandboxed? Could we offer it in some other way? e.g. request it as a JSON blob or well known array structure?

          Patrick Wolf added a comment -

          jglick / abayer / jamesdumay the Global Library is not sandboxed now would it be possible to put this snippet into a global library function and run it? Would currentBuild be available in a global library call?

          Likewise, plugins are not subject to the sandbox so this might be able to be solved with the "step in pipeline" work that Andrew and KK are looking at.

          Patrick Wolf added a comment - jglick / abayer / jamesdumay the Global Library is not sandboxed now would it be possible to put this snippet into a global library function and run it? Would currentBuild be available in a global library call? Likewise, plugins are not subject to the sandbox so this might be able to be solved with the "step in pipeline" work that Andrew and KK are looking at.

          James Dumay added a comment -

          hrmpw thanks for that. abayer you think there is an easier way we can make this work in your new work?

          James Dumay added a comment - hrmpw thanks for that. abayer you think there is an easier way we can make this work in your new work?

          When trying to run this:

          node("mac") {
            env.JAVA_HOME = tool 'JDK 8'
            def buildNumber = env.BUILD_NUMBER
            def build = currentBuild
            
          ....
          
                summarizeBuild(${build})
                ...
          
          }
          
          @NonCPS
          def summarizeBuild(b) {
            b.changeSets.collect { cs ->
              /kind=${cs.kind}; entries=/ + cs.collect { entry ->
                /${entry.commitId} by ${entry.author.id} ~ ${entry.author.fullName} on ${new Date(entry.timestamp)}: ${entry.msg}:  /+ entry.affectedFiles.collect { file ->
                  /${file.editType.name} ${file.path}/
                }.join('; ')
              }.join(', ')
            }.join(' & ')
          }
          

          I always get this error:

          java.io.NotSerializableException: hudson.plugins.git.GitChangeSetList
          	at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
          	at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:569)
          	at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
          	at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56
          

          Did anybody have a valid example?

          Martin Reinhardt added a comment - When trying to run this: node( "mac" ) { env.JAVA_HOME = tool 'JDK 8' def buildNumber = env.BUILD_NUMBER def build = currentBuild .... summarizeBuild(${build}) ... } @NonCPS def summarizeBuild(b) { b.changeSets.collect { cs -> /kind=${cs.kind}; entries=/ + cs.collect { entry -> /${entry.commitId} by ${entry.author.id} ~ ${entry.author.fullName} on ${ new Date(entry.timestamp)}: ${entry.msg}: /+ entry.affectedFiles.collect { file -> /${file.editType.name} ${file.path}/ }.join( '; ' ) }.join( ', ' ) }.join( ' & ' ) } I always get this error: java.io.NotSerializableException: hudson.plugins.git.GitChangeSetList at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860) at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:569) at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65) at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56 Did anybody have a valid example?

            jglick Jesse Glick
            jglick Jesse Glick
            Votes:
            5 Vote for this issue
            Watchers:
            15 Start watching this issue

              Created:
              Updated:
              Resolved: