How to Access Changelogs in a Pipeline Job

Issue

I would like to access changelogs in a Pipeline job.

Environment

  • CloudBees Jenkins Enterprise
  • Jenkins

Related Issue(s)

JENKINS-30412
Blacklisting RunWrapper.getRawBuild

Resolution

Pipeline Supporting APIs Plugin 2.2 or above

You can use currentBuild.changeSets in a sandboxed build as you could see in the following example of Git:

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}"
        }
    }
}

Pipeline Supporting APIs Plugin older than 2.2

You can use currentBuild.rawBuild.changeSets but this is not accessible from the sandbox. Following is an example 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}"
        }
    }
}
Have more questions? Submit a request

5 Comments

  • 0
    Avatar
    Xerxesai Xerxesai

    I made a function out of the above, and you need to remember to add the @NonCPS annotation, else you'll get exceptions.

    My function, which creates a string from all the changes (to be used by slackNotify):

    @NonCPS
    def getChangeString() {
    MAX_MSG_LEN = 100
    def changeString = ""

    echo "Gathering SCM changes"
    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]
    truncated_msg = entry.msg.take(MAX_MSG_LEN)
    changeString += " - ${truncated_msg} [${entry.author}]\n"
    }
    }

    if (!changeString) {
    changeString = " - No new changes"
    }
    return changeString
    }

    I'm sure there's problems with the code, as groovy isn't my bag, but it does work.

    Edited by Xerxesai Xerxesai
  • 1
    Avatar
    Kevin Sim

    Is there a sandbox solution to this yet? Com'on!

     

     

    Edited by Kevin Sim
  • 0
    Avatar
    Boris Vano

    Really? what is the solution for sandboxed builds?

  • 2
    Avatar
    Arnaud Heritier

    https://github.com/jenkinsci/workflow-support-plugin/blob/master/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java#L203

    It should be possible nowdays to use

    currentBuild.changeSets

    Which is available even in the sandbox (whitelisted)

    Is was added in workflow-support 2.2 to fix JENKINS-30412

  • 0
    Avatar
    Claudiu Chis

    I got this working combining the solutions from Xerxesai and Arnaud, so I can include the change log in the email.

     

    #!groovy
    // Declarative //
    pipeline {
    agent any

    environment {
    EMAIL_RECIPIENTS = 'email1@domain.com, email2@domain.com'
    }

    stages {
    stage('Build') {
    steps {
    echo 'Building..'
    }
    }
    stage('Test') {
    steps {
    echo 'Testing..'
    }
    }
    stage('Deploy') {
    steps {
    echo 'Deploying....'
    }
    }
    }
    post {
    success {
    sendEmail("Successful")
    }
    failure {
    sendEmail("Failed")
    }
    }
    }

    @NonCPS
    def getChangeString() {
    MAX_MSG_LEN = 100
    def changeString = ""

    echo "Gathering SCM changes"
    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]
    truncated_msg = entry.msg.take(MAX_MSG_LEN)
    changeString += " - ${truncated_msg} [${entry.author}]\n"
    }
    }

    if (!changeString) {
    changeString = " - No new changes"
    }
    return changeString
    }

    def sendEmail(status) {
    mail (
    to: "$EMAIL_RECIPIENTS",
    subject: "Build $BUILD_NUMBER - " + status + " ($JOB_NAME)",
    body: "Changes:\n " + getChangeString() + "\n\n Check console output at: $BUILD_URL/console" + "\n")
    }

     

     
    Edited by Claudiu Chis
Please sign in to leave a comment.