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.

WHY

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. I had one job where I manually had to checkout using windows batch script since the svn checkout url was created in runtime. And let’s face it. With pipeline hammer in your hand, the checkout process looks like a very inviting nail.

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

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

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.

Leave a Reply

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