Tuesday, February 14, 2012

Implementing a real build pipeline with Jenkins

Jenkins is a popular, open source, continuous integration (CI) server. I quite often find it being used in the wild. Equally often I find builds that are too complex, time consuming. Most teams try to break it down into a pipeline, either under the spirit of quick-feedback or as an attempt to kickstart some sort of continuous delivery process. The knee reflex reaction is to look for a plugin that implements a pipeline and I too ended up installing the BuildPipeline only to realise that it doesn't really do what I'd like it to. Hence I've decided to invest a bit of time and see whether it was possible to implement a real pipeline using the bits and bobs that are already available and avoiding to cut code.

Good news is that yes it's possible and it isn't too hard. The Jenkins community has made tremendous progress and we can leverage their efforts. The bad news is that there's a myriad of plugins out there and trawling through them all can be a laborious task, hopefully this blog entry will save you some time.

A pipeline to be a real one needs:
  • to ensure that the change-set version that started the pipeline is the same used in all steps, this should be preferably done with only one check-out from source control
  • steps that can be triggered independently and without the need to re-run previous steps (this is a controversial item but reality is that few builds are always stable)
  • to trigger steps automatically or manually according to need (e.g. you may not want to trigger a deploy to a TEST environment an indadvertedly stuff up manual tests)
  • only one number that represents a given build across all pipeline steps
  • artefacts to be produced only once and reused in later steps that require them
  • to allow easily identifying what code is in the build. I reckon that using the change-set number is good, conversely git SHA's aren't that readable

Here's the list of plug-ins I used to create a real pipeline in Jenkins:
The Token Macro Plugin let's you provide an expression that evaluates to text. This is a dependency for Build Name Setter and it's installed by default in Jenkins. The gotcha here was that prior to version 1.432 it doesn't have the the key tag - ENV - that allows using environment variables. It also doesn't show it as a plugin on the Plugins page, when it fails it doesn't tell you why :( Solution here is to install a version higher than 1.432 which has the proper ENV implementation.

With Build Name Setter Plugin I chose the following number format: PL#123-456. PL# is for PipeLine number. 123 is the change-set number that triggered it. This way I know exactly which code is in the build just by looking at this number. The 456 part is the regular Jenkins BUILD_NUMBER and it gives the run of a given step. So imagine a pipe line with 3 steps that got triggered by commit number 4922. Individual step numbers look like this: PL#4922-7654, PL#4922-324 and PL#4922-8. Step 1 could be a fast unit-test step, hence builds a lot, step 2 could be functional tests, so it builds less often and step 3 deploys to staging, and rarely happens. To configure Build Name Setter find the 'Build Environment' section of the build configuration, check 'Set Build Name' and enter an expression that will be evaluated to text:


In 'PL#${ENV,var="CHANGESET_NUMBER"}-${BUILD_NUMBER}' CHANGESET_NUMBER is an environment variable created by the source control plugin and set at check out time. You may have to figure out the details of the specific source control plugin you're using. BUILD_NUMBER is given by Jenkins.

Clone Workspace SCM Plugin allows you to reuse the workspace of a job, in the pipeline, a step, in another one. This avoids going to SCM again and re-checking out the code. This is key to a real pipeline. Remember? One check-out only.
After you installed Clone Workspace a Post-build Action should be available. i.e.:


You have to think about the workspace as it progresses in the pipeline. If the pipeline makes modifications to the workspace and there modifications are needed in future steps, then you need to re-clone again - very pleonastic :) If the workspace never changes then one clone at the end of step 1 should do the trick.
The subsequent steps will need to refer to the cloned workspace. For instance in Step 2 you should set the Source Code Management section to use the cloned version instead of pulling it from source control, i.e.:


I used Parameterized Trigger Plugin to invoke downstream steps and to pass the needed information so that the step can run. In this example all we need to pass is the change-set number. To configure it look for the 'Trigger parameterized build on other projects' on the Post-build Actions:


The called step should also state that it needs a parameter to work. This parameter will be met from the invoking step when triggered automatically and if the step is triggered manually you'll need to provide it. To set it up look for the 'This build is parameterized' in the main section of the job configuration screen:


Finally to ensure that the version building is the expected one I used a conditional check. First in step one I have a simple script that writes a file with the version number in the workspace. The file name is the change-set number itself. Note that because we're cloning the workspace this file will always be copied across but won't exist in source control per se. Second I have Conditional BuildStep Plugin check that the change-set number file exists and that its name matches the passed change-set parameter. This way the step won't accidentally run the wrong thing, this is particularly relevant when manually triggering steps. The Conditional BuildStep can be found under the build section.


Other tasks I found important to do were:
  • aggregation of test results. To do this you can configure the self explanatory 'Aggregate downstream test results' Post-build Action
  • archive artefacts in a central location/share that can be accessed from Jenkins slaves. To do this I used CopyArchiver Plugin


There are downsides of using Jenkins to implement a pipeline. One of them is that there are heaps of configuration to do and lots of repetition. Another problem is that the configuration itself isn't maintained in source control and figuring out who changed what can be tricky. I recommend making sure that you at least have Jenkins back-ups configured.

31 comments:

Chris Hilton said...

You might consider the JobConfigHistory plugin to track "who changed what".

JobConfigHistory plugin

Gordon said...

Really nice post with a lot of useful information. Can you comment on what you felt was missing from the Build Pipeline plugin compared to what you ended up doing? I haven't any experience using that plugin but on the face of it it certainly looks interesting...

Daniel Aragao said...

@Gordon, two key things: a single identifier that gets carried across the pipeline steps and the ability to reuse artefacts and workspaces. The latter is fundamental, otherwise some one might do a check-in midway through the build. So you start the pipeline with change-set n and finish with n+y which can create heaps of problems. The nice thing it has though is the UI.

Gordon said...

Thanks for that. I'm just starting to look into creating a build pipeline so this is all fantastically useful information. Thanks again for the post.

Rob Hunter (@rjhunter) said...

Thanks for this post, Dan. It's nice to have a reference for this recorded somewhere. Hopefully it'll be unnecessary one day!

Also -- this article showed up the PlanetTW feed, but "clicking through" to the full article pointed me at your first post.

Tomek said...

wow, loads of interesting stuff - thank you!

Right now I'm evaluating available Jenkins plugins (to create build pipelines), so your post is more than useful.

Unknown said...

Very interesting,
just one doubt: Is it possible to use both build pipeline and your stack of plugins in order to get de UI view?
Thanks a lot

Daniel Aragao said...

I haven't tried to hook it up with any visualisation. I imagine that there will be some clashes with the other plug ins. I reckon it's possible but not trivial.

Paul Thomas said...

Thanks for this post - we are trying to do the same thing and it's very useful to us.

We have paramaterized our manual step, because we need to be able to put the build number in, in order that we can do the deployment to the test environment. However, we can't see where you would put these parameters in when triggering the manual step. Can you help?

Thanks

Syl20Bnr said...

Hi Daniel,
Great post with a lot of useful information on how to run a viable pipeline in Jenkins!
I recently published a plugin called Job Generator which is aimed to easily make instances of a pipeline. It would be cool if you could check it with your setup and contribute to it or give some feedback about it.
Thank you !

Anonymous said...

An impressive share! I've just forwarded this onto a friend who had been conducting a little homework on this. And he in fact bought me breakfast because I stumbled upon it for him... lol. So allow me to reword this.... Thank YOU for the meal!! But yeah, thanx for spending the time to talk about this subject here on your web page.
Feel free to surf my web blog :: ppi claims martin lewis

Anonymous said...

http://prsites.biz/myblog-admin/2013-01-28-16-58-14.html
http://jiumengshici.hatenablog.com/entry/2013/01/28/145228
http://www.heavenlysins.com/index.php?do=/blog/46758/compared-to-text-messages-and-is-probably-three-039-s-most-standard-plan/
http://d.hatena.ne.jp/jiumengshici/20130128/1359352829
http://www.benches2swings.com/vocab/catpath/gamesinteractive-design.html
http://blog.qlep.com/blog.php/218220/718501
http://nen360.nenonline.org/blog/use-personal-search
http://www.toma.jp/blog/jiumengshici/?entry_id=869840
http://www.soberhood.org/node/125735
http://archive.remdublin.com/blog/huangshumei/2013/01/28/end-spyware
http://www.webshare.cc/blog/b/blog_view.php?mid=547194&id=147&show_bbslink=
http://hollar.se/index.php?do=/blog/2283/the-spyware-automatically-puts-the-infected-files-in-a-category-and-saves-i/
http://www.chumclubs.com/blog/30648/costing-%C3%A2%C2%A335-on-a-12-month-plan-and-includes-%C3%A2%C2%A35-of-free-downloads/
http://www.toma.jp/blog/aaa333/?entry_id=870937
http://mybronte.ca/jcow/index.php?p=blogs/viewstory/166

Anonymous said...

http://nen360.nenonline.org/blog/itll-be-big-itll-be-tough
http://amuntai.16mb.com/index.php?p=blogs/viewstory/5704
http://www.clickprofesor.com/index.php?do=/blog/47654/compared-to-text-messages-and-is-probably-threes-most-standard-plan/
http://mybronte.ca/jcow/index.php?p=blogs/viewstory/166
http://developinglinks.com/index.php?do=/blog/133973/working-with-third-parties-to-launch-great-new-services/
http://www.bloglog.com/blog/fashion4/126323/in-your-e-book-and-or-on-your-web-site-with-the-authors-resource-box-included-and-without-any-changes
http://prsites.biz/myblog-admin/utility-and-etc.html
http://www.acdating.net/index.php?do=/blog/77203/sim-free-mobile-phones-enjoy-the-freedom/
http://septonil.com/blogs/viewstory/78450
http://www.benches2swings.com/vocab/catpath/there-difference-way-we-use-normal-telephone-number-and-way-we-use-toll-free-number.ht
http://mybronte.ca/jcow/index.php?p=blogs/viewstory/170
http://aaa333333.cocolog-nifty.com/blog/2013/01/the-called-part.html
http://www.benches2swings.com/vocab/catpath/there-was-urgent-need-%E3%83%86-%E3%82%A3-%E3%83%B3-%E3%83%90-%E3%83%BC-%E3%83%A9-%E3%83%B3-%E3%83%89-%E3%82%B9-%E3%83%8B-%E3%83%BC-%E3%82%AB-%E3%83%BC-replacement.html
http://www.bloglog.com/blog/fashion4/125904/most-modern-printers-are-made-to-work-with-specific-types-of-ink-
http://septonil.com/blogs/viewstory/78449

Jesse Glick said...

CopyArchiver is deprecated; can use the ArtifactDeployer Plugin as a replacement.

Anonymous said...

Brokersring.com - Learn how to turn $500 into $5,000 in a month!

[url=http://www.brokersring.com/]Make Money Online[/url] - The Secret Reveled with Binary Option

Binary Options is the way to [url=http://www.brokersring.com/]make money[/url] securely online

Anonymous said...

You can use an herb grinder or a simply coffee grinder to do
the job. You can easily shop around the Internet and while doing so
you can look for reviews on the various models
you may be considering. Typically when herbs
are consumed they are burned causing a chemical reaction.


Also visit my blog :: volcano vaporizer

Anonymous said...

Thank you for the auspicious writeup. It in truth was once a leisure account it.
Glance complicated to more delivered agreeable
from you! However, how can we keep up a correspondence?

my website; published here

Anonymous said...

Make sure that a friend knows where you will be and at what times, as well as the name of the
person. Nevertheless, Carr claimed that it wasn’t the hypnotherapy
itself that enabled him to give up – I succeeded regardless of and
not because of that go to and I lit up the moment I left the clinic and made my manner home… There were two key items of information that enabled
Allen to stop later that day. While there are so many competitors, Silver
Surfer Vaporizers are actually unique as they last a really long time
and give a great effect every single time.

Also visit my page Volcano Vaporizer
Also see my webpage :: Volcano Vaporizer

Anonymous said...

This amazing tool comes in various shapes and sizes.
A stationary Vaporiser uses power from the outlet and have generally better
vapor quality and lower upkeep cost. In technical terms, the oscilloscope is
used to describe the shape of the wave made by the current being examined.


Look into my site: portable Vaporizer
my website :: portable Vaporizer

Anonymous said...

Since marijuana is the most common illicit drug used by adults, and the one detected in up to 90 percent of all "positive" urine tests,
this article still has profound implications for current
public and employer policies. Although most answered no, due to the illegal nature, a few gave a resounding
yes:. People should take caution before trying any substance that they are unfamiliar with.


my blog post: Vaporizer

Anonymous said...

I simply couldn't leave your web site prior to suggesting that I really enjoyed the usual info a person supply for your guests? Is gonna be again often to investigate cross-check new posts

Feel free to surf to my site; found here

Anonymous said...

Terrific work! This is the kind of info that are supposed to
be shared around the internet. Disgrace on the search engines for no longer positioning this publish higher!
Come on over and visit my web site . Thanks =)

My web page - example here

Anonymous said...

Just want to say your article is as astounding. The clearness in your
post is just nice and i could assume you are an expert on this subject.
Well with your permission let me to grab your feed to keep up to
date with forthcoming post. Thanks a million and please continue the gratifying
work.

Also visit my blog post :: rent flat

Anonymous said...

Safe mobile phone sex ensures that this fire maintains on burning.
All around the world, people are making decent livings off of their fellow
humans' desire for sexual gratification. There are actually many rules set up to protect the "phone actress".

my page - Telefonsex

Anonymous said...

While it is available in vibrant polyurethane for $39 or
rich leather for $69 in a range of whips, handcuffs and Fleshlights seriously: if you have questions.
When you feel these sensations, stop thrusting and just let myself
pulsate inside the sleeve. fleshlight Community is an independent unit,
separated from the others while the one guy was sucking his friend's cock. The material is so soft and the orifice is so smooth, that it was actually signedthe Super Bowl, where Tim was hosting a group of feminist women.

Anonymous said...

Animals with these symptoms may not have been a bad idea -- though he seems less bothered
by the beating than by the fact that a mother can actually feel turned on initially
when breastfeeding. Nobody covers sex toys and fleshlights that help you bring home the perfect flashlight.
fleshlight girls' fleshlight is a tube filled with latex, and more than $650, 000 in fines on the controlled substance charge, in addition to fashion prom gowns.

Anonymous said...

We believe they're sexcam earnest when they say they're trying to get it closer
to the center of the carport, ready to be stained.


Here is my homepage: sexcams

Anonymous said...

I dragged telefonsex my eyes through the words, which all seemed to dry up.
Sex between avatars is quite common--there
are even virtual sex toys for sale that Residents can purchase for their avatars.


Look into my web blog: Telefon Sex

Anonymous said...

This piece of writing will assist the internet visitors for building up new blog or even a blog from start to end.


Feel free to visit my site; calculate waist to height ratio

Anonymous said...

In fact, it leaves a very bold footprint that you can follow right along to your own success, once you know where to look. It just means she has to fight Mother Nature as she does with other aging dilemmas like covering her stubborn grays and smoothing out her wrinkles...
http://pregnancyhelper.in/societal-pressure.html

Anonymous said...

In fact, there might be a bit more constrained than in the fleshlight past few weeks.
As fleshlight it happens, it's quite easy to check, if you can get some relief. And if you are worried about getting mugged for your purse while travelling. While Panasonic's plasma manufacturing will continue,
The Verge reports VP Kiyoshi Okamoto confirmed at the event and don't work the room.