CI/CD using Jenkins , Docker on AZURE Virtual machine [ Ubuntu ] with Github Integration.
Tags: docker, azure, aws, devops
Published: 2022-09-20 04:23:11

## What is a CI/CD pipeline?
**A CI/CD pipeline automates your software delivery process. The pipeline builds code, runs tests (CI), and safely deploys a new version of the application (CD).**
**Automated pipelines remove manual errors, provide standardized feedback loops to developers, and enable fast product iterations.**


## IMPORTANT STEPS :

**- Create a Web Application.**
**- Azure Virtual Machine**
**- Create a Dockerfile and docker-compose.yml.**
**- Jenkins Installation.**
**- Github Integration using personal access token.**
**- Jenkins Node configuration and Job setup.**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ghpzpopqzmfc2kos8md.png)

## Prerequisite :

**You must have an website/web application [In this project , I have used a Web application present in my github repository]**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6jkouzsvmqs8lpnq0usv.png)


## STEP-1 : Create a Virtual machine in azure ( I have used Ubuntu machine )

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3tq09po11005norfup8.png)

**SSH into that server and create a folder naming dockerfiles**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bufzudfb687s0rpbi28y.png)
**Inside that folder , Create one Dockerfile and one yml[docker-compose.yml]**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqmggmna9gwro63e0hkf.png)

**Creating Dockerfile**

**A Dockerfile is a script that automatically creates containers on the Docker platform.** 
**A Dockerfile is basically a text document that contains all the commands a user could call on the command line to assemble an image.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srhbahhk8iv5n0m731ib.png)

**The FROM instruction sets the Base Image for subsequent instructions. As such, a valid Dockerfile must have FROM as its first instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.**

**The ADD instruction add's the local files into the containers specified path.**

**The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.**

**The CMD instruction should be used to run the software contained by your image, along with any arguments. We use this to start our tomcat inside the image.**

**Creating docker-compose.yml as shown below**

**Docker Compose is a tool that was developed to help define and share multi-container applications.** 
**With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5znfkyyiioqs5q5whoz3.png)

## STEP-2 : Create a Personal access token and a github repository

**Personal Access token in github**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afedcgikyp25guh5t22m.png)

**Github repository**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cb6vdd9xfi44epbexb49.png)

**Now push all files into your repository .. Here you can use you personal access token as a password**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvjqxgpn094uwy6zu2im.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5n6alm5cda1f05eucejx.png)

## STEP-3 : Jenkins Installation

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d3r6wtrurxx8n19s9iv.png)

**Jenkins is an open-source automation tool written in Java with plugins built for Continuous Integration purposes.** 
**Jenkins is used to build and test your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build.** 
**It also allows you to continuously deliver your software by integrating with a large number of testing and deployment technologies.**

**Step - 1 Install Java**

```
sudo apt update
Install java
sudo apt install openjdk-11-jre
```


**Step - 2 Install Jenkins**

**Just copy these commands and paste them onto your terminal.**


```
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update 
sudo apt-get install jenkins
```


**Step -3 Start jenkins**

```
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
```
**After that go back to your virtual machine and add a inbound port 8080 into it. [ As jenkins always runs on a port 8080 ]**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ky6kbzdurml147accsn.png)

**Paste your publicip:8080 in browser and put the password to unlock jenkins**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6mj1obejjrhrs89d1kh.png)

**Create your admin user**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrjg4suv9fvf9rd79dcg.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lm2tn1iuppbo8sts78jg.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhttpaimyaqplaxt18gv.png)
**Jenkins Dashboard**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cl5p5qom9kxqndb1853b.png)

**Ist of all we Have to setup an agent [node]**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6evm43zojld9q1ej0wp.png)
**Inside remote directory put your dockerfiles folder location**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n126qm52sovsoeheb1lx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uybsuxi9hkmcmxbldytp.png)
**Select use websocket and save**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca8mfysetlyz5jsoysnk.png)

**After that inside managed plugins , Make sure you have git plugins installed there**

**Jenkins achieves Continuous Integration with the help of plugins. Plugins allow the integration of Various DevOps stages. If you want to integrate a particular tool, you need to install the plugins for that tool. For example Git, Maven 2 project, Amazon EC2, HTML publisher etc.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41q4o53hecg06sp2ycbo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bia7mmbdnqpaulw2u72s.png)

**Now click on configuration setting and connect your github**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zm5owt3nio18b03ppfld.png)
**Add Jenkins**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22tvlqgerm6uoqo6e8n5.png)
**Select Secreat text**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scl0mi2n9oar9qjweily.png)
**Inside secreat paste your github access token**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j0agbu7xxw2d21vnubar.png)
**Now select your id**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fsjjtl2f2vxyse490d48.png)
**Test the connection and save**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lziy6e8ir11vrpfrspce.png)
**Create a job** 

**let's create a Freestyle project to build and run the project stored in the GitHub repository:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcux7u6f0l5ygkofmepx.png)
**In the Source Code Management section, we need to select the repository where we pushed our code.**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jql5i4gso42fgqver0nl.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqw9heop6sg6silam3w9.png)
**As soon as we will click on the above option, we will see the text area in which we will write the commands mentioned below:**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqyvxzmectdw1v163f31.png)
**Here ist put your Dockerfile path**

```
cd /dockerfiles
```
**Run docker build and container run command**

```
docker image build -t weatherapp
docker container run -itd -p 8000:80 weatherapp
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i3kzgm410tibq0hq5pg.png)
**Save it**
**Go to the respective job that we want to run and click on the "Build Now"  link highlighted in the below image:**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik29a0tjacl4qzcwy3q3.png)
**Now your Continous integration pipeline was created .. you can paste your publicip:8000 in browser to watch your application**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ba9cu3t0esohuqmnkb7z.png)

**Now the problem is , If you do some changes in your github then you need to ist kill the running container and build a new container to integrate that changes.**
**So in order to create a Continous deployment you need to run docker-compose file**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6ph0xntoi7rg7alqt23.png)
**Now build it**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uww70qafxbbxte74zdec.png)
**Dockerfile build process console output shown below**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06d0nnl3zvbkemt5vxlk.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twn8hfx94wo0lgl5g6zh.png)
**Now if you have done some changes in your github , it will reflict automatically**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04msilwdptyw3i0xgqcg.png)
**Just click on build now and your changes will be reflected here**
**No need to manually kill the container**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fypsx2e4lmgcktu9chhz.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hzm1r2p82v79i1ndcpw.png)

As you can see our website working properly.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93fx2b1b0eu8udug09b2.png)