Created
April 13, 2017 05:36
-
-
Save ftclausen/8c46195ee56e48e4d01cbfab19c41fc0 to your computer and use it in GitHub Desktop.
Jenkins pipeline - An approach to get all commits since the last successful build.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- mode: groovy -*- | |
// vim: set filetype=groovy : | |
node( 'some_node' ) { | |
stage( "Phase 1" ) { | |
sshagent( credentials: [ 'some_creds' ] ) { | |
checkout scm | |
def lastSuccessfulCommit = getLastSuccessfulCommit() | |
def currentCommit = commitHashForBuild( currentBuild.rawBuild ) | |
if (lastSuccessfulCommit) { | |
commits = sh( | |
script: "git rev-list $currentCommit \"^$lastSuccessfulCommit\"", | |
returnStdout: true | |
).split('\n') | |
println "Commits are: $commits" | |
} | |
} | |
} | |
} | |
def getLastSuccessfulCommit() { | |
def lastSuccessfulHash = null | |
def lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild() | |
if ( lastSuccessfulBuild ) { | |
lastSuccessfulHash = commitHashForBuild( lastSuccessfulBuild ) | |
} | |
return lastSuccessfulHash | |
} | |
/** | |
* Gets the commit hash from a Jenkins build object, if any | |
*/ | |
@NonCPS | |
def commitHashForBuild( build ) { | |
def scmAction = build?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction } | |
return scmAction?.revision?.hash | |
} |
Is there a way i can get the getLastSuccessfulCommit in a Declarative Pipeline ?
GIT_PREVIOUS_SUCCESSFUL_COMMIT
is the environment variable exposed by jenkins build and provides the last successful build git commit.
GIT_PREVIOUS_SUCCESSFUL_COMMIT
is the environment variable exposed by jenkins build and provides the last successful build git commit.
simple variable! it works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in a multi branch pipeline there is a env var exposed
GIT_PREVIOUS_COMMIT
. This seems to be the commit of the last build however regardless of pass fail.