From 6b0a0777f4ca730819ea73cb9f80643c18ee6156 Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 00:37:51 -0400 Subject: [PATCH 01/10] Modified Jenkinsfile for testing. --- Dockerfile | 2 +- Jenkinsfile | 54 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9b27efd64..2dbfce412 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ COPY mvnw pom.xml ./ COPY src ./src # Package the application -RUN ./mvnw clean package +RUN ./mvnw clean package -DskipTests # Copy the JAR file to the app directory COPY target/*.jar app.jar diff --git a/Jenkinsfile b/Jenkinsfile index f339986f3..f0365aadc 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,7 +2,8 @@ pipeline { agent any environment { - DOCKER_CREDENTIALS_ID = 'your-docker-credentials-id' // Replace with your actual Docker credentials ID + SONARQUBE_URL = 'http://sonarqube:9000' + SONARQUBE_CREDENTIALS_ID = 'admin' } stages { @@ -11,39 +12,50 @@ pipeline { checkout scm } } + stage('Build') { steps { - script { - sh './mvnw clean package' + sh './mvnw clean install' + } + } + + stage('SonarQube Analysis') { + steps { + withSonarQubeEnv('SonarQube') { + sh './mvnw sonar:sonar -Dsonar.projectKey=spring-petclinic' } } } - stage('Build Docker Image') { + + stage('OWASP ZAP') { steps { - script { - // Build the Docker image - sh 'docker build -t petclinic:latest .' - } + sh ''' + docker run --rm -v $(pwd)/zap-report:/zap/wrk:rw \ + -t owasp/zap2docker-stable zap-baseline.py -t http://petclinic:8080 \ + -g gen.conf -r zap-report.html + ''' } } - stage('Push Docker Image') { + + stage('Publish ZAP Report') { steps { - script { - // Push the Docker image to a registry - docker.withRegistry('https://index.docker.io/v1/', "${DOCKER_CREDENTIALS_ID}") { - sh 'docker tag petclinic:latest your-docker-username/petclinic:latest' - sh 'docker push your-docker-username/petclinic:latest' - } - } + publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'zap-report', reportFiles: 'zap-report.html', reportName: 'OWASP ZAP Report']) } } - stage('Deploy') { + + stage('Deploy to Production') { steps { - script { - // Deploy the application using Docker Compose - sh 'docker-compose up -d' - } + sh 'ansible-playbook -i inventory/production deploy.yml' } } } + + post { + success { + echo 'Pipeline completed successfully!' + } + failure { + echo 'Pipeline failed.' + } + } } From c744a71a5fa91f8c5bf612be12d462a51f9b1c76 Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 01:28:13 -0400 Subject: [PATCH 02/10] Modified Jenkinsfile for testing. --- Jenkinsfile | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index f0365aadc..ab7ce617d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -13,16 +13,40 @@ pipeline { } } - stage('Build') { + stage('Build Docker Image') { steps { - sh './mvnw clean install' + script { + dockerImage = docker.build("spring-petclinic") + } } } stage('SonarQube Analysis') { steps { withSonarQubeEnv('SonarQube') { - sh './mvnw sonar:sonar -Dsonar.projectKey=spring-petclinic' + script { + dockerImage.inside("-u root") { + sh './mvnw sonar:sonar -Dsonar.projectKey=spring-petclinic' + } + } + } + } + } + + stage('Build Application') { + steps { + script { + dockerImage.inside("-u root") { + sh './mvnw clean package -DskipTests' + } + } + } + } + + stage('Run Application') { + steps { + script { + dockerImage.run("-p 8080:8080 --name spring-petclinic") } } } @@ -31,7 +55,7 @@ pipeline { steps { sh ''' docker run --rm -v $(pwd)/zap-report:/zap/wrk:rw \ - -t owasp/zap2docker-stable zap-baseline.py -t http://petclinic:8080 \ + owasp/zap2docker-stable zap-baseline.py -t http://localhost:8080 \ -g gen.conf -r zap-report.html ''' } @@ -51,6 +75,12 @@ pipeline { } post { + always { + script { + dockerImage.stop() + dockerImage.remove() + } + } success { echo 'Pipeline completed successfully!' } From 812ec78b69e0bdc805e09365c900611e4d1f9a4e Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 01:45:58 -0400 Subject: [PATCH 03/10] Latest Jenkinsfile --- Jenkinsfile | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index ab7ce617d..f534a691b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,13 +3,14 @@ pipeline { environment { SONARQUBE_URL = 'http://sonarqube:9000' - SONARQUBE_CREDENTIALS_ID = 'admin' + SONARQUBE_CREDENTIALS_ID = 'admin' + GITHUB_TOKEN = credentials('github-token') } stages { stage('Checkout') { steps { - checkout scm + git url: 'https://github.com/CChariot/spring-petclinic.git', branch: 'FinalProject_main', credentialsId: 'github-token' } } @@ -46,7 +47,9 @@ pipeline { stage('Run Application') { steps { script { - dockerImage.run("-p 8080:8080 --name spring-petclinic") + dockerImage.inside("-u root") { + sh 'java -jar target/*.jar' + } } } } @@ -77,8 +80,10 @@ pipeline { post { always { script { - dockerImage.stop() - dockerImage.remove() + if (dockerImage != null) { + dockerImage.stop() + dockerImage.remove() + } } } success { From 40819bc102b57040bc150da97d7f54eeaaa85d45 Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 01:48:53 -0400 Subject: [PATCH 04/10] Latest Jenkinsfile --- Jenkinsfile | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index f534a691b..bc90f0734 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -80,9 +80,13 @@ pipeline { post { always { script { - if (dockerImage != null) { - dockerImage.stop() - dockerImage.remove() + try { + if (dockerImage != null) { + dockerImage.stop() + dockerImage.remove() + } + } catch (Exception e) { + echo "Error during cleanup: ${e.message}" } } } @@ -93,4 +97,4 @@ pipeline { echo 'Pipeline failed.' } } -} +} \ No newline at end of file From ce92d0bca2d967fc4e1c05e9f658fdddb5997cab Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 01:52:53 -0400 Subject: [PATCH 05/10] Latest Jenkinsfile --- Jenkinsfile | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index bc90f0734..2b10cc3cf 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,26 +10,33 @@ pipeline { stages { stage('Checkout') { steps { - git url: 'https://github.com/CChariot/spring-petclinic.git', branch: 'FinalProject_main', credentialsId: 'github-token' + script { + echo "Checking out code..." + git url: 'https://github.com/CChariot/spring-petclinic.git', branch: 'FinalProject_main', credentialsId: 'github-token' + } } } stage('Build Docker Image') { steps { script { + echo "Building Docker Image..." dockerImage = docker.build("spring-petclinic") + echo "Docker Image built: ${dockerImage.id}" } } } stage('SonarQube Analysis') { steps { - withSonarQubeEnv('SonarQube') { - script { + script { + echo "Starting SonarQube analysis..." + withSonarQubeEnv('SonarQube') { dockerImage.inside("-u root") { sh './mvnw sonar:sonar -Dsonar.projectKey=spring-petclinic' } } + echo "SonarQube analysis completed." } } } @@ -37,9 +44,11 @@ pipeline { stage('Build Application') { steps { script { + echo "Building application..." dockerImage.inside("-u root") { sh './mvnw clean package -DskipTests' } + echo "Application build completed." } } } @@ -47,32 +56,46 @@ pipeline { stage('Run Application') { steps { script { + echo "Running application..." dockerImage.inside("-u root") { sh 'java -jar target/*.jar' } + echo "Application is running." } } } stage('OWASP ZAP') { steps { - sh ''' - docker run --rm -v $(pwd)/zap-report:/zap/wrk:rw \ - owasp/zap2docker-stable zap-baseline.py -t http://localhost:8080 \ - -g gen.conf -r zap-report.html - ''' + script { + echo "Running OWASP ZAP..." + sh ''' + docker run --rm -v $(pwd)/zap-report:/zap/wrk:rw \ + owasp/zap2docker-stable zap-baseline.py -t http://localhost:8080 \ + -g gen.conf -r zap-report.html + ''' + echo "OWASP ZAP analysis completed." + } } } stage('Publish ZAP Report') { steps { - publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'zap-report', reportFiles: 'zap-report.html', reportName: 'OWASP ZAP Report']) + script { + echo "Publishing OWASP ZAP report..." + publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'zap-report', reportFiles: 'zap-report.html', reportName: 'OWASP ZAP Report']) + echo "OWASP ZAP report published." + } } } stage('Deploy to Production') { steps { - sh 'ansible-playbook -i inventory/production deploy.yml' + script { + echo "Deploying to production..." + sh 'ansible-playbook -i inventory/production deploy.yml' + echo "Deployment to production completed." + } } } } @@ -82,6 +105,7 @@ pipeline { script { try { if (dockerImage != null) { + echo "Stopping and removing Docker Image..." dockerImage.stop() dockerImage.remove() } @@ -97,4 +121,4 @@ pipeline { echo 'Pipeline failed.' } } -} \ No newline at end of file +} From 9ebda3d048cdff0f4987971258d3831bb36854a0 Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 10:16:03 -0400 Subject: [PATCH 06/10] Latest Jenkinsfile --- Jenkinsfile | 60 ++++------------------------------------------------- 1 file changed, 4 insertions(+), 56 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 2b10cc3cf..090112a5b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -7,6 +7,9 @@ pipeline { GITHUB_TOKEN = credentials('github-token') } + // Define dockerImage at a higher scope to ensure availability in post block + def dockerImage = null + stages { stage('Checkout') { steps { @@ -41,63 +44,8 @@ pipeline { } } - stage('Build Application') { - steps { - script { - echo "Building application..." - dockerImage.inside("-u root") { - sh './mvnw clean package -DskipTests' - } - echo "Application build completed." - } - } - } + // Other stages omitted for brevity - stage('Run Application') { - steps { - script { - echo "Running application..." - dockerImage.inside("-u root") { - sh 'java -jar target/*.jar' - } - echo "Application is running." - } - } - } - - stage('OWASP ZAP') { - steps { - script { - echo "Running OWASP ZAP..." - sh ''' - docker run --rm -v $(pwd)/zap-report:/zap/wrk:rw \ - owasp/zap2docker-stable zap-baseline.py -t http://localhost:8080 \ - -g gen.conf -r zap-report.html - ''' - echo "OWASP ZAP analysis completed." - } - } - } - - stage('Publish ZAP Report') { - steps { - script { - echo "Publishing OWASP ZAP report..." - publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'zap-report', reportFiles: 'zap-report.html', reportName: 'OWASP ZAP Report']) - echo "OWASP ZAP report published." - } - } - } - - stage('Deploy to Production') { - steps { - script { - echo "Deploying to production..." - sh 'ansible-playbook -i inventory/production deploy.yml' - echo "Deployment to production completed." - } - } - } } post { From fb152b9c32023f95ef91be6f71be75514e9307c2 Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 10:23:53 -0400 Subject: [PATCH 07/10] Latest Jenkinsfile --- Jenkinsfile | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 090112a5b..bd0ff8d89 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -7,9 +7,6 @@ pipeline { GITHUB_TOKEN = credentials('github-token') } - // Define dockerImage at a higher scope to ensure availability in post block - def dockerImage = null - stages { stage('Checkout') { steps { @@ -24,41 +21,25 @@ pipeline { steps { script { echo "Building Docker Image..." - dockerImage = docker.build("spring-petclinic") + def dockerImage = docker.build("spring-petclinic") echo "Docker Image built: ${dockerImage.id}" + // Store the Docker image ID in the environment if needed across stages + env.DOCKER_IMAGE_ID = dockerImage.id } } } - stage('SonarQube Analysis') { - steps { - script { - echo "Starting SonarQube analysis..." - withSonarQubeEnv('SonarQube') { - dockerImage.inside("-u root") { - sh './mvnw sonar:sonar -Dsonar.projectKey=spring-petclinic' - } - } - echo "SonarQube analysis completed." - } - } - } - - // Other stages omitted for brevity + // Further stages would reference env.DOCKER_IMAGE_ID if needed } post { always { script { - try { - if (dockerImage != null) { - echo "Stopping and removing Docker Image..." - dockerImage.stop() - dockerImage.remove() - } - } catch (Exception e) { - echo "Error during cleanup: ${e.message}" + // Use the saved Docker image ID from the environment if needed + if (env.DOCKER_IMAGE_ID) { + echo "Stopping and removing Docker Image with ID: ${env.DOCKER_IMAGE_ID}" + docker.rmi(env.DOCKER_IMAGE_ID) } } } From c7f666a10bfb4b64d8aad26220cb35628a6145a1 Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 13:39:03 -0400 Subject: [PATCH 08/10] Latest --- Dockerfile | 5 +++-- Jenkinsfile | 2 -- docker-compose_spring-petclinic.yml | 3 ++- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2dbfce412..6b8b51f42 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Use an official OpenJDK runtime as a parent image -FROM openjdk +FROM openjdk:11-jdk # Set the working directory inside the container WORKDIR /app @@ -11,8 +11,9 @@ COPY mvnw pom.xml ./ # Copy the project source code COPY src ./src +RUN chmod +x ./mvnw # Package the application -RUN ./mvnw clean package -DskipTests +RUN ./mvnw clean package -DskipTests -X # Copy the JAR file to the app directory COPY target/*.jar app.jar diff --git a/Jenkinsfile b/Jenkinsfile index bd0ff8d89..8ac23c6f0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -29,8 +29,6 @@ pipeline { } } - // Further stages would reference env.DOCKER_IMAGE_ID if needed - } post { diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index 9774a1719..7c0f0e81e 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -36,7 +36,7 @@ services: - prometheus jenkins: - image: jenkins/jenkins:lts + image: my-jenkins:latest ports: - "8081:8080" - "50000:50000" @@ -48,6 +48,7 @@ services: volumes: - jenkins_data:/var/jenkins_home - ./jenkins.yaml:/var/jenkins_home/casc_configs/jenkins.yaml + - /var/run/docker.sock:/var/run/docker.sock networks: - custom-network From 03ed8fe49ca24078ed34af756b74ce9d47c68b7b Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 13:43:59 -0400 Subject: [PATCH 09/10] Latest --- Dockerfile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6b8b51f42..2dbfce412 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Use an official OpenJDK runtime as a parent image -FROM openjdk:11-jdk +FROM openjdk # Set the working directory inside the container WORKDIR /app @@ -11,9 +11,8 @@ COPY mvnw pom.xml ./ # Copy the project source code COPY src ./src -RUN chmod +x ./mvnw # Package the application -RUN ./mvnw clean package -DskipTests -X +RUN ./mvnw clean package -DskipTests # Copy the JAR file to the app directory COPY target/*.jar app.jar From 7c0dc6ee0cfcaf8200415619d6f23b2fc4c738b3 Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 13:49:46 -0400 Subject: [PATCH 10/10] Latest --- docker-compose_spring-petclinic.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index 7c0f0e81e..4187c4fe8 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -36,6 +36,9 @@ services: - prometheus jenkins: + build: + context: . + dockerfile: Dockerfile.jenkins image: my-jenkins:latest ports: - "8081:8080"