From d79fc9812c24fc5278694c7f8a3e1c8eb02e1e90 Mon Sep 17 00:00:00 2001 From: Nicholas Mucks Date: Sat, 27 Jul 2024 10:35:55 -0700 Subject: [PATCH 01/12] feat: add sonarqube official configs --- .gitignore | 3 +++ docker-compose_spring-petclinic.yml | 29 ++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 59ead86e9..bd63fa7d5 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ _site/ zap-report/* .DS_Store +sonarqube_data +sonarqube_extensions +sonarqube_logs diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index 9774a1719..c9bbffd50 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -62,19 +62,38 @@ services: - petclinic sonarqube: - image: sonarqube:latest + image: sonarqube:community + depends_on: + - db + environment: + SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar + SONAR_JDBC_USERNAME: sonar + SONAR_JDBC_PASSWORD: sonar + volumes: + - sonarqube_data:/opt/sonarqube/data + - sonarqube_extensions:/opt/sonarqube/extensions + - sonarqube_logs:/opt/sonarqube/logs ports: - "9000:9000" - networks: - - custom-network - depends_on: - - petclinic + db: + image: postgres:12 + environment: + POSTGRES_USER: sonar + POSTGRES_PASSWORD: sonar + volumes: + - postgresql:/var/lib/postgresql + - postgresql_data:/var/lib/postgresql/data volumes: prometheus_data: grafana_data: jenkins_data: + sonarqube_data: + sonarqube_extensions: + sonarqube_logs: + postgresql: + postgresql_data: networks: custom-network: From e21bbb4ff161773d90a4ccaab56f08fca56b161f Mon Sep 17 00:00:00 2001 From: Nicholas Mucks Date: Sat, 27 Jul 2024 10:47:50 -0700 Subject: [PATCH 02/12] fix: add ./ to sonarqube --- docker-compose_spring-petclinic.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index c9bbffd50..9b855b942 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -70,9 +70,9 @@ services: SONAR_JDBC_USERNAME: sonar SONAR_JDBC_PASSWORD: sonar volumes: - - sonarqube_data:/opt/sonarqube/data - - sonarqube_extensions:/opt/sonarqube/extensions - - sonarqube_logs:/opt/sonarqube/logs + - ./sonarqube_data:/opt/sonarqube/data + - ./sonarqube_extensions:/opt/sonarqube/extensions + - ./sonarqube_logs:/opt/sonarqube/logs ports: - "9000:9000" db: From d5c6f31b5c149dd5ee264ea77a619ba91622a049 Mon Sep 17 00:00:00 2001 From: tautaus <43488851+tautaus@users.noreply.github.com> Date: Sat, 27 Jul 2024 12:42:24 -0700 Subject: [PATCH 03/12] Add files via upload update for build --- Dockerfile | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2dbfce412..b67210092 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,26 @@ -# Use an official OpenJDK runtime as a parent image -FROM openjdk - -# Set the working directory inside the container -WORKDIR /app - -# Copy the Maven wrapper and the pom.xml file -COPY .mvn/ .mvn -COPY mvnw pom.xml ./ - -# Copy the project source code -COPY src ./src - -# Package the application -RUN ./mvnw clean package -DskipTests - -# Copy the JAR file to the app directory -COPY target/*.jar app.jar - -# Run the jar file -CMD ["java", "-jar", "app.jar"] - -# Expose the port the app runs on -EXPOSE 8080 +# First stage: Build the application using Maven +FROM maven:3.8.1-openjdk-17 AS build +WORKDIR /app + +# Copy the Maven wrapper and the pom.xml file +COPY .mvn/ .mvn +COPY mvnw pom.xml ./ + +# Copy the project source code +COPY src ./src + +# Package the application +RUN ./mvnw package + +# Second stage: Use an official OpenJDK runtime as a parent image +FROM openjdk +WORKDIR /app + +# Copy the JAR file from the build stage +COPY --from=build /app/target/*.jar app.jar + +# Run the jar file +CMD ["java", "-jar", "app.jar"] + +# Expose the port the app runs on +EXPOSE 8080 From 549f0255c4605fc23f3b3921cbfc7ef05d7e9589 Mon Sep 17 00:00:00 2001 From: Nicholas Mucks Date: Sat, 27 Jul 2024 17:32:21 -0700 Subject: [PATCH 04/12] fix: align jenkins_data to other voluments and remove unneeded configs --- docker-compose_spring-petclinic.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index 98c79abe3..b545d6739 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -39,17 +39,15 @@ services: build: context: . dockerfile: Dockerfile.jenkins - image: my-jenkins:latest ports: - "8081:8080" - "50000:50000" - privileged: true - user: root + environment: - JAVA_OPTS=-Djenkins.install.runSetupWizard=false - CASC_JENKINS_CONFIG=/var/jenkins_home/casc_configs/jenkins.yaml volumes: - - jenkins_data:/var/jenkins_home + - ./jenkins_data:/var/jenkins_home - ./jenkins.yaml:/var/jenkins_home/casc_configs/jenkins.yaml - /var/run/docker.sock:/var/run/docker.sock networks: From bee774e2faf6320526994de8f8f0a2a033f5f762 Mon Sep 17 00:00:00 2001 From: Nicholas Mucks Date: Sat, 27 Jul 2024 17:45:43 -0700 Subject: [PATCH 05/12] fix: change the dockerfile back to the one that works --- .gitignore | 1 + Dockerfile | 50 ++++++++++++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index bd63fa7d5..3520b961f 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ zap-report/* sonarqube_data sonarqube_extensions sonarqube_logs +jenkins_data diff --git a/Dockerfile b/Dockerfile index b67210092..2dbfce412 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,24 @@ -# First stage: Build the application using Maven -FROM maven:3.8.1-openjdk-17 AS build -WORKDIR /app - -# Copy the Maven wrapper and the pom.xml file -COPY .mvn/ .mvn -COPY mvnw pom.xml ./ - -# Copy the project source code -COPY src ./src - -# Package the application -RUN ./mvnw package - -# Second stage: Use an official OpenJDK runtime as a parent image -FROM openjdk -WORKDIR /app - -# Copy the JAR file from the build stage -COPY --from=build /app/target/*.jar app.jar - -# Run the jar file -CMD ["java", "-jar", "app.jar"] - -# Expose the port the app runs on -EXPOSE 8080 +# Use an official OpenJDK runtime as a parent image +FROM openjdk + +# Set the working directory inside the container +WORKDIR /app + +# Copy the Maven wrapper and the pom.xml file +COPY .mvn/ .mvn +COPY mvnw pom.xml ./ + +# Copy the project source code +COPY src ./src + +# Package the application +RUN ./mvnw clean package -DskipTests + +# Copy the JAR file to the app directory +COPY target/*.jar app.jar + +# Run the jar file +CMD ["java", "-jar", "app.jar"] + +# Expose the port the app runs on +EXPOSE 8080 From dcfe77041479b335af3263072f241421a40ced62 Mon Sep 17 00:00:00 2001 From: bossardme Date: Sat, 27 Jul 2024 18:27:47 -0700 Subject: [PATCH 06/12] working sonarqube and sonarscanner working sonarqube and sonarscanner --- .gitignore | 4 ++++ docker-compose_spring-petclinic.yml | 28 ++++++++++++++++++++++++---- sonar-project.properties | 5 +++++ 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 sonar-project.properties diff --git a/.gitignore b/.gitignore index 3520b961f..c89efa498 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,8 @@ zap-report/* sonarqube_data sonarqube_extensions sonarqube_logs +sonarqube_bundled-plugins +sonarqube_conf jenkins_data +postgresql +postgresql_data diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index b545d6739..e80dcc1e4 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -72,28 +72,48 @@ services: SONAR_JDBC_USERNAME: sonar SONAR_JDBC_PASSWORD: sonar volumes: + - ./sonarqube_conf:/opt/sonarqube/conf - ./sonarqube_data:/opt/sonarqube/data - - ./sonarqube_extensions:/opt/sonarqube/extensions - ./sonarqube_logs:/opt/sonarqube/logs + - ./sonarqube_extensions:/opt/sonarqube/extensions + - ./sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins ports: - "9000:9000" + - "9092:9092" + networks: + - custom-network + db: image: postgres:12 environment: POSTGRES_USER: sonar POSTGRES_PASSWORD: sonar volumes: - - postgresql:/var/lib/postgresql - - postgresql_data:/var/lib/postgresql/data + - ./postgresql:/var/lib/postgresql + - ./postgresql_data:/var/lib/postgresql/data + networks: + - custom-network + sonarscanner: + image: sonarsource/sonar-scanner-cli + networks: + - custom-network + volumes: + - ./:/usr/src + environment: + - SONAR_HOST_URL=http://sonarqube:9000/ + depends_on: + - sonarqube volumes: prometheus_data: grafana_data: jenkins_data: + sonarqube_conf: sonarqube_data: - sonarqube_extensions: sonarqube_logs: + sonarqube_extensions: + sonarqube_bundled-plugins: postgresql: postgresql_data: diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 000000000..57bd2f920 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,5 @@ +sonar.projectKey=petclinic +sonar.projectName=petclinic +sonar.projectVersion=1.0 +sonar.sources=. +sonar.exclusions=**/*.java \ No newline at end of file From 033ecf1a03a4090f67c6f5b0c848a10c066d14f8 Mon Sep 17 00:00:00 2001 From: Nicholas Mucks Date: Sat, 27 Jul 2024 18:48:45 -0700 Subject: [PATCH 07/12] fix: add support for apple m1 chips and move db before sonarqube --- docker-compose_spring-petclinic.yml | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index e80dcc1e4..32f18431e 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -63,6 +63,17 @@ services: depends_on: - petclinic + db: + image: postgres:12 + environment: + POSTGRES_USER: sonar + POSTGRES_PASSWORD: sonar + volumes: + - ./postgresql:/var/lib/postgresql + - ./postgresql_data:/var/lib/postgresql/data + networks: + - custom-network + sonarqube: image: sonarqube:community depends_on: @@ -83,19 +94,13 @@ services: networks: - custom-network - db: - image: postgres:12 - environment: - POSTGRES_USER: sonar - POSTGRES_PASSWORD: sonar - volumes: - - ./postgresql:/var/lib/postgresql - - ./postgresql_data:/var/lib/postgresql/data - networks: - - custom-network + sonarscanner: image: sonarsource/sonar-scanner-cli + + # need this for macOS Apple M1 +# platform: linux/amd64 networks: - custom-network volumes: From 2d5bd081abfd67f2594da115a2da420b98d820d4 Mon Sep 17 00:00:00 2001 From: bossardme Date: Sat, 27 Jul 2024 19:12:56 -0700 Subject: [PATCH 08/12] updated readme for sonarqube --- docker-compose_spring-petclinic.yml | 1 + readme.md | 36 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index 32f18431e..7771d2831 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -107,6 +107,7 @@ services: - ./:/usr/src environment: - SONAR_HOST_URL=http://sonarqube:9000/ + - SONAR_TOKEN=$MY_SONAR_TOKEN depends_on: - sonarqube diff --git a/readme.md b/readme.md index 7b03f1f8a..f08a24cab 100644 --- a/readme.md +++ b/readme.md @@ -92,3 +92,39 @@ docker exec -it spring-petclinic-petclinic-1 bash - Go back to the Jenkins dashboard. - Select your pipeline job. - Click on **Build Now** to run the pipeline. + +## Steps 3: Running Static Analysis with Sonarqube +1. Access Sonarqube: Open [http://localhost:9000](http://localhost:9000) and set up Sonarqube. Install the suggested plugins. + +2. Login to Sonarqube with the following user and password: + - Username: admin + - Password: admin (update password when prompted after login) + +3. Create project on Sonarqube + - Navigate over to create project + - set the following variables: + Project display name = petclinic + Project key = petclinic + Main branch name = main + - Choose the following option: global branch setting + - Choose the following Analysis Method: Locally + - Generate a project token + +3. Set sonar token + - Copy the generated project token to clipboard + - In your workspace, set the token environment variable (MY_SONAR_TOKEN) + + ```bash + export MY_SONAR_TOKEN= + ``` + +4. Run static analysis + + ```bash + docker run \ + --rm \ + -e SONAR_HOST_URL=http://sonarqube:9000/ \ + -e SONAR_TOKEN=$MY_SONAR_TOKEN \ + -v "./:/usr/src" --network=spring-petclinic_custom-network\ + sonarsource/sonar-scanner-cli + ``` \ No newline at end of file From eb217f481550983b443d760e3c92a69fb09a6257 Mon Sep 17 00:00:00 2001 From: Nicholas Mucks Date: Sat, 27 Jul 2024 19:20:56 -0700 Subject: [PATCH 09/12] fix: use RUN cp for target folder instead --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2dbfce412..0e394b307 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,8 +14,8 @@ COPY src ./src # Package the application RUN ./mvnw clean package -DskipTests -# Copy the JAR file to the app directory -COPY target/*.jar app.jar +# Copy the JAR file to the app directory. This is created in the build process +RUN cp /app/target/*.jar app.jar # Run the jar file CMD ["java", "-jar", "app.jar"] From 256c0bc648d8b8ee3e507e806ff872d283c9d00c Mon Sep 17 00:00:00 2001 From: Nicholas Mucks Date: Sun, 28 Jul 2024 09:55:19 -0700 Subject: [PATCH 10/12] test --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index f08a24cab..d23920b31 100644 --- a/readme.md +++ b/readme.md @@ -127,4 +127,4 @@ docker exec -it spring-petclinic-petclinic-1 bash -e SONAR_TOKEN=$MY_SONAR_TOKEN \ -v "./:/usr/src" --network=spring-petclinic_custom-network\ sonarsource/sonar-scanner-cli - ``` \ No newline at end of file + ``` From d40ba0ccb9108f0997e32d1c82bdea662a7ae077 Mon Sep 17 00:00:00 2001 From: Alan Kim Date: Sun, 28 Jul 2024 13:03:12 -0400 Subject: [PATCH 11/12] Test push --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index d23920b31..4c0a808e9 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# Getting started +# Getting started - Final Project ## Running a build pipeline From eb1f493e71b100d13f75d0ec7ed9269a2c263c1a Mon Sep 17 00:00:00 2001 From: Lihan Date: Sun, 28 Jul 2024 14:34:20 -0400 Subject: [PATCH 12/12] Latest Jenkins config --- Dockerfile | 16 +++------------- Jenkinsfile | 14 ++++++++++++-- docker-compose_spring-petclinic.yml | 6 ++++-- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0e394b307..ce19ac4ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,11 @@ # Use an official OpenJDK runtime as a parent image -FROM openjdk +FROM openjdk:11-jdk-slim # Set the working directory inside the container WORKDIR /app -# Copy the Maven wrapper and the pom.xml file -COPY .mvn/ .mvn -COPY mvnw pom.xml ./ - -# Copy the project source code -COPY src ./src - -# Package the application -RUN ./mvnw clean package -DskipTests - -# Copy the JAR file to the app directory. This is created in the build process -RUN cp /app/target/*.jar app.jar +# Copy the JAR file to the app directory (adjust this path according to your actual JAR file name in the target directory) +COPY target/*.jar app.jar # Run the jar file CMD ["java", "-jar", "app.jar"] diff --git a/Jenkinsfile b/Jenkinsfile index 8ac23c6f0..a0428726d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -17,11 +17,20 @@ pipeline { } } + stage('Build JAR') { + steps { + script { + echo "Building JAR..." + sh './mvnw clean package -Dmaven.test.skip=true' + } + } + } + stage('Build Docker Image') { steps { script { echo "Building Docker Image..." - def dockerImage = docker.build("spring-petclinic") + def dockerImage = docker.build("spring-petclinic", "--no-cache .") echo "Docker Image built: ${dockerImage.id}" // Store the Docker image ID in the environment if needed across stages env.DOCKER_IMAGE_ID = dockerImage.id @@ -29,6 +38,7 @@ pipeline { } } + // Further stages would reference env.DOCKER_IMAGE_ID if needed } post { @@ -37,7 +47,7 @@ pipeline { // 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) + sh "docker rmi -f ${env.DOCKER_IMAGE_ID}" } } } diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index 7771d2831..cbad04772 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -39,15 +39,17 @@ services: build: context: . dockerfile: Dockerfile.jenkins + image: my-jenkins:latest ports: - "8081:8080" - "50000:50000" - + privileged: true + user: root environment: - JAVA_OPTS=-Djenkins.install.runSetupWizard=false - CASC_JENKINS_CONFIG=/var/jenkins_home/casc_configs/jenkins.yaml volumes: - - ./jenkins_data:/var/jenkins_home + - jenkins_data:/var/jenkins_home - ./jenkins.yaml:/var/jenkins_home/casc_configs/jenkins.yaml - /var/run/docker.sock:/var/run/docker.sock networks: