From 4de95f869b3925af2ee72d18f7372780b0b9c0d6 Mon Sep 17 00:00:00 2001
From: Daya Adianto <dayaadianto@cs.ui.ac.id>
Date: Wed, 25 Oct 2023 10:56:27 +0700
Subject: [PATCH 1/6] Update GitHub Workflow for building the project using
 Maven

---
 .github/workflows/maven-build.yml | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/maven-build.yml b/.github/workflows/maven-build.yml
index 6c38e842..ca00582a 100644
--- a/.github/workflows/maven-build.yml
+++ b/.github/workflows/maven-build.yml
@@ -1,3 +1,4 @@
+---
 name: Java CI with Maven
 
 on:
@@ -17,7 +18,7 @@ jobs:
         uses: actions/setup-java@v2
         with:
           java-version: '17'
-          distribution: 'adopt'
+          distribution: 'temurin'
           cache: maven
       - name: Build with Maven
         run: mvn -B install --file pom.xml -Djacoco.skip=true -DdisableXmlReport=true
-- 
GitLab


From 392034e0f4935637551ff9021c64102b51f04464 Mon Sep 17 00:00:00 2001
From: Daya Adianto <dayaadianto@cs.ui.ac.id>
Date: Wed, 25 Oct 2023 10:56:59 +0700
Subject: [PATCH 2/6] Upgrade jib-maven-plugin version to 3.4.0

---
 pom.xml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index c95b91e8..bd7d25ee 100644
--- a/pom.xml
+++ b/pom.xml
@@ -18,7 +18,7 @@
     </parent>
 
     <properties>
-        <!-- Third librairies -->
+        <!-- Third-party libraries -->
         <spring-data-jdbc.version>1.2.1.RELEASE</spring-data-jdbc.version>
         <springdoc-openapi-ui.version>2.0.2</springdoc-openapi-ui.version>
         <jackson-databind-nullable.version>0.2.1</jackson-databind-nullable.version>
@@ -31,7 +31,7 @@
         <build-helper-maven-plugin.version>3.2.0</build-helper-maven-plugin.version>
 
         <!-- Docker -->
-        <docker.jib-maven-plugin.version>1.3.0</docker.jib-maven-plugin.version>
+        <docker.jib-maven-plugin.version>3.4.0</docker.jib-maven-plugin.version>
         <docker.image.prefix>springcommunity</docker.image.prefix>
         <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
     </properties>
-- 
GitLab


From 8c3f79f20e8ee71b0cc341f431190fb9827ba314 Mon Sep 17 00:00:00 2001
From: Daya Adianto <dayaadianto@cs.ui.ac.id>
Date: Wed, 25 Oct 2023 11:02:43 +0700
Subject: [PATCH 3/6] Add Dockerfile

---
 Dockerfile | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 Dockerfile

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 00000000..fbfaa878
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,33 @@
+# Use JDK & Maven image to build the application
+FROM docker.io/library/maven:3.9.4-eclipse-temurin-17-alpine AS builder
+
+# Set the working directory inside the container
+WORKDIR /src
+
+# Copy the source code into the container
+COPY . .
+
+# Build the application JAR file
+RUN mvn -DskipTests package
+
+# Use JRE image for running the application
+FROM docker.io/library/eclipse-temurin:17.0.8.1_1-jre-alpine
+
+# Create a non-root user named "app" to own and run the application
+RUN addgroup app \
+    && adduser -s /bin/false -G app -D -H app
+
+# Switch to the "app" user, so the application does not run as root
+USER app
+
+# Set the working directory inside the container to /opt/app
+WORKDIR /opt/app
+
+# Copy the app into the container
+COPY --chown=app:app --from=builder /src/target/sitodo-*.jar .
+
+# Expose port 9966
+EXPOSE 9966
+
+# Run the application JAR file
+CMD ["/bin/sh", "-c", "java -jar spring-petclinic-rest-*.jar"]
-- 
GitLab


From c0858c9238f2a118d4eb77baa36e01a8e1fd1850 Mon Sep 17 00:00:00 2001
From: Daya Adianto <dayaadianto@cs.ui.ac.id>
Date: Wed, 25 Oct 2023 11:19:57 +0700
Subject: [PATCH 4/6] Configure GitLab CI/CD

---
 .gitlab-ci.yml | 128 +++++++++++++++++++++++++++++++++++++++++++++++++
 pom.xml        |  14 ++++++
 2 files changed, 142 insertions(+)
 create mode 100644 .gitlab-ci.yml

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 00000000..3931f2e7
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,128 @@
+---
+# Based on the Maven CI/CD template from GitLab: https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Maven.gitlab-ci.yml
+variables:
+  # This will suppress any download for dependencies and plugins or upload messages which would clutter the console log.
+  # `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
+  MAVEN_OPTS: >
+    -Dhttps.protocols=TLSv1.2
+    -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
+    -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN
+    -Dorg.slf4j.simpleLogger.showDateTime=true
+    -Djava.awt.headless=true
+  # As of Maven 3.3.0 instead of this, you may define these options in `.mvn/maven.config` so the same config is used
+  # when running from the command line.
+  # `installAtEnd` and `deployAtEnd` are only effective with the recent version of the corresponding plugins.
+  MAVEN_CLI_OPTS: >
+    --batch-mode
+    --errors
+    --fail-at-end
+    --show-version
+    -DinstallAtEnd=true
+    -DdeployAtEnd=true
+
+# Check the list of available templates at https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates
+include:
+  - template: Jobs/Secret-Detection.gitlab-ci.yml
+  - template: Workflows/MergeRequest-Pipelines.gitlab-ci.yml
+
+stages:
+  - build
+  - test
+  - deploy
+  - report
+
+.upstream-deploy-production-rules:
+  rules:
+    - if: $CI_PROJECT_NAMESPACE != "pmpl/examples"
+      when: never
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      when: always
+      allow_failure: true
+
+build:
+  stage: build
+  image: docker.io/library/maven:3.8.6-eclipse-temurin-17-focal
+  before_script:
+    - java -version && javac --version && mvn --version
+    - pwd
+  script:
+    - mvn $MAVEN_CLI_OPTS -DskipTests
+      -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository package
+  cache:
+    key:
+      files:
+        - pom.xml
+    paths:
+      - .m2/repository
+  artifacts:
+    paths:
+      - target/
+
+test:
+  stage: test
+  image: docker.io/library/maven:3.8.6-eclipse-temurin-17-focal
+  needs:
+    - build
+  before_script:
+    - java -version && javac --version && mvn --version
+    - pwd
+  script:
+    # Run test suites and generate test reports
+    - mvn clean verify
+    # Get line coverage
+    - grep -o "Total[^%]*%" target/site/jacoco/index.html
+  coverage: '/Total.*?(\d{1,3})%/'
+  cache:
+    key:
+      files:
+        - pom.xml
+    paths:
+      - .m2/repository
+  artifacts:
+    paths:
+      - target/*.exec
+      - target/site/jacoco/
+    reports:
+      junit:
+        - target/surefire-reports/TEST-*.xml
+
+deploy:
+  stage: deploy
+  image: docker.io/bitnami/git:2.42.0
+  rules: !reference [.upstream-deploy-production-rules, rules]
+  script:
+    - echo "TODO"
+  environment:
+    name: production
+    url: https://spring-petclinic-rest.dokku-ppl.cs.ui.ac.id
+  dependencies: []
+
+visualize-coverage:
+  stage: report
+  image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.9
+  before_script: []
+  script:
+    - python /opt/cover2cover.py target/site/jacoco/jacoco.xml $CI_PROJECT_DIR/src/main/java > target/cobertura.xml
+  needs:
+    - test
+  dependencies:
+    - test
+  artifacts:
+    reports:
+      coverage_report:
+        coverage_format: cobertura
+        path: target/cobertura.xml
+
+sonarqube-check:
+  stage: report
+  image: docker.io/library/maven:3.8.6-eclipse-temurin-17-focal
+  variables:
+    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
+    GIT_DEPTH: "0"
+  rules: !reference [.upstream-deploy-production-rules, rules]
+  script:
+    - mvn -DskipTests verify sonar:sonar
+  cache:
+    key: "${CI_JOB_NAME}"
+    paths:
+      - .sonar/cache
diff --git a/pom.xml b/pom.xml
index bd7d25ee..ac76dd6e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -29,11 +29,20 @@
         <jacoco.version>0.8.8</jacoco.version>
         <openapi-generator-maven-plugin.version>6.3.0</openapi-generator-maven-plugin.version>
         <build-helper-maven-plugin.version>3.2.0</build-helper-maven-plugin.version>
+        <sonar-maven-plugin.version>3.9.1.2184</sonar-maven-plugin.version>
 
         <!-- Docker -->
         <docker.jib-maven-plugin.version>3.4.0</docker.jib-maven-plugin.version>
         <docker.image.prefix>springcommunity</docker.image.prefix>
         <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
+
+        <!-- Sonar Scanner -->
+        <sonar.host.url>https://sonarqube.cs.ui.ac.id</sonar.host.url>
+        <sonar.projectKey>pmpl_examples_spring-petclinic-rest_AYtlCaXx94kwVhMRxVzs</sonar.projectKey>
+        <sonar.qualitygate.wait>true</sonar.qualitygate.wait>
+        <sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/*.xml</sonar.coverage.jacoco.xmlReportPaths>
+        <sonar.junit.reportPaths>target/surefire-reports/*.xml</sonar.junit.reportPaths>
+        <sonar.qualitygate.wait>true</sonar.qualitygate.wait>
     </properties>
 
     <dependencies>
@@ -339,6 +348,11 @@
                     </compilerArgs>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.sonarsource.scanner.maven</groupId>
+                <artifactId>sonar-maven-plugin</artifactId>
+                <version>${sonar-maven-plugin.version}</version>
+            </plugin>
         </plugins>
     </build>
 </project>
-- 
GitLab


From b79555363dc77ea6d0a7326da463081b9b178f99 Mon Sep 17 00:00:00 2001
From: Daya Adianto <dayaadianto@cs.ui.ac.id>
Date: Wed, 25 Oct 2023 11:37:44 +0700
Subject: [PATCH 5/6] Configure deployment to Dokku PPL instance

---
 .gitlab-ci.yml | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3931f2e7..87608388 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -88,13 +88,22 @@ test:
 
 deploy:
   stage: deploy
-  image: docker.io/bitnami/git:2.42.0
+  image: docker.io/dokku/ci-docker-image:0.9.3
   rules: !reference [.upstream-deploy-production-rules, rules]
+  variables:
+    GIT_DEPTH: "0"
+    GIT_REMOTE_URL: "ssh://dokku@dokku-ppl.cs.ui.ac.id:22/spring-petclinic-rest"
+    SSH_PRIVATE_KEY: $PRODUCTION_SSH_PRIVATE_KEY
+    BRANCH: $CI_DEFAULT_BRANCH
   script:
-    - echo "TODO"
+    - dokku-deploy
+  after_script:
+    - dokku-unlock
   environment:
     name: production
     url: https://spring-petclinic-rest.dokku-ppl.cs.ui.ac.id
+  needs:
+    - test
   dependencies: []
 
 visualize-coverage:
@@ -126,3 +135,5 @@ sonarqube-check:
     key: "${CI_JOB_NAME}"
     paths:
       - .sonar/cache
+
+# TODO: Add manual CI job to re-deploy or re-create the deployed app on Dokku PPL
-- 
GitLab


From bfd22c6556e6ce5db14d67384f25f822aba1d570 Mon Sep 17 00:00:00 2001
From: Daya Adianto <dayaadianto@cs.ui.ac.id>
Date: Wed, 25 Oct 2023 11:48:44 +0700
Subject: [PATCH 6/6] Remove duplicate property in pom.xml

---
 pom.xml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index ac76dd6e..bdb11afd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -39,7 +39,6 @@
         <!-- Sonar Scanner -->
         <sonar.host.url>https://sonarqube.cs.ui.ac.id</sonar.host.url>
         <sonar.projectKey>pmpl_examples_spring-petclinic-rest_AYtlCaXx94kwVhMRxVzs</sonar.projectKey>
-        <sonar.qualitygate.wait>true</sonar.qualitygate.wait>
         <sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/*.xml</sonar.coverage.jacoco.xmlReportPaths>
         <sonar.junit.reportPaths>target/surefire-reports/*.xml</sonar.junit.reportPaths>
         <sonar.qualitygate.wait>true</sonar.qualitygate.wait>
-- 
GitLab