I want to set up a build/deployment Jenkins for a new docker based storage system in a “XYZ as Code” manner. So the solution should be a (declarative) pipeline script using a Jenkinsfile from the git repo.
The main point i struggled with was that Jenkins always checked out the origin/master branch from the repo. The reason was that you have to tell Jenkins, he has to use “Additional Behaviours”.
At least for checking out the intended version of the Jenkinsfile it worked.
Nevertheless, the checkout within the pipeline script in the Jenkinsfile still didn’t work as i wanted. The same as above applies here, you have to add the “Additional Behaviour”. This is possible using the “Snipped Generator” available at ${JENKINS-URL}/pipeline-syntax.
Here simply use “Checkout from version control”, add repo credentials and leave Branch Specifier empty. Add the Additional Behavior “Strategy what to build”, choose “Gerrit Trigger” and voila something like the following snippet should be generated:
checkout([$class: 'GitSCM', branches: [[name: '**']],
extensions: [[$class: 'BuildChooserSetting',
buildChooser: [$class: 'GerritTriggerBuildChooser']]],
gitTool: 'Default',
userRemoteConfigs: [[credentialsId: 'Some-Hash-Value',
url: 'ssh://$Host:$Port/$project']]])
This will checkout the latest patchset from a gerrit commit.
