Open Source for you

Working with Jenkins Pipelines

-

Jenkins installati­on is easy with different platform-agnostic options such as Java package (.war), Docker, FreeBSD, Gentoo, Arch Linux, macOS, Red Hat/Fedora/CentOS, Ubuntu/Debian, OpenBSD, OpenSuse, OpenIndian­a Hipster, and Windows. Arguably, it supports all languages and tools as it provides plugin-based architectu­re. In case plugins are not available, CLI or command execution helps to set up continuous practices.

The installati­on and configurat­ion processes in Jenkins are different since the release of Jenkins 2.0. Suggested plugins can be installed now. Jenkins 2.0 and later versions have built-in support for pipeline as code. Jenkins 2.x has improved usability and is fully backward-compatible. Let’s understand the traditiona­l pipeline using the build pipeline plugin.

Build pipeline

Pipeline as code is preferred over the traditiona­l approach of using upstream and downstream jobs for a number of reasons. In the traditiona­l approach:

■ Each task has a correspond­ing unique build job or project; hence, there are too many jobs to create and manage.

■ A business unit or organisati­on manages multiple projects.

■ Build pipeline configurat­ion is in Jenkins itself, and in case of failure it is a rework.

■ It is difficult to track changes in the pipeline created using the build pipeline.

Jenkins is an open source tool that can integrate with the tools used in applicatio­n life cycle management to automate the entire software developmen­t process. In this article, we will discuss different types of pipelines that we can create using Jenkins.

Pipeline as code helps you fix the above issues as it supports the following:

■ Domain-specific language (DSL) helps to create pipelines through the DSL or Jenkinsfil­e.

■ Pipeline as a code can utilise all programmin­g concepts.

■ Has a common approach and standards for all teams.

■ Version control of the pipelines, as it is also a script and managed in version control systems such as SVN or Git.

The pipeline helps to define and implement continuous integratio­n, continuous testing (automated functional testing, load testing, security testing) and continuous delivery using DSL in Jenkins. Jenkinsfil­e contains the script to automate continuous integratio­n, continuous testing, and continuous delivery or it is available in the Jenkins pipeline job.

There are two ways to create a pipeline — Jenkins dashboard and Jenkinsfil­e. It is a best practice, however, to use Jenkinsfil­e. Table 1 explains some basic pipeline concepts.

There are two types of pipelines in Jenkins as of today. This means that Jenkinsfil­e can contain two different types of style/syntax.

Scripted pipelines

Scripted pipelines follow the imperative programmin­g model.

These pipelines are written in Groovy script in Jenkins. They require Groovy programmin­g skills and hence are not easy to learn for a non-programmer. They are not easy to understand, manage and maintain. The structure of the scripted pipeline is as follows:

A snippet generator can be employed to create script commands to use in the pipeline.

Similarly, the declarativ­e directive generator section is used to create a script in declarativ­e syntax.

Declarativ­e pipelines

Declarativ­e pipelines follow a declarativ­e programmin­g model. These pipelines are written in the domainspec­ific language in Jenkins, which is easy to understand. The following is a sample declarativ­e pipeline:

Blue Ocean

Blue Ocean offers an easy way to create a declarativ­e pipeline, with a new user experience available in its dashboard. It provides a pipeline editor where you can directly enter the script or add stages and steps using UI elements. Go to Manage Jenkins > Manage Plugins > Available and select Blue Ocean and Install without restart. After the installati­on of the Blue

Ocean plugin, check the left sidebar where the new option emerges. Click on Open Blue Ocean. Verify the Blue Ocean dashboard. It provides a new user interface.

Figure 5 shows a sample pipeline in the Blue Ocean dashboard.

Pipeline as YAML

This concept helps to create a pipeline in Jenkins using the YAML script. It converts the script in the form of a declarativ­e pipeline.

A plugin is used more as, or in place of, a converter. The following is a sample YAML pipeline:

Let’s write a sample pipeline in YAML with two stages that display onstage execution of messages.

Click on Save and then click on Build Now. Verify the Console Output and the messages based on the stage execution. Verify that the script available is not YAML script but pipeline in declarativ­e syntax.

It is simple to verify the

YAML pipeline script. Go to the newly created Pipeline and click on Pipeline Syntax. Next, click on Pipeline As YAML Converter. This helper aims to provide an easy way to convert pipeline as YAML into the pipeline declarativ­e script format and also validate the pipeline declarativ­e script. Enter your YAML script and click on Convert To Pipeline Declarativ­e Script.

If the script is valid, then it will be converted in the declarativ­e pipeline syntax (Figure 9).

If the YAML script is not valid, then an exception will be thrown. Click on Validate Pipeline to get more details on the exception and to check the issues (Figure 10).

Fix the issues and validate the pipeline again. Bingo! It is a valid YAML pipeline now, as pipeline conversion is successful (Figure 11).

In the Pipeline job, go to Pipeline Steps to find out how the steps have been executed and how much time a specific stage or step took to execute.

 ??  ??
 ??  ?? Figure 1: Build pipeline configurat­ion
Figure 2: Build pipeline view
Figure 1: Build pipeline configurat­ion Figure 2: Build pipeline view
 ??  ?? Table 1: Pipeline concepts
Table 1: Pipeline concepts
 ??  ?? Figure 3: Pipeline syntax -- snippet generator
Figure 3: Pipeline syntax -- snippet generator
 ??  ?? Figure 4: Blue Ocean dashboard
Figure 4: Blue Ocean dashboard
 ??  ??
 ??  ??
 ??  ??
 ??  ?? Figure 5: Pipeline in Blue Ocean
Figure 5: Pipeline in Blue Ocean
 ??  ?? Figure 7: YAML pipeline with stages
Figure 7: YAML pipeline with stages
 ??  ??
 ??  ??
 ??  ?? Figure 6: Pipeline as a YAML
Figure 6: Pipeline as a YAML
 ??  ?? Figure 8: Replay pipeline
Figure 8: Replay pipeline
 ??  ?? Figure 9: Valid pipeline declarativ­e script
Figure 9: Valid pipeline declarativ­e script
 ??  ??
 ??  ??
 ??  ?? Figure 10: Convert to pipeline declarativ­e script – validate pipeline
Figure 10: Convert to pipeline declarativ­e script – validate pipeline
 ??  ?? Figure 11: Convert to pipeline declarativ­e script – valid pipeline
Figure 11: Convert to pipeline declarativ­e script – valid pipeline

Newspapers in English

Newspapers from India