-1

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.

2 Answers2

1
if ("${GIT_COMMIT}"=="${GIT_PREVIOUS_SUCCESSFUL_COMMIT}") {
    sh 'exit 1'
}
else {
    echo "${GIT_PREVIOUS_COMMIT}" 
} 
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