How to create a pipeline in Jenkins?
In today’s blog post, we will discuss what is a pipeline in Jenkins, how many types of pipelines available in Jenkins and how to create a pipeline in Jenkins.
What is pipeline in Jenkins?
A pipeline in general refers to a set of tools that is used to perform some action on somewhere. Pipelines are mostly used to test, build and deploy some code on a target environment using DevOps methodologies. A pipeline in Jenkins is a combination of plugins that enables you to perform CI/CD operations using Jenkins.
Jenkins’s pipeline consists of below components:
- A Source (Source code of application/program that you want to execute through the pipeline, generally stored in a version control system like GitHub)
- Jenkins pipeline definition file known as Jenkinsfile which tells the pipeline what action to perform.
- A target where the pipeline will get executed.
What is a Jenkinsfile?
The definition of a Jenkins Pipeline is written into a text file named Jenkinsfile. A Jenkinsfile can be written using two types of syntax – Declarative and Scripted. Declarative and Scripted Pipelines are constructed fundamentally differently. Declarative Pipeline is a more recent feature of Jenkins Pipeline which provides richer syntactical features over Scripted Pipeline syntax, and is designed to make writing and reading Pipeline code easier.
Note: Many of the individual syntactical components (or “steps”) written into a Jenkinsfile, however, are common to both Declarative and Scripted Pipeline.
Note: You can also create a pipeline using UI but, in the backend, it also uses a Jenkinsfile to store the pipeline definition.
Note: A Declarative pipeline definition has more features and controls over the Scripted pipeline and we will cover mostly Declarative pipeline in this Jenkins series.
Components of a Declarative pipeline in Jenkins:
A Pipeline is a user-defined model of a CD pipeline. A Pipeline’s code defines your entire build process, which typically includes stages for building an application, testing it and then delivering it. A pipeline block is a key part of Declarative Pipeline syntax.
The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed inside pipeline.
post section defines one or more additional steps that are run upon the completion of a Pipeline’s or stage’s run (depending on the location of the post section within the Pipeline).
Containing a sequence of one or more stage directives, the stages section is where the bulk of the “work” described by a Pipeline will be located. At a minimum, it is recommended that stages contain at least one stage directive for each discrete part of the continuous delivery process, such as Build, Test, and Deploy.
The steps section defines a series of one or more steps to be executed in a given stage directive.
The environment directive specifies a sequence of key-value pairs which will be defined as environment variables for all steps, or stage-specific steps, depending on where the environment directive is located within the pipeline.
The options directive allows configuring Pipeline-specific options from within the Pipeline itself. Pipeline provides a number of these options, such as buildDiscarder, but they may also be provided by plugins, such as timestamps.
The input directive on a stage allows you to prompt for input, using the input step. The stage will pause after any options have been applied, and before entering the agent block for that stage or evaluating the when condition of the stage. If the input is approved, the stage will then continue. Any parameters provided as part of the input submission will be available in the environment for the rest of the stage.
The parameters directive provides a list of parameters that a user should provide when triggering the Pipeline. The values for these user-specified parameters are made available to Pipeline steps via the params object.
The triggers directive defines the automated ways in which the Pipeline should be re-triggered. For Pipelines which are integrated with a source such as GitHub or BitBucket, triggers may not be necessary as webhooks-based integration will likely already be present. The triggers currently available are cron, pollSCM and upstream.
The tools directive defined the set of tools that needs to be auto-install and put on the PATH so that your pipeline can use those tools. For example, your source code may need some external tool for testing and using tool directive you can pre-install those tools before your pipeline gets executed. Tools block is ignored if agent none is specified.
The when directive allows the Pipeline to determine whether the stage should be executed depending on the given condition. The when directive must contain at least one condition.
Stages in Declarative Pipeline may have a matrix section defining a multi-dimensional matrix of name-value combinations to be run in parallel. We’ll refer these combinations as “cells” in a matrix. Each cell in a matrix can include one or more stages to be run sequentially using the configuration for that cell.
How to create a pipeline in Jenkins?
Step 1: Login to your Jenkins dashboard and check if nodes are available with labels.
We are going to use the “DEV_BUILD” node which has a label “dev”.
Step 2: Create your 1st Jenkins pipeline.
On Jenkins dashboard home page, click “Create a job”.
If you already created some pipeline project then click “Add Item” to create a new pipeline
Provide a name to your project and select “Pipeline” and click “OK”.
On the “Advance Project Options”, select “Pipeline script” in Definition and paste below pipeline definition.
echo 'building your app!'
echo 'testing your app!'
echo 'deploying your app!'
Click “Save” once done.
Note: Replace the agent block as per your Jenkins setup.
Step 3: Trigger your Jenkins pipeline manually.
Click “Build Now” to start your pipeline execution.
Our pipeline execution completed successfully.
If you don’t like the default Jenkins pipeline UI and installed “Blue Ocean” plugin then click on “Open Blue Ocean”.
Click on the pipeline to view details.
Hope you have enjoyed this article, to get more details on Jenkins, please refer below Jenkins official documentation.