Jenkins Declarative CI/CD Pipeline on AWS

Avijit DeyAvijit Dey
6 min read

Introduction

Continuous Integration and Continuous Deployment (CI/CD) pipelines have become essential in modern software development. Jenkins is a widely used automation server that facilitates the creation of robust CI/CD pipelines. This project focuses on implementing a Jenkins Declarative CI/CD pipeline using AWS services to automate the deployment process of a web application named Django-notes-app.

Objective

The main objective of this project is to set up a Jenkins Declarative CI/CD pipeline on Amazon Web Services (AWS) for Django-notes app. This pipeline should automate the build, test, and deployment processes. This project will be fully based on the Declarative pipeline and we will deploy Django- notes-app. The GitHub repository link is pasted below: -

https://github.com/avijitdey2002/django-notes-app

Tools and Technologies

Jenkins

Jenkins is an open-source automation server that helps automate parts of the software development process, including building, testing, and deploying code. It offers a wide range of plugins and integrations, making it a popular choice for CI/CD.

Docker

Docker is a platform and set of tools designed to make it easier to create, deploy, and run applications within containers. Containers are lightweight, stand-alone, and executable packages that include everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings.

AWS (Amazon Web Services)

Amazon Web Services (AWS) is a cloud computing platform that provides a variety of cloud services, including computing power, storage, and database services. In this project, AWS services such as EC2, S3, and Elastic Beanstalk will be used to build and deploy the application.

Docker-Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define a multi-container environment in a single YAML file, specifying the services, networks, and volumes required for your application's stack. With Docker Compose, you can easily manage and orchestrate the deployment of multiple containers that make up your application, simplifying the development, testing, and deployment process.

DockerHub

Docker Hub is a cloud-based platform provided by Docker, Inc. that serves as a central repository for Docker images. It is one of the most popular container image registries and plays a critical role in the Docker ecosystem. Docker Hub allows developers to share, distribute, and access container images easily.

GitHub

GitHub is a web-based platform and hosting service for version control using Git. It is one of the most widely used platforms for software development, collaboration, and project management. GitHub provides a range of features and tools that facilitate the entire software development lifecycle.

Implementation Steps

  1. Create an EC2 instance on your AWS account. Set the Amazon Machine Image(AMI) as Ubuntu and the Instance type as t2.micro. Create a security group to create a firewall for security.

  2. Using SSH, connect to the instance. As you can see my instance is connected to my system and ready to be used.

a. Very first thing you should do is to run this command sudo apt-get update it will update your system.

b. We have to clone the code to our new EC2 instance, to do so use below command:

https://github.com/avijitdey2002/django-notes-app.git

c. Change the working directory using cd django-notes-app/ then run ls you will get to see all project required files.

d. We are going to build our code on docker so to install docker use sudo apt install docker.io

e. To check if the docker is installed, run docker --version .

f. We have docker installed in the instance, but our user is not added in the docker group to perform any docker actions. So we have to give docker permissions to our user, to do so run sudo usermod -aG docker $USER .

g. Then reboot our system just to synchronize the changes we have made, run sudo reboot (It may take some time).

h. Run docker ps command to see the status of the process. If you can run this command without any error message, then you have the docker permission now.

i. To access the Dockerfile present inside the django-notes-app file we use cat Dockerfile. Dockerfile is a set of instructions and contains all the requirements/ dependencies which are needed to run the code.

j. Finally, run docker build -t notes-app . and notice the changes which were provided in the docker file.

We can see that our build was successful.

  1. Install Java and Jenkins on our EC2 instance.

    a. Run sudo apt update sudo apt install openjdk-17-jre . To check if java is installed we use java -version command.

    b. To install Jenkins , we use the following commands -

     curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.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-stable binary/ | sudo tee \
         /etc/apt/sources.list.d/jenkins.list > /dev/null
    
      sudo apt-get update
      sudo apt-get install fontconfig openjdk-11-jre
      sudo apt-get install jenkins
    

    c. To check if the Jenkins server is active and running we use service jenkins status command.

d. Copy the public IP of the EC2 instance and paste it in a new tab followed by the port number :8080. Before doing so , make sure that you have added this port number in the inbound rules of the security group in the EC2 instance . Jenkins login page will be displayed. Now, assign an username and password to your Jenkins account . Configure all the login settings and proceed to the next step.

No alt text provided for this image

    1. Create a pipeline and add the GitHub repository URL
  1. No alt text provided for this image

5. Install Docker-compose to centrally manage the deployments of many different docker containers.

Run the command to install docker-compose sudo apt-get install docker-compose, You can find docker-compose.yml file in my GitHub repository.

Docker-compose will help to DOWN and UP the environment while releasing new changes. (In Live).

6. Now write the pipeline script (Jenkinsfile) for code checkout, image build, pushing to dockerHub and deploy. You can find the script in: https://github.com/avijitdey2002/django-notes-app

pipeline {
    agent any 

    stages{
        stage("Clone Code"){
            steps {
                echo "Cloning the code"
                git url:"https://github.com/LondheShubham153/django-notes-app.git", branch: "main"
            }
        }
        stage("Build"){
            steps {
                echo "Building the image"
                sh "docker build -t my-note-app ."
            }
        }
        stage("Push to Docker Hub"){
            steps {
                echo "Pushing the image to docker hub"
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                sh "docker push ${env.dockerHubUser}/my-note-app:latest"
                }
            }
        }
        stage("Deploy"){
            steps {
                echo "Deploying the container"
                sh "docker-compose down && docker-compose up -d"

            }
        }
    }
}

7. Run the pipeline by clicking on BUILD NOW .

  1. After multiple attempts and troubleshooting, the pipeline is comepletely automated when we add Webhooks to it.

As we can see the pipeline is running smoothly and our Notes app is deployed successfully using Webhooks

Conclusion

The implementation of a Jenkins Declarative CI/CD pipeline using AWS has successfully automated the build, test, and deployment processes for our web application - Django-Notes-App. This project streamlines the development and deployment workflow, ensuring code quality and consistent deployments. Ongoing improvements and optimizations will continue to enhance the efficiency and reliability of the CI/CD pipeline.

This project demonstrates the power of combining industry-standard CI/CD practices with the scalability and flexibility of AWS cloud services to deliver high-quality software efficiently.

0
Subscribe to my newsletter

Read articles from Avijit Dey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Avijit Dey
Avijit Dey