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

7 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?

  • 3
    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
  • 0
    Avatar
    Ut Ut
    Set authors = currentBuild.changeSets.collect({ it.items.collect { it.author } })
  • 0
    Avatar
    Medovárszky Zoltán

    Some pimping:

    ...

                script {

                    

                    changeLog = getChangeString()

                    emailBody = """

                    <style>

                        .tbl { display: table; border-left: 1px solid #ccc; border-top: 1px solid #ccc; }

                        .tblRow { display: table-row; }

                        .tblCell { border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; display: table-cell; padding: 3px 10px; }

                        .tblBody { display: table-row-group; }

                        .hl { font-weight: bold; background-color: #eee; }

                    </style>

                    <body>

                        <div class="tbl">

                            <div class="tblBody">

                                <div class="tblRow"><div class="tblCell hl">Result</div><div class="tblCell">${currentBuild.currentResult}</div></div>

                                <div class="tblRow"><div class="tblCell hl">Name</div><div class="tblCell">${currentBuild.fullDisplayName}</div></div>

                                <div class="tblRow"><div class="tblCell hl">Build URL</div><div class="tblCell">${BUILD_URL}</div></div>

                                <div class="tblRow"><div class="tblCell hl">Job URL</div><div class="tblCell">${JOB_URL}</div></div>

                                <div class="tblRow"><div class="tblCell hl">Build number</div><div class="tblCell">${BUILD_NUMBER}</div></div>

                                <div class="tblRow"><div class="tblCell hl">Repository</div><div class="tblCell">${gitProjectURL}</div></div>

                                <div class="tblRow"><div class="tblCell hl">Branch/PR</div><div class="tblCell">${BRANCH_NAME}</div></div>

                                <div class="tblRow"><div class="tblCell hl">Duration</div><div class="tblCell">${currentBuild.durationString}</div></div>

                                <div class="tblRow"><div class="tblCell hl">Log</div><div class="tblCell">${BUILD_URL}consoleText</div></div>

                                <div class="tblRow"><div class="tblCell hl">Changelog</div><div class="tblCell">${changeLog}</div></div>

                            </div>

                        </div>

                    </body>

                    """.stripIndent()

                    

                    emailSubject = "Jenkins build ${currentBuild.currentResult}: ${JOB_NAME} #${BUILD_NUMBER}"

                    emailRecipients = [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']]                

                    emailext subject: emailSubject, body: emailBody, recipientProviders: emailRecipients

                }

    ...

     

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

    echo "Gathering SCM changes"
    def changeLogSets = currentBuild.changeSets
    for (logset in changeLogSets) {
    for (entry in logset.items) {
    truncated_msg = entry.msg.take(MAX_MSG_LEN)
    changeString += " - ${truncated_msg} [${entry.author}]<br />"
    for (file in entry.affectedFiles) {
    changeString += "<span style=\"padding-left:20px;\">[${file.editType.name}] ${file.path}<br /></span>"
    }
    }
    }

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

     

    Edited by Medovárszky Zoltán
Please sign in to leave a comment.