Build  Declarative CI/CD Pipeline  with AWS, Docker, and Jenkins

Build Declarative CI/CD Pipeline with AWS, Docker, and Jenkins

Projrct-1

ยท

5 min read

A continuous integration and continuous deployment (CI/CD) pipeline is a series of steps that must be performed in order to deliver a new version of software. that help us build, test, and deploy software quickly and efficiently.

Project Overview

Our project is a Django-notes-app web application .To automate the deployment process of this application, we have set up a CI/CD pipeline using Jenkins. The pipeline consists of several stages, including code checkout, building Docker images, deploying services to servers using Docker Compose, updating the Docker registry, on the server.

By setting up this pipeline, we aim to streamline the deployment process of our django web application and ensure that each new change to the codebase is automatically deployed to the production/ testing/ development environment. This approach will save us time and effort while ensuring the reliability and scalability of our application.

Pre-requisites

  1. A GitHub account to store the source code.

  2. One server/machine : Ec2 Instance

  3. Jenkins, Docker and Docker Compose are installed.

  4. Docker registry to store the Docker-versioned images.

  5. Knowledge of Groovy syntax to create the Jenkins pipelines.

Let's get started--->

Step 1: Get the source code of this Django web App

Refer this repository:

https://github.com/Akshay-aan/my-app.git

Step 2: Make the server ready for deployment purpose

Create ec2 instances, one as the master node and the other as Agent nodes for deployment purposes.

Instances can be t2-micro, 1 CPU, 1 GiB Memory, and ports 8080 , 80, 22 ,8000 are open to allow incoming traffic

Step 3: Setup these server to use Jenkins

  1. Jenkins Master Node must have Jenkins installed.

  2. Jenkins node must have Java, docker, docker-compose installed and configured.

    sudo apt install docker-compose -y for docker-compose installation

    Just follow the commands given below and you get it installed on your machine.

Install Docker

$ sudo apt-get update

$ sudo apt-get install docker.io

$ sudo usermod -aG docker $USER

$ sudo reboot

$ sudo docker run hello-world

$ docker ps

Install Jenkins

Jenkins requires Java to run, so first install Java -->

$ sudo apt-get update

$ sudo apt install openjdk-11-jre

$ java -version

Long-Term Support release of Jenkins---->

$ 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 jenkins

$ jenkins --version

Access your Jenkins on Localhost[EC2](Localhost[EC2) [instance public ip]:8080

Step 4: Create a Jenkins Pipeline project

Step 5: Set the GitHub webhook to trigger the project pipeline

Go to the GitHub repository that you forked in Step 1 -->

If you find a green tick mark then you are done!

Step 6: Create a Jenkinsfile for our pipeline to run

pipeline {
    agent any

    stages{
        stage("code"){
            steps{
                echo "cloning the code"
                git url:"https://github.com/Akshay-aan/my-app.git", branch: "main"
            }
        }
        stage("Build"){
            steps{
                echo "Building the image"
                sh "docker build -t note-app-image ."
            }
        }
        stage("Push to Docker Hub"){
            steps{
                echo "pushing the image to docker hub"
                withCredentials([usernamePassword(credentialsId:"Jenkinid",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                sh "docker tag note-app-image ${env.dockerHubUser}/note-app-image:latest"
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                sh "docker push ${env.dockerHubUser}/note-app-image:latest"
                }

            }
        }
        stage ("Deploy"){
             steps{
                echo "Deploying the container"
                sh "docker-compose down && docker-compose up -d"

            }
        }
    }

The pipeline has several stages that are executed sequentially:

  1. "cloning the code" stage: This stage checks out the code from the GitHub repository using Git.

  2. "Building docker image" stage: This stage builds Docker images for the container application

  3. "Upload docker image to dockerHub" stage: This stage updates the Docker images on DockerHub by pushing the newly built image

    The pipeline is executed and sets an environment variable DOCKER_IMAGE_TAG using the build number. The pipeline uses several shell scripts to perform the Docker-related tasks . The pipeline also uses credentials to log in to DockerHub and push images.

  4. "Deploy the container" stage: This stage deploys the Docker containers for the application using docker-compose.

Step 7: Run the Pipeline manually to check for any errors

Step 8: Check whether the docker image successfully upload to dockerHub or not

The build Succeeded and we can access the Application on development-server

Step 9: Validate The Output of our project

Go to the ec2 instance we created get the public ip and validate

http://[ec2 instance public ip]:8000

Done!

๐Ÿ‘๐Ÿผโœ”๏ธโœ…โ˜‘๏ธ๐Ÿ

Summary:

Building a Declarative CI/CD Pipeline with AWS, Docker, and Jenkins enables organizations to streamline their software development and deployment processes. By integrating Jenkins, a powerful automation tool, with AWS cloud services and Docker containers, teams can create a seamless and scalable pipeline for continuous integration and continuous delivery (CI/CD) of their applications. The declarative approach simplifies pipeline configuration, making it easier to define and manage the steps involved in building, testing, and deploying software. the flexibility and scalability of AWS, combined with Docker's containerization technology, allows developers to deploy applications consistently and efficiently across various environments.

Hope you learn something today from this blog!

Happy Learning!

This is my first blog ..Stay tuned for my next blog . I will keep sharing my learnings and knowledge here with you.

Let's learn together! I appreciate any comments or suggestions you may have to improve my blog content.

Thank you,

Akshay Nazirkar

ย