I want to prevent the Jenkins build if the present commit is same as the previous sucessful build commit. Is there any way to check the present build and previous build in Jenkins. I'm using Jenkins file.
In simple words I don't want jenkins to have a build if there is no commit. Manual Build should not work.
Asked
Active
Viewed 520 times
-1

Alexy Pulivelil
- 87
- 10
2 Answers
1
if ("${GIT_COMMIT}"=="${GIT_PREVIOUS_SUCCESSFUL_COMMIT}") {
sh 'exit 1'
}
else {
echo "${GIT_PREVIOUS_COMMIT}"
}

Alexy Pulivelil
- 87
- 10
0
AFAIK, you can't prevent the build from happening, unless you use SCM polling. If polling is not an option you can simply start the build, check whether there are any changes, and then stop the build. For this you can use changeSet
.
stage('Build') {
steps {
git url:'https://github.com/xxxx/sample.git', branch: 'main'
script {
if(currentBuild.changeSets.size() > 0) {
echo "There are changes, so continue"
} else {
echo "No changes, stop the build"
currentBuild.result = "NOT_BUILT"
error("Skipping the build.")
}
echo "Building Stuff"
}
}
}

ycr
- 12,828
- 2
- 25
- 45
-
Thanks a lot for sharing the answer. Let me try this out – Alexy Pulivelil Aug 30 '22 at 06:31