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

11 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
  • 0
    Avatar
    Majusmisiak Majusmisiak

    Very nice to get change sets after the build for changelog / email generation.

    Is it possible to achieve something similar upfront? Assume that I have MultiBranch Jenkins job and I want to decide on rebuilding only part of the project before the checkout. Then I can allocate appropriate node to rebuild selected part of the project.

    It works for me if I run node("any") { checkout scm } before the script provided, but does not immediately after checking out Jenkinsfile by Jenkins. Is there another way to do that?

  • 0
    Avatar
    Tomas Bjerre

    There is also a Git Changelog Plugin.

    It can be used for parsing Git (returnType CONTEXT) or rendering a changelog with the context and Mustach template (returnType STRING).

    More docs in the Wiki or GitHub, but here is a little something that renders the changelog to a string.

     def changelogString = gitChangelog returnType: 'STRING',
      from: [type: 'REF', value: 'git-changelog-1.50'],
      to: [type: 'REF', value: 'master'],
      template: """
      <h1> Git Changelog changelog </h1>
    
    <p>
    Changelog of Git Changelog.
    </p>
    
    {{#tags}}
    <h2> {{name}} </h2>
     {{#issues}}
      {{#hasIssue}}
       {{#hasLink}}
    <h2> {{name}} <a href="{{link}}">{{issue}}</a> {{title}} </h2>
       {{/hasLink}}
       {{^hasLink}}
    <h2> {{name}} {{issue}} {{title}} </h2>
       {{/hasLink}}
      {{/hasIssue}}
      {{^hasIssue}}
    <h2> {{name}} </h2>
      {{/hasIssue}}
    
    
       {{#commits}}
    <a href="https://github.com/tomasbjerre/git-changelog-lib/commit/{{hash}}">{{hash}}</a> {{authorName}} <i>{{commitTime}}</i>
    <p>
    <h3>{{{messageTitle}}}</h3>
    
    {{#messageBodyItems}}
     <li> {{.}}</li> 
    {{/messageBodyItems}}
    </p>
    
    
      {{/commits}}
    
     {{/issues}}
    {{/tags}}
      """
  • 0
    Avatar
    Clément Brief

    Hello everybody,

    I would like to know if it's possible to get all new files never committed before ? A little like "entry.affectedFiles" but only with new files.
    I need this to run SQL scripts but I need to know if the script has already been run.

    Thank you,
    Glaglaton

     

     
  • 0
    Avatar
    Angelo Loria

    I'm struggling to have more than one affected file listed. Using the code in the article only lists the first affected file, and I'm not sure why; shouldn't it iterate through the array?

     

    EDIT: Never mind, Jenkins has a bug where stuff doesn't iterate properly. Using it in a script{} within a stage works. Adding it as @NonCPS does not.

     

    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}"
            }
        }
    }
    Edited by Angelo Loria
Please sign in to leave a comment.