Learning Jenkins v2.x Pipeline
Learning Jenkins v2.x Pipeline
This tutorial will show you the basic steps for getting a Jenkins pipeline up and running and using it in your continuous integration/deployment workflow.
Join the DZone community and get the full member experience.Join For Free
Monitor your CI/CD pipelines end-to-end with Hygieia, an open source dashboard from Capital One.
This is about re-tracing to the state in time, about a year and half back, that demanded me (with little or no choice but) to acquire a new set of skills - set up new projects in Jenkins v2.x Pipeline with no formal training in pipelines, and likewise, no prior experience in Java programming. Looking back to that situation and trying to understand what resources were available, how and where I got started, mistakes and pitfalls (quite a common occurrence, then and now as well) that helped me to kick-start this journey which is, in progress still, very much.
Of course, some of the references listed here are quite recent (like CloudBees offerings) while some of them (Pipeline Examples) have evolved or are growing with contributions from the community.
What Was I Looking For?
Equipped with basic knowledge of programming in Python and Shell, but absolutely zero in Java or Groovy, it was (at times, even now) absolutely adventurous to start with. But where to start and how to start being productive?
The objective of this article is to point you to some of the resources to get started with Jenkins Pipelines.
Set Up Jenkins Playground
For very obvious reasons, Jenkins in production isn't a toy thing to play around, so try and set up a local instance of Jenkins on a desktop or laptop. With this approach, a fresh set of challenges arises - install Jenkins as .war (web archive) or Docker container, or spin up a Virtual Machine and install Jenkins in it - what to choose from these options? While there are several other ways and means to achieve this, my personal choice was the VM route.
With the chosen approach to have a test instance of Jenkins settled, the next step (no pun on Pipeline steps!) is to initiate Pipeline style builds using Jenkinsfile (pipeline workflow script).
It was quite an astonishing array of terminologies that can be, at times, perplexing -- pipeline, workflow, Groovy, DSL, step, node, Jenkinsfile, so on and so forth. Persist and persevere.
The simplest thing to do is to start with a few built-in Pipeline scripts -- available in the "Definition" section of the "Pipeline" tab.
Hold on there -- the sample scripts provided here are basic and they are in "imperative" or "scripted" style of Groovy DSL. There are no examples of the "opinionated" Declarative style scripts. And, this is the current state, too; if you happen to install the latest (LTS or bleeding-edge) version of Jenkins with the standard suite of plugins.
If you are curious as to why there are two different styles of writing Pipeline-as-code, look here.
Okay, now that I've started by copying, verbatim, a few lines of pipeline script (Groovy DSL), there should be a method to try out (in a repetitive manner) various basic steps detailed in the Pipeline documentation. This is where "Replay" came to assist me.
This will open up a small editor window with the version of the workflow script that was used to execute the build job. Now, this is where it gets interesting. Just below the editor window is a link titled "Pipeline Syntax."
If you click on this link, Pipeline Syntax, that's displayed at the bottom of the in-line editor (shown in the Replay window), it will open the "Snippet Generator," along with several other options -- on the left-hand side, there is a link to exhaustive documentation on various steps, then the references to Global variables, and how to setup IntelliJ editor for Groovy DSL.
Snippet generator is a very handy tool -- you can select the Pipeline step of your choice and then generate snippet code which can then be reused in your workflow script.
Not all plugins are pipeline ready, while a few plugins are compatible and expose steps that can be used in Pipeline script. To know that list of compatible plugins, visit this page.
Another resource that's noteworthy of mention is the numerous snippet of Pipeline code that's maintained here, with generous contributions from various folks.
Here are few tips on how to get started with Pipeline.
As is applicable to any learning methodology, it is good to know the pitfalls to avoid and how best to keep away from bad choices; best practices are outlined here.
Opinions expressed by DZone contributors are their own.