pavankalyan chithari
pavankalyan chithari

Reputation: 21

Jenkins print build job commit IDs

I want to store and print all the commits happend to git since last build in jenkins pipeline

please find this image

There are 3 commits happened to git between previous build and current build, I just need to print those commit IDs and name of the user who did that commit

Upvotes: 1

Views: 678

Answers (1)

ycr
ycr

Reputation: 14604

You can use the following script to get the change set between the last successful build and the current build. If you just want to get the change set from the last build you can simply do currentBuild.changeSets in your Pipeline.

def allChangeSetsFromLastSuccessfulBuild() {
    def job = Jenkins.instance.getItem("$JOB_NAME")
    def lastSuccessBuild = job.lastSuccessfulBuild.number as int
    def currentBuildId = "$BUILD_ID" as int
    
    def changeSets = []

    for(int i = lastSuccessBuild + 1; i < currentBuildId; i++) {
        echo "Getting Change Set for the Build ID : ${i}"
        def changeSet = job.getBuildByNumber(i).getChangeSets()
        changeSets.addAll(changeSet)
    }
    changeSets.addAll(currentBuild.changeSets) // Add the current change set
    return changeSets
}

Upvotes: 1

Related Questions