DEV Community

Cover image for CI/CD using Jenkins , Docker on AZURE Virtual machine [ Ubuntu ] with Github Integration.
Surya Shankar
Surya Shankar

Posted on • Updated on

CI/CD using Jenkins , Docker on AZURE Virtual machine [ Ubuntu ] with Github Integration.

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

Prerequisite :

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

Image description

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

Image description

SSH into that server and create a folder naming dockerfiles

Image description
Inside that folder , Create one Dockerfile and one yml[docker-compose.yml]

Image description

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

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

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

Personal Access token in github
Image description

Github repository
Image description

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

Image description

Image description

STEP-3 : Jenkins Installation

Image description

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step -3 Start jenkins

sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
Enter fullscreen mode Exit fullscreen mode

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

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

Image description

Create your admin user

Image description

Image description
Image description
Jenkins Dashboard

Image description

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

Image description
Inside remote directory put your dockerfiles folder location
Image description
Image description
Select use websocket and save
Image description

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
Image description

Now click on configuration setting and connect your github

Image description
Add Jenkins
Image description
Select Secreat text
Image description
Inside secreat paste your github access token
Image description
Now select your id
Image description
Test the connection and save
Image description
Create a job

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

Image description
In the Source Code Management section, we need to select the repository where we pushed our code.
Image description
Image description
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
Here ist put your Dockerfile path

cd /dockerfiles
Enter fullscreen mode Exit fullscreen mode

Run docker build and container run command

docker image build -t weatherapp
docker container run -itd -p 8000:80 weatherapp
Enter fullscreen mode Exit fullscreen mode

Image description
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
Now your Continous integration pipeline was created .. you can paste your publicip:8000 in browser to watch your application

Image description

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
Now build it
Image description
Dockerfile build process console output shown below
Image description
Image description
Now if you have done some changes in your github , it will reflict automatically

Image description
Just click on build now and your changes will be reflected here
No need to manually kill the container

Image description

Image description

As you can see our website working properly.

Image description

Top comments (0)