I am trying to assign the git commit hash to a variable defines in Jenkins pipeline as follows
GIT_COMMIT_HASH = sh "(git log -n 1 --pretty=format:'%H')"
This will print the commit hash in Jenkins build log but it fails to assign the value.
When I try to print the value using
steps{
script {
GIT_COMMIT_HASH = sh "(git log -n 1 --pretty=format:'%H')"
echo "**************************************************"
echo "${GIT_COMMIT_HASH}"
echo "**************************************************"
}
}
This will results null
How may I assign the value ?
Pass the commit sha1 to a predefined parameter and in Build-Execute shell run git checkout <commit> before the build process starts. Some extra work is needed to make sure the check-out goes well. Check the box This project is parameterized and then you can add predefined parameters.
In Git, get the tree hash with: git cat-file commit HEAD | head -n1. The commit hash by hashing the data you see with cat-file . This includes the tree object hash and commit information like author, time, commit message, and the parent commit hash if it's not the first commit.
Commit hashesThe long string following the word commit is called the commit hash. It's unique identifier generated by Git. Every commit has one, and I'll show you what they're used for shortly. Note: The “commit hash” is sometimes called a Git commit “reference” or “SHA”.
The conventional commits plugin is a Jenkins plugin to programmatically determine the next semantic version of a git repository using: Last tagged version. Commit message log. Current version of the project.
You have to tell the sh script to return stdout back to your script, rather than just dumping it to stdout.
GIT_COMMIT_HASH = sh (script: "git log -n 1 --pretty=format:'%H'", returnStdout: true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With