Retrieving the Patchset Number from a Gerrit Event
Note (2026-08-14): The env var names used below are still injected by the current Gerrit Trigger plugin — GERRIT_REFSPEC, GERRIT_HOST, GERRIT_PORT, GERRIT_PROJECT, and also GERRIT_PATCHSET_NUMBER / GERRIT_CHANGE_NUMBER (both confirmed present in the plugin source). checkout scmGit(...) remains the current recommended Declarative-pipeline git step.
clone by http
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 pipeline { agent any options { skipDefaultCheckout() } stages { stage('PatchSet Number' ) { steps { script { if (!env.GERRIT_PATCHSET_NUMBER) { error 'GERRIT_PATCHSET_NUMBER not set - is this job triggered by Gerrit Trigger?' } println "Change: ${env.GERRIT_CHANGE_NUMBER}" println "PatchSet: ${env.GERRIT_PATCHSET_NUMBER}" println "Refspec: ${env.GERRIT_REFSPEC}" currentBuild.description = "refs/changes/${env.GERRIT_CHANGE_NUMBER}/patchset ${env.GERRIT_PATCHSET_NUMBER}" } } } stage('Checkout PatchSet' ) { steps { checkout scmGit( branches: [[name: 'FETCH_HEAD' ]], extensions: [ cloneOption( depth: 1 , shallow: true , noTags: true , ) ], userRemoteConfigs: [[ refspec: '${GERRIT_REFSPEC}' , url: 'https://${GERRIT_HOST}/${GERRIT_PROJECT}' ]] ) } } } }
clone by ssh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 pipeline { agent any options { skipDefaultCheckout() } stages { stage('PatchSet Number' ) { steps { script { if (!env.GERRIT_PATCHSET_NUMBER) { error 'GERRIT_PATCHSET_NUMBER not set - is this job triggered by Gerrit Trigger?' } println "PatchSet: ${env.GERRIT_PATCHSET_NUMBER}" println "Refspec: ${env.GERRIT_REFSPEC}" } } } stage('Checkout PatchSet' ) { steps { checkout scmGit( branches: [[name: 'FETCH_HEAD' ]], extensions: [ cloneOption( depth: 1 , shallow: true , noTags: true , ), [$class: 'UserIdentity' , email: '[email protected] ' , name: 'Jenkins' ] ], userRemoteConfigs: [[ credentialsId: '<CRED_ID>' , refspec: '${GERRIT_REFSPEC}' , url: "ssh://jenkins@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}" ]] ) } } } }