Jenkins: checkout Gerrit patchset (Gerrit Trigger plugin)

Today I had to setup automatic pipeline triggering for each new patchset pushed to Gerrit for review. The Gerrit Trigger plugin makes it a piece of cake to achieve the goal.

In reply to How to Checkout a Gerrit Change in a Jenkins Sandbox Pipeline: such a snippet can be easily found directly in Jenkins > browse to your pipeline > click "configure" > click "pipeline syntax" > Sample step: select checkout: General SCM > fill what you need, click on advanced and add a refspec and generate the snippet. Here a snippet using the GERRIT variables exposed by the plugin.

node {
 stage('checkout gerrit patchset') {
 echo "gerrit branch: ${GERRIT_BRANCH}, gerrit refspec: ${GERRIT_REFSPEC}"
 checkout([$class: 'GitSCM', branches: [[name: "${GERRIT_BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'jenkins-rsa', refspec: "${GERRIT_REFSPEC}", url: 'ssh://yourgerritserver:29418/yourrepo']]])
 }
}

Note extensions: [[$class: 'CleanBeforeCheckout']] is a good idea if you need to build from different branches, if your setup is simpler, you can just use extensions: [].