pipeline { agent any stages { stage('Build') { steps { script { sh """ docker buildx build -t helloworld """ } } } } post { failure { script { // Get the last successful build number def jobName = env.JOB_NAME def lastSuccessfulBuild = Jenkins.instance.getItemByFullName(jobName).getLastSuccessfulBuild() if (lastSuccessfulBuild) { // Redeploy the last successful build echo "Redeploying build #${lastSuccessfulBuild.number}..." echo "last build was ${lastSuccessfulBuild.number}" // You can call your deployment script or steps here // Example: sh "deploy-script.sh ${lastSuccessfulBuild.number}" } else { error "No successful build found to deploy." } } } } }