How to checkout code in Jenkins pipeline

By | June 15, 2018

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.


So why will you checkout code in Jenkins pipeline?
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.


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).

checkout code in Jenkins pipeline

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.

checkout code in Jenkins pipeline

Time to use the Snippet Generator. Click on the Pipeline Syntax.

checkout code in Jenkins pipeline

You will then see this. Click on the Sample Step and choose checkout: General SCM

checkout code in Jenkins pipeline

A familiar screen pops up.

checkout code in Jenkins pipeline

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

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.

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.