How to Checkout a Gerrit Change in a Jenkins Sandbox Pipeline

Lately I was working on a Jenkins Pipeline file on a Gerrit enabled project.

To do some experiments in a pipeline project you have to mimic the checkout step.

I ended up with the following snippet to do live/experimental changes to a specific Gerrit change:

env.GERRIT_REFSPEC = 'refs/changes/47/11/2'
env.GERRIT_PATCHSET_REVISION = 'c72380cbb771fb97dd11404354834201a4d17a56'

node {
  stage('Checkout') {
    sh "env"
    if(env.GERRIT_REFSPEC && env.GERRIT_PATCHSET_REVISION) {
      println "$GERRIT_REFSPEC"
      checkout([$class: 'GitSCM', branches: [[name: "$GERRIT_PATCHSET_REVISION"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'planets_gerrit_id_rsa', name: "", refspec: "$GERRIT_REFSPEC", url: 'ssh://gerrit@gerrit.datenkollektiv.de:12345/planets-simulation']]])
    } else {
      git url: 'git@git.datenkollektiv.de:planets-simulation', credentialsId: 'planets_id_rsa'
    }
  }

  ...
}

As I wasn't able to get this up and running easily I thought it might be worth sharing...

If you got so far you might also like our other Jenkins related posts:

Programmatically add Environment Variables to a Jenkins Instance

How-To manipulate a Jenkins Job with the Script Console

Adding Views and Descriptions to Jenkins - The Groovy Way

Show Comments