From 5134e4c4b172e95fd45dcd03a48c9287d87bfe0f Mon Sep 17 00:00:00 2001
From: Emmanuel Bruno <emmanuel.bruno@univ-tln.fr>
Date: Wed, 15 Dec 2021 15:18:26 +0100
Subject: [PATCH] adds sonar.

---
 .github/ci-settings.xml            | 37 ++++++++++++++++++++++++++++++
 .github/workflows/docker-image.yml | 14 +++++++++--
 docker-compose.yml                 |  2 +-
 mvn.sh                             | 17 ++++++++++++++
 4 files changed, 67 insertions(+), 3 deletions(-)
 create mode 100644 .github/ci-settings.xml
 create mode 100755 mvn.sh

diff --git a/.github/ci-settings.xml b/.github/ci-settings.xml
new file mode 100644
index 0000000..c2019e0
--- /dev/null
+++ b/.github/ci-settings.xml
@@ -0,0 +1,37 @@
+<!-- A MAVEN SETTINGS FILE TO BE USED IN CI -->
+<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
+                          https://maven.apache.org/xsd/settings-1.0.0.xsd">
+
+    <servers>
+        <server>
+            <id>github</id>
+            <username>${env.GITHUBLOGIN}</username>
+            <password>${env.GITHUBPASSWORD}</password>
+        </server>
+        <server>
+            <id>dockerhub</id>
+            <username>${env.DOCKER_USERNAME}</username>
+            <password>${env.DOCKER_PASSWORD}</password>
+        </server>
+        <server>
+            <id>github.pages</id>
+            <username>git</username>
+            <configuration>
+                <scmVersionType>branch</scmVersionType>
+                <scmVersion>gh-pages</scmVersion>
+            </configuration>
+        </server>
+    </servers>
+
+    <profiles>
+        <profile>
+            <id>sonar</id>
+            <properties>
+                <sonar.host.url>http://compute-lsis-2:9000</sonar.host.url>
+                <sonar.login>${env.SONAR_TOKEN}</sonar.login>
+            </properties>
+        </profile>
+    </profiles>
+
+</settings>
diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml
index 1224b82..a6f9549 100644
--- a/.github/workflows/docker-image.yml
+++ b/.github/workflows/docker-image.yml
@@ -14,5 +14,15 @@ jobs:
 
     steps:
     - uses: actions/checkout@v2
-    - name: Build the Docker image
-      run: DOCKER_BUILDKIT=1 docker build . --file ./docker/Dockerfile --tag my-image-name:$(date +%s)
+    - name: Build the Docker image for branch ${GITHUB_REF##*/}
+      run: DOCKER_BUILDKIT=1 docker build . --file ./docker/Dockerfile --tag brunoe/samplejee91::${GITHUB_REF##*/}
+
+
+  maven-sonar:
+    runs-on: [ self-hosted, Linux ]
+    needs: maven-build
+    # Develop branch only
+    # if: github.ref == 'refs/heads/develop'
+    steps:
+      - name: Launch a sonar analysis
+        run: ./mvn.sh -D sonar.branch.name=${GITHUB_REF#refs/heads/} --activate-profiles sonar sonar:sonar && mvn --activate-profiles sonar -pl . sonar-quality-gate:check
diff --git a/docker-compose.yml b/docker-compose.yml
index 8f7e82d..efa6f90 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -4,7 +4,7 @@ services:
     build:
       context: .
       dockerfile: docker/Dockerfile
-    image: brunoe/samplejee91restapp
+    image: brunoe/samplejee91
     environment:
       - ADMIN_PASSWORD=admin
     ports:
diff --git a/mvn.sh b/mvn.sh
new file mode 100755
index 0000000..5d5927e
--- /dev/null
+++ b/mvn.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+docker run \
+          --env GITHUBLOGIN="$GITHUBLOGIN" \
+          --env GITHUBPASSWORD="$GITHUBPASSWORD" \
+          --env SONAR_TOKEN="$SONAR_TOKEN" \
+          --volume ~/.m2:/var/maven/.m2 \
+          --volume ~/.sonar:/var/maven/.sonar \
+          --volume ~/.config:/var/maven/.config \
+          --volume ~/.ssh:/home/user/.ssh \
+          --volume ~/.gitconfig:/home/user/.gitconfig \
+          --volume "$(pwd)":/usr/src/mymaven \
+          --workdir /usr/src/mymaven \
+          --rm \
+          --env PUID="$(id -u)" -e PGID="$(id -g)" \
+          --env MAVEN_CONFIG=/var/maven/.m2 \
+          brunoe/maven:3.8.1-jdk-16 \
+          runuser --user user --group user -- mvn -B -e -T 1C -Duser.home=/var/maven --settings /usr/src/mymaven/.github/ci-settings.xml "$@"
\ No newline at end of file
-- 
GitLab