How to create a pipeline in Jenkins with an example.

The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository.
Some of immediate benefits are :
Automatically creates a Pipeline build process for all branches and pull requests.
Code review/iteration on the Pipeline (along with the remaining source code).
Jenkinsfile can be written using two types of syntax -
- Declarative
- 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.
Declarative Pipeline :
The syntax of declarative pipeline is

- In the above syntax pipeline A Pipeline’s code defines your entire build process, which typically includes stages for building an application, testing it and then delivering it.
- Execute this Pipeline or any of its stages, on any available agent.
- Define the different stages such Build,Test,Deploy.
- Perform some steps related to the stage.
Scripted Pipeline:

A node is a machine which is part of the Jenkins environment and is capable of executing a Pipeline,and also, a node block is a key part of Scripted Pipeline syntax.
A stage block defines a conceptually distinct subset of tasks performed through the entire Pipeline (e.g. “Build”, “Test” and “Deploy” stages).
A single task. Fundamentally, a step tells Jenkins what to do at a particular point in time (or “step” in the process). For example, to execute the shell command make use the sh step: sh ‘make’.
Ways to create a pipeline with example:
There are several ways to create a pipeline such as
- Blue ocean
- Classic UI
- SCM
We will discuss classic UI and SCM here
SCM:
- Click on new Item on left side of dashboard.
- Enter the pipeline name and select pipeline option and click on ok.
3 . You will less options compare to the freestyle job.
4 . Click on pipeline tab and select pipeline script from SCM option from the dropdown.
5 . Before entering the Git URL need to create a jenkinsfile and commit to the repository.
6 . I have created a jenkinsfile and committed to the repository.
7 . Now fill all the inputs such as git project url and creds and mention the correct branch name, specify the jenkinsfile path and save it.
8 . Now build the project.
9 . After build successfully you can see the pipeline on left side like this specified by the stages.
10 . In console output you can see the steps.
Classic UI:
- Click on new Item on left side of dashboard.
- Enter the pipeline name and select pipeline option and click on ok
3. Click on pipeline tab and select pipeline script option from the drop down.
4. And now paste the script below and save it .
5 . Now click on the build now and see the result build is successful with 3 stages Build,Test and Deploy.
Hope all you got how to create a pipeline in Jenkins If not please read my previous blog about About How to setup and create a first Job in Jenkins.