jenkinsjenkins-pipelinegerrit-trigger

Why Does My Jenkins Job Reset After Completion When Triggered by Gerrit?


I'm using a Jenkins pipeline that is triggered by Gerrit. After the job completes, it seems to reset, which causes issues with subsequent steps or tracking the results. Here is the relevant part of my pipeline configuration:

pipeline {
agent {
    label "builder"
}
environment {
    PROJ_ROOT = "${env.WORKSPACE}"
    BUILD_NUMBER = "${env.BUILD_NUMBER}"
}
triggers {
    gerrit(triggerOnEvents: [
        patchsetCreated()
    ])
}
stages {
    stage("Preparation") {
        steps {
            script {
                // clone project and checkout specific change
                sh """
                git clone https://ci-tw.xxx.com/gerrit/${env.GERRIT_PROJECT} ${env.uniqueProjectDir}
                cd ${env.uniqueProjectDir}
                git checkout ${env.GERRIT_BRANCH}
                git fetch origin ${env.GERRIT_REFSPEC}
                git checkout FETCH_HEAD
                """
            }
        }
    }
    stage("Run CIP Build and Tests") {
        steps {
            script {
                // run build and tests
                def buildResult = sh(script: "./bin/build/run_cip_build.sh", returnStatus: true)
                if (buildResult != 0) {
                    currentBuild.result = "FAILURE"
                    error "Build or tests failed."
                }
            }
        }
    }
    stage("Cleanup Docker") {
        steps {
            script {
                sh """
                docker ps -q --filter "status=exited" | xargs --no-run-if-empty docker rm || true
                docker network prune -f || true
                docker images -q --filter "dangling=true" | xargs --no-run-if-empty docker rmi || true
                """
            }
        }
    }
}
post {
    always {
        echo "Pipeline finished."
        script {
            deleteDir()
        }
    }
    failure {
        echo "Pipeline failed."
    }
    success {
        echo "Pipeline succeeded."
    }
}

}

The Issue:

Whenever my Jenkins job fails, the Gerrit trigger settings get reset.

Questions:


Solution

  • After testing, I found that the problem seems to be related to the triggers set in the Pipeline. This causes the Gerrit trigger to be reset every time the job fails.

    triggers {
    gerrit(triggerOnEvents: [
        patchsetCreated()
    ])
    

    }