Let's create a ci/cd pipeline with Jenkins: 
Tags: jenkins, devops, ciandcd
Published: 2020-11-21 19:24:34

## __What is Jenkins :__
Jenkins is a opensource tool used for automating the application deployment and delivery . Jenkins can be used for building , packaging , testing and deploying the application . Which is more often called as a ci/cd pipeline .

## __What is ci/cd pipeline :__
ci/cd pipeline is a part of __Devops__ lifecycle it comes under deployment phase . Where we have to pass our application through version control , building ,  testing and then deploy our application to the testing environment first secondly to the production environment this phase is completely automated by using a continuous integration and continuous deployment pipeline so that we make sure our application delivery time is reduced .  

![ci/cd pipeline](https://d1h3p5fzmizjvp.cloudfront.net/wp-content/uploads/2019/08/29115129/CICD-pipeline-1024x354.png)

## __Install and configure Jenkins :__
Here we are doing it on ubuntu machine you can do it on any operating system your choice .

## __Installing Jenkins :__

* before installing make sure you have java install .

```$sudo apt-get install openjdk-11-jdk```

```$wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -```

```$sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > \
    /etc/apt/sources.list.d/jenkins.list'```

```$sudo apt-get update```

```$sudo apt-get install jenkins```

### __Enabling Jenkins to run :__

by default Jenkins is running on port 8080 so we have to enable Jenkins on the system startup .

``` $sudo systemctl status jenkins ```

``` $sudo systemctl enable jenkins ```

### __opening the firewall :__

If you have firewall enabled make sure it accepts all the incoming request through port 8080 .

``` $sudo ufw status ```
``` $sudo ufw allow 8080 ```

### __setting up jenkins :__

Then open go to port 8080 on your local machine 
``` http://localhost:8080 ```
if you are running it on a remote machine then simply replace the host ip or domain instead of localhost .
``` http://host_ip:8080 ```
* set up the default password by navigating to the directory .

![set up jenkins](https://dev-to-uploads.s3.amazonaws.com/i/y4lzgsekpqspdco79riz.png)

* install suggested  plugins .

![suggested plugins](https://dev-to-uploads.s3.amazonaws.com/i/03xkit3lb0mabzck77a8.png)

## __Lets create a pipeline :__
1. Create a webhook with GitHub so when we push the code to GitHub it can automatically do the desired step(Building, packaging, testing .) 
* Go to your GitHub repository .
* Then settings .
* Then Webhook .
* Then Type the URL of your server with 
```$http://(ip address):8080/github-webhook/```
2.Then in Jenkins you have to create a new job .
* Add git as a version code and repository URL .
* Then under the build triggers select

![build triggers](https://dev-to-uploads.s3.amazonaws.com/i/0cbs5jp0hc68u122tjlx.png)

* Then under build select execute shell
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e4dsb8kdxakvgmw65b0j.png)

* Then when you push your code to the github you can see a build status
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mt4yocf7ddivbw5hwwt4.png)

## __Multi tire architecture :__
In real world scenario deploying an application passes through multiple stages . 
1. First the developer has to write the code and push the code to version control system .
2. Through WebHook which is triggering point when ever someone pushes code to github it will trigger some work and in our case it is pushing the code to Jenkins .
3. Then we have to run the test cases if everything is fine, the code might goes under a penetration testing environment then if it will meet all the business requirements then finally to the production server .

![CI/CD pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsvq163iz9he7m15c3mr.png)

Above image shows a minimalistic process. Where as different organization follow different steps before pushing there code to the production server.