Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to include changelog information on pipeline script #397

Open
olexandrd opened this issue Sep 13, 2018 · 3 comments
Open

Ability to include changelog information on pipeline script #397

olexandrd opened this issue Sep 13, 2018 · 3 comments

Comments

@olexandrd
Copy link

Currently there is not opportunity to include changes (github changelog) in messages from pipeline script, but this option is available in freestyle projects (Post-build Actions -> Slack Notifications -> Advanced -> Notification message includes).

@olexandrd
Copy link
Author

Found workaround there - https://support.cloudbees.com/hc/en-us/articles/217630098-How-to-access-Changelogs-in-a-Pipeline-Job-.
As a result, my declarative pipeline script looks next:

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

pipeline {
    agent none
    stages {...}
    post {
        success {
           slackSend (color: '#32CD32', message: "${env.JOB_NAME} Success, Changes: \n$changeString")
        }
}

@rafitadiaz
Copy link

rafitadiaz commented Aug 15, 2019

This should be part of the basic options for the pipeline method.
I also needed the triggering causes in my notifications and I also needed to find a workaround: https://stackoverflow.com/questions/46908390/jenkins-declarative-pipeline-find-out-triggering-job

// vars/getTriggerCause.groovy
@NonCPS
def call() {    
    def triggerCause = "" 
    echo "Get triggering Cause"
    
    // Get triggering information
    def causes = currentBuild.rawBuild.getCauses()
    for(cause in causes) {
        if (cause.class.toString().contains("UpstreamCause")) {
            triggerCause += "Triggered by job: " + cause.upstreamProject + "\n"
        } else {
            // This method already add "Started by " in the description
            triggerCause += cause.getShortDescription()  + "\n"
        }
    }

    return triggerCause
}

@samwar
Copy link

samwar commented May 26, 2021

That code can be written in a more groovy way:

def getChanges) {
    MAX_MSG_LEN = 100
    def changes = ""
    echo "Gathering SCM changes"
    def changeLogSets = currentBuild.rawBuild.changeSets
    changeLogSets.each { def changeLogSet ->
        def entries = changeLogSet.items
        entries.each { def entry ->
            truncated_msg = entry.msg.take(MAX_MSG_LEN)
            changes += " - $truncated_msg [$entry.author]\n"
        }
    }
    if (!changes) {
        changes = " - No new changes"
    }
    return changes
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants