From fa43d8889d3ea71b3ffc6a6b7a85b283c7e417cc Mon Sep 17 00:00:00 2001 From: Emmanuel Bruno <emmanuel.bruno@univ-tln.fr> Date: Tue, 2 Feb 2021 10:42:41 +0100 Subject: [PATCH] adds docke maven and github CI. --- .github/ci-settings.xml | 37 +++++++++++++++++++++++ .github/workflows/ci.yml | 55 +++++++++++++++++++++++++++++++++++ .github/workflows/cleanup.yml | 15 ++++++++++ mvn.sh | 17 +++++++++++ pom.xml | 4 +-- 5 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 .github/ci-settings.xml create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/cleanup.yml create mode 100755 mvn.sh diff --git a/.github/ci-settings.xml b/.github/ci-settings.xml new file mode 100644 index 0000000..5ee9c16 --- /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://pc-bruno:9000</sonar.host.url> + <sonar.login>${env.SONAR_TOKEN}</sonar.login> + </properties> + </profile> + </profiles> + +</settings> diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..14e1b6c --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,55 @@ +## A simple example to use Github Actions for Java CI +## This produces a simple jar artefact are store in the github maven repository +# We build the project using maven. +# The credential ae set in a specific settings.xml using env variables +# The ENV variables are stored as Github Secrets +# +# The secrets are set in CLI with https://www.npmjs.com/package/gh-create-update-secret +# +# ghrepo=`git remote -v|grep fetch|sed 's/.*:\(.*\).git/\1/'` +# export GH_PAT=$GITHUB_PASSWORD +# gh-create-update-secret --secret GITHUBLOGIN --value $GITHUB_LOGIN --repo $ghrepo +# gh-create-update-secret --secret GITHUBPASSWORD --value $GITHUB_PASSWORD --repo $ghrepo + +# gh-create-update-secret --secret DOCKER_USERNAME --value $DOCKER_USERNAME --repo $ghrepo +# gh-create-update-secret --secret DOCKER_PASSWORD --value $DOCKER_PASSWORD --repo $ghrepo + +# gh-create-update-secret --secret SONAR_TOKEN --value $SONAR_TOKEN --repo $ghrepo +name: Java CI + +# on every push +on: [push] + +env: + GITHUBLOGIN: ${{secrets.GITHUBLOGIN}} + GITHUBPASSWORD: ${{secrets.GITHUBPASSWORD}} + DOCKER_USERNAME: ${{secrets.DOCKER_USERNAME}} + DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}} + SONAR_TOKEN: ${{secrets.SONAR_TOKEN}} + +jobs: + # This job build, test, package and deploy the artefact + maven-build: + # The tags on the runner + runs-on: [self-hosted, Linux] + + steps: + #we get the content of the repository + - uses: actions/checkout@v2 + + # build the project in a docker container with a specific maven settings.xml + # it uses env variables from github secrets for the credentials + # to github, dockerhub and sonar. + - name: Build and test with Maven in docker + run: ./mvn.sh clean verify + + # This job runs a sonarqube analysis on the hosted runner + # only on develop branch + 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/.github/workflows/cleanup.yml b/.github/workflows/cleanup.yml new file mode 100644 index 0000000..ca79bbb --- /dev/null +++ b/.github/workflows/cleanup.yml @@ -0,0 +1,15 @@ +name: Delete Old Snpashot Package # on every push +on: [push] +jobs: + cleanup: + # The tags on the runner + runs-on: [self-hosted, Linux] + steps: + - uses: smartsquaregmbh/delete-old-packages@v0.2.0 + with: + keep: 1 + names: | + fr.univtln.bruno.demos.jaxrs.demojaxrs + fr.univtln.bruno.demos.jaxrs.model + fr.univtln.bruno.demos.jaxrs.server + fr.univtln.bruno.demos.jaxrs.javaclient diff --git a/mvn.sh b/mvn.sh new file mode 100755 index 0000000..32c5a76 --- /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.6.3-jdk-11-openj9 \ + 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 diff --git a/pom.xml b/pom.xml index 0c023ee..0539581 100644 --- a/pom.xml +++ b/pom.xml @@ -2,10 +2,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>fr.univtln.bruno.exemple.simplerest</groupId> - <artifactId>restbiblio</artifactId> + <artifactId>sample-jaxrs</artifactId> <packaging>jar</packaging> <version>2.0-SNAPSHOT</version> - <name>restbiblio</name> + <name>Sample JAX-RS</name> <dependencyManagement> <dependencies> <!-- Jakarta RESTful Web Services (JAXRS) https://jakarta.ee/specifications/restful-ws/3.0/ --> -- GitLab