Real-World CI/CD DevSecOps Pipeline for Deployment of Python Web-app

Β·

6 min read

Hello friends, we will be deploying a Python Web-app Based Application. This is an everyday use case scenario used by several organizations. We will be using Jenkins as a CICD tool and deploying our application on Docker Contaier. Hope this detailed blog is useful.

Steps:-

Step 1 β€” Create an Ubuntu T2 Large Instance

Step 2 β€” Install Jenkins, Docker and Trivy. Create a Sonarqube Container using Docker.

Step 3 β€” Install Plugins like JDK, Sonarqube Scanner, Maven, OWASP Dependency Check,

Step 4 β€” Create a Pipeline Project in Jenkins using Declarative Pipeline

Step 5 β€” Install OWASP Dependency Check Plugins

Step 6 β€” Docker Image Build and Push

Step 7 β€” Deploy image using Docker

Step 8β€” Access the Real World Application

Step 9β€” Terminate the AWS EC2 Instance

References

Now, lets get started and dig deeper into each of these steps :-

Step 1 β€” Launch an AWS T2 Large Instance. Use the image as Ubuntu. You can create a new key pair or use an existing one. Enable HTTP and HTTPS settings in the Security Group.

Step 2 β€” Install Jenkins, Docker and Trivy

2A β€” To Install Jenkins

Connect to your console, and enter these commands to Install Jenkins

sudo apt-get update

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

sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins

Once Jenkins is installed, you will need to go to your AWS EC2 Security Group and open Inbound Port 8080, since Jenkins works on Port 8080.

Now, grab your Public IP Address

<EC2 Public IP Address:8080>
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Unlock Jenkins using an administrative password and install the required plugins.

Jenkins will now get installed and install all the libraries.

Jenkins Getting Started Screen

2B β€” Install Docker

sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER
sudo chmod 777 /var/run/docker.sock 
sudo docker ps

After the docker installation, we create a sonarqube container (Remember added 9000 port in the security group)

docker run -d --name sonar -p 9000:9000 sonarqube:lts-community

2C β€” Install Trivy

sudo apt-get install wget apt-transport-https gnupg lsb-release

wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null

echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list

sudo apt-get update

sudo apt-get install trivy -y

Next, we will login to Jenkins and start to configure our Pipeline in Jenkins

Step 3 β€” Install Plugins like JDK, Sonarqube Scanner, Maven, OWASP Dependency Check, Docker

3A β€” Install Plugin

Goto Manage Jenkins β†’Plugins β†’ Available Plugins β†’

Install below plugins

1 β†’ Eclipse Temurin Installer (Install without restart)

2 β†’ SonarQube Scanner (Install without restart)

3B β€” Configure Java and Maven in Global Tool Configuration

Goto Manage Jenkins β†’ Tools β†’ Install JDK and Maven3 β†’ Click on Apply and Save

3C β€” Create a Job

Label it as Real-World CI-CD, click on Pipeline and Ok.

Step 4β€” Install OWASP Dependency Check Plugins

GotoDashboard β†’ Manage Jenkins β†’ Plugins β†’ OWASP Dependency-Check. Click on it and install without restart.

First, we configured Plugin and next we have to configure Tool

Goto Dashboard β†’ Manage Jenkins β†’ Tools β†’

Click on apply and Save here.

Step 5β€” Configure Sonar Server in Manage Jenkins

Grab the Public IP Address of your EC2 Instance, Sonarqube works on Port 9000 , sp <Public IP>:9000. Goto your Sonarqube Server. Click on Administration β†’ Security β†’ Users β†’ Click on Tokens and Update Token β†’ Give it a name β†’ and click on Generate Token

Click on Update Token

Copy this Token

Goto Dashboard β†’ Manage Jenkins β†’ Credentials β†’ Add Secret Text. It should look like this

Now, goto Dashboard β†’ Manage Jenkins β†’ Configure System

Click on Apply and Save

Configure System option is used in Jenkins to configure different server

Global Tool Configuration is used to configure different tools that we install using Plugins

We will install sonar-scanner in tools.

Lets goto our Pipeline and add Sonar-qube Stage in our Pipeline Script

pipeline {
    agent any

    tools {
        jdk 'jdk17'
        maven 'maven3'
    }

    environment {
        SCANNER_HOME=tool 'sonar-scanner'
    }

    stages {
        stage('Git Checkout') {
            steps {
                git branch: 'main', url: 'https://github.com/SushantOps/Python-Webapp.git'
            }
        }

        stage("OWASP Dependency"){
            steps{
                dependencyCheck additionalArguments: '--scan ./ ', odcInstallation: 'DC'
                dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
            }
        }

        stage("Trivy FS "){
            steps{
                sh "trivy fs ."
            }
        } 

        stage(" Sonarqube Analysis "){
            steps{
                 withSonarQubeEnv('sonar') {
                    sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Python-webapp \
                    -Dsonar.projectKey=Python-webapp '''

                 }
            }
        } 
    }
}

Click on Build now, you will see the stage view like this

To see the report, you can goto Sonarqube Server and goto Projects.

You can see the report has been generated and the status shows as passed. You can see that there are 346 lines. To see detailed report, you can go to issues.

Step 6β€” we have to install make package

sudo apt install make

# to check version install or not
make -v

Step 7β€” Docker Image Build and Push

We need to install Docker tool in our system, Goto Dashboard β†’ Manage Plugins β†’ Available plugins β†’ Search for Docker and install these plugins

  • Docker

  • Docker Commons

  • Docker Pipeline

  • Docker API

  • docker-build-step

and click on install without restart

Now, goto Dashboard β†’ Manage Jenkins β†’ Tools β†’

Add DockerHub Username and Password under Global Credentials

When all stages in docker is succesfully created then you will see the result .you log in to Dockerhub, you will see a new image is created

Step 8β€” Deploy image using Docker

Add this stage to your pipeline syntax

 stage("Deploy to container"){
            steps{
                script{
                    withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') {
                    sh "docker run -d -p 5000:5000 sushantkapare1717/python-webapp:latest "
                   }
                }
            }
        }

You will see the Stage View like this,

(Add port 5000 th Security Group)

The final script looks like this,

pipeline {
    agent any

    tools {
        jdk 'jdk17'
    }

    environment {
        SCANNER_HOME=tool 'sonar-scanner'
    }

    stages {
        stage('Git Checkout') {
            steps {
                git branch: 'main', url: 'https://github.com/SushantOps/Python-Webapp.git'
            }
        }

        stage("OWASP Dependency"){
            steps{
                dependencyCheck additionalArguments: '--scan ./ ', odcInstallation: 'DC'
                dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
            }
        }

        stage("Trivy FS "){
            steps{
                sh "trivy fs ."
            }
        } 

        stage(" Sonarqube Analysis "){
            steps{
                 withSonarQubeEnv('sonar') {
                    sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Python-webapp \
                    -Dsonar.projectKey=Python-webapp '''

                 }
            }
        } 

        stage("Docker Build and Tag  "){
            steps{
                script{
                    withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') {
                    sh "make image"
                   }
                }
            }
        } 

         stage("Trivy Image Scan "){
            steps{
                sh "trivy image sushantkapare1717/python-webapp "
            }
        } 
        stage("Docker Push"){
            steps{
                script{
                    withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') {
                    sh "make push"
                   }
                }
            }
        } 

        stage("Deploy to container"){
            steps{
                script{
                    withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') {
                    sh "docker run -d -p 5000:5000 sushantkapare1717/python-webapp:latest "
                   }
                }
            }
        } 
    }
}

And you can access your application on Port 5000. This is a Real World Application that has all Functional Tabs.

Step 9 β€” Access the Real World Application

Step 10 β€” Terminate the AWS EC2 Instance

Lastly, do not forget to terminate the AWS EC2 Instance.

If this post was helpful, please do follow and click the clap πŸ‘ button below to show your support.

Follow me on Linked-in and Github for further updates- https://www.linkedin.com/in/sushantkapare/

https://github.com/SushantOps

Did you find this article valuable?

Support Vishal Kushwah Blog by becoming a sponsor. Any amount is appreciated!

Β