Project 2 - Day 81 Deploying web app using Jenkins CI/CD declarative pipeline.

Project 2 - Day 81 Deploying web app using Jenkins CI/CD declarative pipeline.

Description

The project is about automating the deployment process of a web application using Jenkins and its declarative syntax. The pipeline includes stages like building, testing, and deploying to a staging environment. It also includes running acceptance tests and deploying to production if all tests pass.

Pipeline syntax

pipeline {
    agent any
    stages {
        stage("Code") {
            steps {
                echo "Code Cloned"
            }
        }
         stage("Build") {
            steps {
                echo "Code Build"
            }
        }
         stage("Test") {
            steps {
                echo "Code Test"
            }
        }
         stage("Deploy") {
            steps {
                echo "Code Deploy"
            }
        }
    }

Steps:

Log in to the AWS console

Create an EC2 instance (Ubuntu)

Now connect to the instance using the SSH client

Go to the download folder, where the .pem file is placed and open the terminal in the same location, and paste the SSH.

This will connect your terminal to the EC2 instance

Let's install Jenkins on this EC2 instance

jenkins.io/doc/book/installing/linux

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

Install the Docker service by using the below command

sudo apt install docker.io -y

Let's check all the services, we installed till now

java --version
sudo systemctl status jenkins
sudo systemctl status docker

# Add the ubuntu/jenkins user to docker group and reboot to get previlaged access
$ sudo usermod -aG docker $USER

Run some docker commands in the terminal

# Check the docker images
$ docker images

Allow ports 8080 for Jenkins and 8000 for docker from a security group > inbound rules

Now, Copy the Public IP of the EC2 machine and paste it to the browser to access the Jenkins portal as "PublicIP:8080"

Goto Jenkins Dashboard and Click on “New Item”

Name: declarative_pipeline

Select: Pipeline

Navigate to the pipeline section and try a sample pipeline

Click on “Save”.

Click on “Build Now”.

Once it will be completed, It will return as “Success”

Deploy a simple todo using a declarative pipeline

Click on Configure

Navigate to the pipeline and start writing the pipeline script

pipeline {
    agent any

    stages{
        stage ("Get the code from repo") {
            steps {
                git branch:"develop", url: "https://github.com/rjthapaa/django-todo-cicd.git"
            }
        }

        stage ("Build image from docker file") {
            steps {
                sh "docker build . -t djangoimage:latest"
            }
        }

        stage ("run the image and deploy container") {
            steps {
                sh "docker run --name=djangoc -d -p 8000:8000 djangoimage:latest"
            }
        }
    }
}

Apply and save

Click on Build Now

Once it will be completed, It will return as “Success”

Now, Search for the Public IP with port 8000 in the browser,

For more Jenkins projects, please read the below-listed blogs

https://rjthapaa.hashnode.dev/day-27-jenkins-declarative-pipeline-project-with-docker

https://rjthapaa.hashnode.dev/day-28-jenkins-master-slave