DEV Community

Surender Gupta
Surender Gupta

Posted on

Building a Scalable PetClinic Web Application: A Full-Stack Approach

Vagrant for Local Setup

Initialize Vagrant with Ubuntu

vagrant init ubuntu/jammy64
Enter fullscreen mode Exit fullscreen mode

Update Vagrantfile

Uncomment the private and public network settings and set the private network as per your choice.

Start Vagrant

vagrant up
Enter fullscreen mode Exit fullscreen mode

(If prompted, choose network bridge 1.) Wait for it to be successfully up and running.

SSH into Vagrant Machine

vagrant ssh
Enter fullscreen mode Exit fullscreen mode

Set Hostname

sudo hostname jenkins-apache
exec bash
Enter fullscreen mode Exit fullscreen mode

Install Jenkins

Create a jenkins.sh file and add the following commands:

sudo vi jenkins.sh
Enter fullscreen mode Exit fullscreen mode

Add the following content:

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 update
sudo apt install openjdk-17-jdk -y
sudo apt install openjdk-17-jre -y
sudo apt-get install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
Enter fullscreen mode Exit fullscreen mode

Make Jenkins Script Executable & Run

sudo chmod +x jenkins.sh
./jenkins.sh
Enter fullscreen mode Exit fullscreen mode

Retrieve Jenkins Admin Password

ADMIN_PASSWORD=$(sudo cat /var/lib/jenkins/secrets/initialAdminPassword)
echo "Your Jenkins initial admin password is $ADMIN_PASSWORD"
Enter fullscreen mode Exit fullscreen mode

Find IP Address

hostname -I
Enter fullscreen mode Exit fullscreen mode

Example output:

10.0.2.15 192.168.56.56 192.168.1.108 fd00::e3:d8ff:feeb:2b51
Enter fullscreen mode Exit fullscreen mode

Access Jenkins at http://192.168.56.56:8080.

Install Docker

sudo apt-get install docker.io -y
sudo usermod -aG docker $USER
sudo chmod 666 /var/run/docker.sock
sudo docker ps
Enter fullscreen mode Exit fullscreen mode

Install SonarQube on Docker

docker run -d --name sonar -p 9000:9000 sonarqube:lts-community
Enter fullscreen mode Exit fullscreen mode

If unable to access:

sudo ufw enable
sudo ufw allow 9000/tcp
Enter fullscreen mode Exit fullscreen mode

Install Trivy

Create a trivy.sh file and add:

sudo vi trivy.sh
Enter fullscreen mode Exit fullscreen mode

Add the following content:

sudo apt-get install wget apt-transport-https gnupg lsb-release -y
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo deb 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
TRIVY_VERSION=$(trivy version)
echo $TRIVY_VERSION
Enter fullscreen mode Exit fullscreen mode

Run:

sudo chmod +x trivy.sh
./trivy.sh
Enter fullscreen mode Exit fullscreen mode

Configure Jenkins Plugins

Install the following plugins:

  • Eclipse Temurin Installer
  • SonarQube Scanner
  • Maven
  • OWASP Dependency Check
  • Docker Plugins

Configure Java and Maven

Go to Manage JenkinsTools → Install JDK and Maven3 → Apply and Save.

Create a Jenkins Pipeline Job

Create a new job, label it as PetClinic, select Pipeline, and add the following script:

pipeline {
    agent any
    tools{
        jdk 'jdk17'
        maven 'maven3'
    }
    stages{
        stage("Git Checkout"){
            steps{
                git branch: 'local', url: 'https://github.com/surendergupta/petclinic.git'
            }
        }
        stage("Compile"){
            steps{
                sh "mvn clean compile"
            }
        }
        stage("Test Cases"){
            steps{
                sh "mvn test"
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Configure SonarQube in Jenkins

  • Go to Manage JenkinsSystem → SonarQube installations → Add SonarQube
  • Name: sonar-server
  • Server URL: http://<IP_ADDRESS_SONAR_SERVER>:9000
  • Authentication Token: sonar-token

Add SonarQube Scanner under Manage JenkinsTools.

Add SonarQube Analysis Stage to Pipeline

stage("Sonarqube Analysis") {
    steps{
        withSonarQubeEnv('sonar-server') {
            sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Petclinic \
            -Dsonar.java.binaries=. \
            -Dsonar.projectKey=Petclinic '''
        }
    }
}
stage("Sonarqube Quality Gate") {
    steps {
        waitForQualityGate abortPipeline: false, credentialsId: 'sonar-token'
    }
}
Enter fullscreen mode Exit fullscreen mode

Configure OWASP Dependency Check

  • Go to Manage JenkinsPlugins → Install OWASP Dependency-Check.
  • Configure under Manage JenkinsTools → Add Dependency-Check.

Add OWASP Dependency Check Stage to Pipeline

stage('OWASP FS SCAN') {
    steps {
        dependencyCheck additionalArguments: '--scan ./ --enableExperimental --format XML', odcInstallation: 'DP-Check'
        dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
    }
}
Enter fullscreen mode Exit fullscreen mode

Build and Install Stage

stage("Build"){
    steps{
        sh "mvn clean install"
    }
}
Enter fullscreen mode Exit fullscreen mode

Configure Email Notifications

Install the Email Extended Notification plugin and configure:

  • SMTP Server: smtp.gmail.com
  • Port: 465
  • Use SSL: Checked
  • Username: <your-email>
  • Password: <app-password>

Trivy File System Scan

stage("Trivy File System Scan"){
    steps{
        sh "trivy fs --format table -o trivy-fs-report.html ."
    }
}
Enter fullscreen mode Exit fullscreen mode

Email Trivy Report

post {
    always {
        emailext attachLog: true,
        subject: "'${currentBuild.result}'",
        body: "Please find the attached Trivy FS Scan Report",
        recipientProviders: [[$class: 'DevelopersRecipientProvider']],
        attachmentsPattern: "trivy-fs-report.html"
    }
}
Enter fullscreen mode Exit fullscreen mode

This completes the setup for a fully integrated PetClinic web application pipeline.

Top comments (0)

👋 Kindness is contagious

If you found this article helpful, please give a ❤️ or share a friendly comment!

Got it