From b4fda5f7775b7aa7295d17ae4ef0c05f2b1e80ce Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Wed, 9 Oct 2024 15:43:37 +0200
Subject: [PATCH] OZG-6811 Add Jeninsfile

---
 Jenkinsfile | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 162 insertions(+)
 create mode 100644 Jenkinsfile

diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..149c162
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,162 @@
+pipeline {
+    agent {
+       node {
+           label 'ozgcloud-jenkins-build-agent-jdk21'
+        }
+    }
+
+    environment {
+        BLUE_OCEAN_URL = "https://jenkins.infra.ozg-cloud.systems/job/ozgcloud-document-manager/job/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/"
+        RELEASE_REGEX = /\d+.\d+.\d+/
+        SNAPSHOT_REGEX = /\d+.\d+.\d+-SNAPSHOT/
+        FAILED_STAGE = ""
+        SH_SUCCESS_STATUS_CODE = 0
+    }
+
+    options {
+        timeout(time: 1, unit: 'HOURS')
+        disableConcurrentBuilds()
+        buildDiscarder(logRotator(numToKeepStr: '5'))
+    }
+
+    stages {
+        stage('Check Version') {
+            steps {
+                script {
+                    FAILED_STAGE = env.STAGE_NAME
+                    def rootVersion = getPomVersion('pom.xml')
+                    def serverVersion = getParentPomVersion('document-manager-server/pom.xml')
+                    def interfaceVersion = getPomVersion('document-manager-interface/pom.xml')
+
+                    if(serverVersion != rootVersion){
+                        error("document-manager-server Version stimmt nicht mit root überein")
+                    }
+                    if(interfaceVersion != rootVersion){
+                        error("document-manager-interface Version stimmt nicht mit root überein")
+                    }
+
+                    if(env.BRANCH_NAME == 'release'){
+                        if ( !(rootVersion ==~ RELEASE_REGEX)) {
+                            error("Keine Release Version für Branch ${env.BRANCH_NAME}.")
+                        }
+                    } else {
+                        if ( !(rootVersion ==~ SNAPSHOT_REGEX)) {
+                            error("Keine Snapshot Version für Branch ${env.BRANCH_NAME}.")
+                        }
+                    }
+                }
+            }
+        }
+
+        stage('Set Version') {
+          when {
+            not {
+                anyOf {
+                    branch 'master'
+                    branch 'release'
+                }
+            }
+          }
+          steps {
+                script {
+                    FAILED_STAGE=env.STAGE_NAME
+                    JAR_TAG = getPomVersion('pom.xml').replace("SNAPSHOT", "${env.BRANCH_NAME}-SNAPSHOT")
+                }
+                configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
+                    sh "mvn --no-transfer-progress -s $MAVEN_SETTINGS versions:set -DnewVersion=${JAR_TAG} -DprocessAllModules=true"
+                }
+          }
+        }
+
+        stage('Build Document-Manager') {
+          steps {
+                script {
+                    FAILED_STAGE=env.STAGE_NAME
+                }
+
+                configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
+                    sh 'mvn --no-transfer-progress -s $MAVEN_SETTINGS clean install -Dmaven.wagon.http.retryHandler.count=3'
+                }
+            }
+        }
+
+        stage('Deploy to Nexus'){
+            steps {
+                script {
+                    FAILED_STAGE = env.STAGE_NAME
+                }
+                configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
+                    sh 'mvn --no-transfer-progress -s $MAVEN_SETTINGS -DskipTests deploy -Dmaven.wagon.http.retryHandler.count=3'
+                }
+            }
+        }
+
+        stage('Sonar Checks') {
+            when {
+                branch 'master'
+            }
+        	steps {
+        	     script {
+                    FAILED_STAGE=env.STAGE_NAME
+
+                  	configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
+                       	try {
+                            withSonarQubeEnv('sonarqube-ozg-sh'){
+                                sh 'mvn -s $MAVEN_SETTINGS sonar:sonar'
+                            }
+                        } catch (Exception e) {
+                            unstable("SonarQube failed")
+                        }
+            		}
+                }
+        	}
+        }
+    }
+
+    post {
+        always {
+            junit testResults: '**/target/surefire-reports/*.xml', skipPublishingChecks: true
+        }
+        failure {
+            script {
+                if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME == 'release') {
+                    sendFailureMessage()
+                }
+            }
+        }
+    }
+}
+
+Void sendFailureMessage() {
+    def room = ''
+    def data = """{"msgtype":"m.text",
+                    "body":"Document-Manager: Build Failed. Stage: ${FAILED_STAGE} Build-ID: ${env.BUILD_NUMBER} Link: ${BLUE_OCEAN_URL}",
+                    "format": "org.matrix.custom.html",
+                    "formatted_body":"<h3>Document-Manager: Build Failed.</h3><b>Stage:</b> ${FAILED_STAGE}<br><b>Build-ID:</b> <a href='${BLUE_OCEAN_URL}'>${env.BUILD_NUMBER}</a>"}"""
+
+    if (env.BRANCH_NAME == 'master') {
+        room = "!GjqhmouBtnDbwUkAjx:matrix.ozg-sh.de"
+    }
+    else if (env.BRANCH_NAME == 'release') {
+        room = "!oWZpUGTFsxkJIYNfYg:matrix.ozg-sh.de"
+    }
+
+    sh "curl -XPOST -H 'authorization: Bearer ${getElementAccessToken()}' -d '${data}' https://matrix.ozg-sh.de/_matrix/client/v3/rooms/$room/send/m.room.message"
+}
+
+String getElementAccessToken() {
+    withCredentials([string(credentialsId: 'element-login-json', variable: 'LOGIN_JSON')]) {
+        return readJSON ( text: sh (script: '''curl -XPOST -d \"$LOGIN_JSON\" https://matrix.ozg-sh.de/_matrix/client/v3/login''', returnStdout: true)).access_token
+    }
+}
+
+String getPomVersion(String pomFile){
+    def pom = readMavenPom file: pomFile
+
+    return pom.version
+}
+
+String getParentPomVersion(String filePath) {
+    def pom = readMavenPom file: filePath
+    return pom.parent.version
+}
\ No newline at end of file
-- 
GitLab