Created
August 14, 2019 15:48
-
-
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
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)