Skip to content

Instantly share code, notes, and snippets.

@ababushk
Created August 14, 2019 15:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ababushk/930980d79ab09c74da34be459563f760 to your computer and use it in GitHub Desktop.
Save ababushk/930980d79ab09c74da34be459563f760 to your computer and use it in GitHub Desktop.
Get commit hash without checkout step in Jenkins Pipeline in GitHub Organization Folder
def commitHashForBuild(build) {
def scmAction = build?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
if (scmAction?.revision instanceof org.jenkinsci.plugins.github_branch_source.PullRequestSCMRevision) {
return scmAction?.revision?.pullHash
} else if (scmAction?.revision instanceof jenkins.plugins.git.AbstractGitSCMSource$SCMRevisionImpl) {
return scmAction?.revision?.hash
} else {
error("Build doesn't contain revision information. Do you run this from GitHub organization folder?")
}
}
commit_sha = commitHashForBuild(currentBuild.rawBuild)
@dliakh
Copy link

dliakh commented Mar 20, 2024

It looks like it returns not the hash of the commit that triggered the build, but some commit that was checked out in the past
(for me that's apparently the hash of the commit that triggered the pipeline last time when the skipDefaultCheckout option was not active)

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