From aca845b1f4478a06f20d0ce9b0dbe7602b12a834 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Tue, 7 Jun 2022 15:30:32 +0200
Subject: [PATCH] ozg-2502 trigger test rollout

---
 Jenkinsfile | 105 ++++++++++++++++++++++++++++++++++------------------
 1 file changed, 70 insertions(+), 35 deletions(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index 1ca0692ac2..f9f68bb626 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -125,22 +125,6 @@ pipeline {
                 }
             }
         }
-        stage('Init k8s') {
-            steps {
-                script {
-                    FAILED_STAGE = env.STAGE_NAME
-                    E2E_FAILED = ""
-                }
-                container("k8s") {
-                    configFileProvider([configFile(fileId: 'jenkins-kuby-kubeconfig', variable: 'KUBE_CONFIG')]) {
-                        sh 'mkdir ~/.kube'
-                        sh 'cp ${KUBE_CONFIG} ~/.kube/config'
-                    }
-
-                    sh 'helm version'
-                }
-            }
-        }
         stage('Deploy Maven Artifacts to Nexus') {
             when {
                 anyOf {
@@ -190,14 +174,26 @@ pipeline {
                 }
             }
         }
-        stage('Rollout (kiel-dev & ea-dev) | (kiel-test $ sl-test) Goofy') {
-            when {
-                anyOf {
-                    branch 'master'
-                    branch 'release'
+        stage('Init k8s') {
+            steps {
+                script {
+                    FAILED_STAGE = env.STAGE_NAME
+                    E2E_FAILED = ""
+                }
+                container("k8s") {
+                    configFileProvider([configFile(fileId: 'jenkins-kuby-kubeconfig', variable: 'KUBE_CONFIG')]) {
+                        sh 'mkdir ~/.kube'
+                        sh 'cp ${KUBE_CONFIG} ~/.kube/config'
+                        sh 'cat ~/.kube/config'
+                    }
+                    sh 'helm version'
                 }
             }
-
+        }
+        stage('Rollout Dev Goofy') {
+            when {
+                branch 'master'
+            }
             steps {
                 script {
                     FAILED_STAGE = env.STAGE_NAME
@@ -205,12 +201,42 @@ pipeline {
 
                 container("k8s"){
                     script {
-                        if (env.BRANCH_NAME == 'master') {
-                            sh 'kubectl rollout restart deployment/goofy -n sh-kiel-dev'
-                            sh 'kubectl rollout status deployment/goofy -n sh-kiel-dev'
-
-                            sh 'kubectl rollout restart deployment/goofy -n sh-ea-dev'
-                            sh 'kubectl rollout status deployment/goofy -n sh-ea-dev'
+                            sh """
+                                error=0
+                                for NAMESPACE in \$(kubectl get namespaces -l ozg-environment=dev -o custom-columns=NAME:.metadata.name --no-headers); \
+                                do \
+                                    if kubectl get deployment -n \$NAMESPACE | grep goofy; then \
+                                        kubectl rollout restart deployment/goofy -n \$NAMESPACE; \
+                                        if ! kubectl rollout status deployment/goofy -n \$NAMESPACE; then \
+                                            error=1; \
+                                        fi; \
+                                    fi; \
+                                done
+                                exit \$error
+                            """
+                    }
+                }
+            }
+        }
+        stage('Trigger Test rollout') {
+            when {
+                branch 'release'
+            }
+            steps {
+                script {
+                    FAILED_STAGE = env.STAGE_NAME
+                  	
+                    withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
+                        sh 'git clone https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/rollout.git'
+
+                        dir('rollout') {
+                            sh "sed -i 's/GOOFY_APP_VERSION\\=.*/GOOFY_APP_VERSION=${VERSION}/' versions/dev/versions.sh"
+
+                            sh 'git add versions/dev/versions.sh'
+                            sh 'git config user.email "jenkins@ozg.de"'
+                            sh 'git config user.name "jenkins"'
+                            sh "git commit -m 'jenkins rollout goofy ${VERSION}'"
+                            sh 'git push https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/rollout.git'
                         }
                     }
                 }
@@ -329,7 +355,7 @@ Void checkIfNamespaceExists(String namespace) {
         def namespaceList = sh (script: 'kubectl get namespaces', returnStdout: true)
 
         if(namespaceList.contains(namespace)) {
-            error("Namespace: ${namespace} existiert bereits")
+            sh "kubectl delete namespace ${namespace}"
         }
     }
 }
@@ -337,9 +363,7 @@ Void checkIfNamespaceExists(String namespace) {
 Void startPluto(String namespace, String values, String bezeichner) {
     container("k8s") {
         dir('goofy-client/apps/goofy-e2e/deployment-values/pluto') {
-        	withCredentials([usernamePassword(credentialsId: 'jenkins-nexus-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
-            	sh "helm upgrade --install --create-namespace pluto pluto -f ${values} --set kop.bezeichner=${bezeichner} --namespace ${namespace} --version 0.12.0-SNAPSHOT" + ' --repo https://nexus.ozg-sh.de/repository/ozg-base-apps-snapshot --username ${USER} --password ${PASSWORD} --wait --wait-for-jobs'
-            }
+           	sh "helm upgrade --install --create-namespace pluto ozg-base-apps-snapshot/pluto -f ${values} --set kop.bezeichner=${bezeichner} --namespace ${namespace} --version ${getLatestChartVersion('pluto').trim()} --wait --wait-for-jobs"
         }
 
 	    sh "kubectl rollout status statefulset/pluto-database -n ${namespace}"
@@ -349,9 +373,7 @@ Void startPluto(String namespace, String values, String bezeichner) {
 Void startGoofy(String namespace, String values, String imageTag, String bezeichner) {
     container("k8s") {
         dir('goofy-client/apps/goofy-e2e/deployment-values/goofy') {
-        	withCredentials([usernamePassword(credentialsId: 'jenkins-nexus-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
-            	sh "helm upgrade --install --create-namespace goofy goofy -f ${values} --set image.tag=${imageTag} --set kop.bezeichner=${bezeichner} --namespace ${namespace} --version 0.14.0-SNAPSHOT" + ' --repo https://nexus.ozg-sh.de/repository/ozg-base-apps-snapshot --username ${USER} --password ${PASSWORD} --wait --wait-for-jobs'
-            }
+           	sh "helm upgrade --install --create-namespace goofy ozg-base-apps-snapshot/goofy -f ${values} --set image.tag=${imageTag} --set kop.bezeichner=${bezeichner} --namespace ${namespace} --version ${getLatestChartVersion('goofy').trim()} --wait --wait-for-jobs"
         }
 
         createKeycloakGroups(namespace)
@@ -360,6 +382,12 @@ Void startGoofy(String namespace, String values, String imageTag, String bezeich
     }
 }
 
+String getLatestChartVersion(String chart) {
+    container("k8s") {
+        return sh (script: "helm search repo ozg-base-apps-snapshot --devel -l -o json | jq -r 'first(.[] | select((.name==\"ozg-base-apps-snapshot/${chart}\") and (.version|match(\"SNAPSHOT\$\"))) | .version)'", returnStdout: true)
+    }
+}
+
 String runTests(String stageName, String reportFolder) {
 	container("cypress") {
         try {
@@ -397,6 +425,12 @@ String generateBezeichner(String stage) {
     def branchName = makeUrlConform(env.BRANCH_NAME)
     def stageName = makeUrlConform(stage)
 
+    def cutBranchNamePosition = 30 - (branchName.length() + stageName.length() + 8)
+
+    if(cutBranchNamePosition < 0) {
+        branchName = branchName[0..cutBranchNamePosition]
+    }
+
     return "${branchName}${stageName}"
 }
 
@@ -412,6 +446,7 @@ Void generateKeycloakUserYaml(String namespace) {
    		def userJson = readJSON file: 'goofy-client/apps/goofy-e2e/src/fixtures/user/'+user
 
    		newUserYaml.metadata.name = namespace + "-" + userJson.name
+        newUserYaml.metadata.namespace = namespace
    		newUserYaml.metadata.labels.realm = namespace
    		newUserYaml.spec.realmSelector.matchLabels.realm = namespace
    		newUserYaml.spec.user.username = userJson.name
-- 
GitLab