This post is on how to checkout code in Jenkins Pipeline using Snippet Generator. Both Git as well as SVN are supported. Jenkins pipeline feature is an awesome feature. A reason good enough to make you upgrade to Jenkins 2. It essentially has made scripting a first class citizen in world of Jenkins. I am replacing all the clunky old NANT script based Jenkins jobs with Pipeline based jobs.
WHY
So why will you checkout code in Jenkins pipeline?
Having the ability to check out code at any stage of the pipeline is invaluable.
Jenkins provides a very simple out of the box way of checking out code in pipeline.
checkout scm
.
It will simply checkout code’s version which triggered the run.
However in case you want more control then you need to customise the checkout process.
For example what if you wanted to checkout some other code during the checkout process instead of the one which triggered the run? All this is possible.
HOW
First install Jenkins Subversion plugin. If you chose the default option of installing popular plugins at the start of Jenkins then Jenkins Subversion plugin will be there already. Once installed you can see something familiar. This is how you used to do it (and can still do in a freestyle project).
However we will create a pipeline job. Once done then all we need is to write the pipeline script. We can take a shortcut here.
Time to use the Snippet Generator. Click on the Pipeline Syntax
.
You will then see this. Click on the Sample Step
and choose checkout: General SCM
A familiar screen pops up.
In my case I will choose SCM type as Subversion. Fill in the details as usual. And click Generate pipeline Script
.
It will generate a mess of code which you can then copy and past in the pipeline as shown below. I usually format it a bit to keep my sanity.
It will be something like this
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: '234243-45654-234randomstuff', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'https://starkindustries/ironman/superGlueForThanosFingers/repo']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
Then all you need is to paste it in the pipeline script area. Here is the complete script which does essentially only one thing e.g. checkout code in Jenkins pipeline.
node('master') { stage 'Checkout' cleanWs() checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: '234243-45654-234randomstuff', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'https://starkindustries/ironman/superGlueForThanosFingers/repo']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']]) }
Here we are configuring this pipeline job to run on master only. We have defined a stage called Checkout
and in that we have put the code. Note that I am using cleanWs
to clean up the whole workspace before downloading the code. Also you can select more options in the code generation page to get a more customized behavior. And that’s it. Done.
Do not forget the goodies which email-ext plugin can bring to your Jenkins journey. My post on it is now live.