Creating Pipeline in Jenkins
Tags: devops, beginners, programming, tutorial
Published: 2022-01-04 21:51:47

If you want to get started in DevOps, Jenkins is most important and handy software you should learn.In this article, I will run you through how to install Jenkins and create first  Pipeline

Jenkins can be installed in numerous ways, I will be using  docker to run the jenkins locally.

To run the below command you need docker.

`docker run --name jenkin-master
 -d -v /Users/shiva/jenkins_home:/var/jenkins_home 
 -p 8080:8080 jenkins/jenkins:jdk11 `

This command downloads the jenkins:jdk11 image and runs it, **_-v_** create a volume to maintain data persistence, runs the Jenkins in port 8080 with name jenkin-master

Go to http://localhost:8080 to access the Jenkins. You will see below screen

![First screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s02py47v4xqrogmg1vvb.png)

To access the path given in the Jenkins page, we need to log into the docker image with below command

`docker exec -it jenkin-master /bin/bash`

**_-it_** stands for interactive terminal

Once you are in, use the below command to get the password
`cat /var/jenkins_home/secrets/initialAdminPassword`

In Next screen, select _Install suggested plugins_, once its done you can create a new admin user.


![create new user](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9sta8kr1a6qze91ywoi3.png)

click next and start using jenkins

You will land on this page.


![Home page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj9705yd76bb8ndw1ofl.png)

On the left you will see the menu, click on **_New Item_** , then select **_Pipeline_** and give it a name _**firstJob**_

Pipeline is a series of step which helps in building the software. In other words, pipeline is used to implement and integrate continuous delivery. 

There are two types of pipeline,
-> scripted &
-> declarative
We are going to write our first declarative pipeline.

After creating your **_firstJob_** scroll down to Pipeline and write the below code in the editor

{% gist https://gist.github.com/rshiva/9dea1137bea54fe7103f468f25193b64 %}

**_Pipeline_** -> All the declarative scripted pipeline starts with a pipeline block
**_agent_** -> instructs Jenkins to run the stages on any agent or node
**_stages_** -> Stages contain one or more stage.
**_stage_** -> Stage reside inside the stages and it should have a steps block. Every stage will have a name like **Test, Build, Deploy**. Here we named our stage as **Hello**
**_steps_** -> Steps are written inside the stage and it contains list of commands or scripts that need to run. Here we just wanted to echo "Hello World."
 

Once you have saved your first declarative Pipeline, let's run it. Click on **_Build Now_**. After the build is done, you can click on **build number 1** from build history then in the left menu click on **_Console Output_** to view the output of the build

![Menu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/waq9dvczamaswe3nzqov.png)
 

As you can see, the "Hello World" is successfully printed in the output console.

![console output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpkcwm5kxj5ua2kmpfr1.png)
 
Congratulations! You have created your first pipeline in Jenkins, We will use this as a base to build complete continuous delivery.