CI/CD using Jenkins , Docker on AZURE Virtual machine [ Ubuntu ] with Github Integration.
Surya Shankar
Posted on September 20, 2022
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.
Prerequisite :
You must have an website/web application [In this project , I have used a Web application present in my github repository]
STEP-1 : Create a Virtual machine in azure ( I have used Ubuntu machine )
SSH into that server and create a folder naming dockerfiles
Inside that folder , Create one Dockerfile and one yml[docker-compose.yml]
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.
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.
STEP-2 : Create a Personal access token and a github repository
Personal Access token in github
Now push all files into your repository .. Here you can use you personal access token as a password
STEP-3 : Jenkins Installation
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 ]
Paste your publicip:8080 in browser and put the password to unlock jenkins
Create your admin user
Ist of all we Have to setup an agent [node]
Inside remote directory put your dockerfiles folder location
Select use websocket and save
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.
Now click on configuration setting and connect your github
Add Jenkins
Select Secreat text
Inside secreat paste your github access token
Now select your id
Test the connection and save
Create a job
let's create a Freestyle project to build and run the project stored in the GitHub repository:
In the Source Code Management section, we need to select the repository where we pushed our code.
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:
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
Save it
Go to the respective job that we want to run and click on the "Build Now" link highlighted in the below image:
Now your Continous integration pipeline was created .. you can paste your publicip:8000 in browser to watch your application
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
Now build it
Dockerfile build process console output shown below
Now if you have done some changes in your github , it will reflict automatically
Just click on build now and your changes will be reflected here
No need to manually kill the container
As you can see our website working properly.
Posted on September 20, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 20, 2022