From d7ae8b20f774d18582817f60c70a6d478f674724 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Thu, 13 Jul 2023 19:43:03 +0200
Subject: [PATCH] jenkins: add deployment

---
 Jenkinsfile | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index 111f56a..4e19d52 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -8,7 +8,7 @@ pipeline {
     }
 
     environment {
-        BLUE_OCEAN_URL = "https://jenkins.ozg-sh.de/job/kop-common/job/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/"
+        BLUE_OCEAN_URL = "https://jenkins.ozg-sh.de/job/api-lib/job/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/"
         RELEASE_REGEX = /\d+.\d+.\d+/
         SNAPSHOT_REGEX = /\d+.\d+.\d+-SNAPSHOT/
     }
@@ -49,5 +49,53 @@ pipeline {
                 }
  	       }
  	   } //stage build
+ 	   stage('Deploy to nexus') {
+ 	       when {
+ 	           anyOf {
+ 	               branch 'master'
+ 	               branch 'release'
+ 	           }
+ 	       }
+ 	       steps {
+ 	           script {
+ 	               FAILED_STAGE = env.STAGE_NAME
+ 	           }
+				configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
+                    sh 'mvn -s $MAVEN_SETTINGS -DskipTests deploy'
+                }
+ 	       }
+ 	   } //stage deploy
+ 	   post {
+          failure {
+              script {
+                  if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME == 'release') {
+                      sendFailureMessage()
+                  }
+              }
+          }
+       }
    } //stages
 } //pipeline
+
+Void sendFailureMessage() {
+    def room = ''
+    def data = """{"msgtype":"m.text", \
+                    "body":"api-lib: Build Failed. Stage: ${FAILED_STAGE} Build-ID: ${env.BUILD_NUMBER} Link: ${BLUE_OCEAN_URL}", \
+                    "format": "org.matrix.custom.html", \
+                    "formatted_body":"api-lib: Build Failed. Stage: ${FAILED_STAGE} Build-ID: <a href='${BLUE_OCEAN_URL}'>${env.BUILD_NUMBER}</a>"}"""
+       
+    if (env.BRANCH_NAME == 'master') {
+        room = "!iQPAvQIiRwRpNOszjw: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
+    }
+}
\ No newline at end of file
-- 
GitLab