Skip to content
Snippets Groups Projects
Jenkinsfile 15.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • OZGCloud's avatar
    OZGCloud committed
    def FAILED_STAGE
    
    
    OZGCloud's avatar
    OZGCloud committed
    pipeline {
    
    OZGCloud's avatar
    OZGCloud committed
        agent {
            node {
                label 'jenkins-worker'
            }
    
    OZGCloud's avatar
    OZGCloud committed
        }
    
    
        environment {
            BLUE_OCEAN_URL = "https://jenkins.ozg-sh.de/blue/organizations/jenkins/goofy/detail/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/pipeline"
    
            RELEASE_REGEX = /\d+.\d+.\d+/
            SNAPSHOT_REGEX = /\d+.\d+.\d+-SNAPSHOT/
    
    OZGCloud's avatar
    OZGCloud committed
        options {
            timeout(time: 1, unit: 'HOURS')
            disableConcurrentBuilds()
    
    OZGCloud's avatar
    OZGCloud committed
            buildDiscarder(logRotator(numToKeepStr: '5'))
    
    OZGCloud's avatar
    OZGCloud committed
        }
    
        stages {
    
    OZGCloud's avatar
    OZGCloud committed
    //         stage('Check Version') {
    //             steps {
    //                 script {
    //                     FAILED_STAGE = env.STAGE_NAME
    //                     def rootPom = readMavenPom file: 'pom.xml'
    //                     def rootVersion = rootPom.version
    
    OZGCloud's avatar
    OZGCloud committed
    //                     def serverPom = readMavenPom file: 'goofy-server/pom.xml'
    //                     def serverVersion = serverPom.parent.version
    
    OZGCloud's avatar
    OZGCloud committed
    //                     def clientPom = readMavenPom file: 'goofy-client/pom.xml'
    //                     def clientVersion = clientPom.parent.version
    
    OZGCloud's avatar
    OZGCloud committed
    //                     if(env.BRANCH_NAME == 'release'){
    //                         if ( !(rootVersion ==~ RELEASE_REGEX) || !(serverVersion ==~ RELEASE_REGEX) || !(clientVersion ==~ RELEASE_REGEX)) {
    //                             error("Keine Release Version für Branch ${env.BRANCH_NAME}.")
    //                         }
    //                     } else {
    //                         if ( !(rootVersion ==~ SNAPSHOT_REGEX) || !(serverVersion ==~ SNAPSHOT_REGEX) || !(clientVersion ==~ SNAPSHOT_REGEX)) {
    //                             error("Keine Snapshot Version für Branch ${env.BRANCH_NAME}.")
    //                         }
    //                     }
    
    OZGCloud's avatar
    OZGCloud committed
    //                     if( !(rootVersion == serverVersion && rootVersion == clientVersion)){
    //                         error("Versionen sind nicht identisch")                        
    //                     }
    //                 }
    //             }
    //         }
    //         stage('Client') {
    //             steps {
    //                 container("nodejs"){
    //                     script {
    //                         FAILED_STAGE=env.STAGE_NAME
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    //                         sh 'npm --version'
    //                         dir('goofy-client') {
    //                             sh 'echo "registry=http://nexus.ozg-sh.de/repository/npm-proxy" >> ~/.npmrc'
    //                             sh 'echo "_auth=amVua2luczpQaihzX0ZNNFU5ZC8=" >> ~/.npmrc'
    
    OZGCloud's avatar
    OZGCloud committed
    //                             sh 'npm install --no-optional'
    
    OZGCloud's avatar
    OZGCloud committed
                            
    
    OZGCloud's avatar
    OZGCloud committed
    //                             if (env.BRANCH_NAME == 'release') {
    //                                 sh 'npm run ci-prodBuild'
    //                             }
    //                             else {
    //                                 sh 'npm run ci-build'
    //                             }
    
    OZGCloud's avatar
    OZGCloud committed
    //                             sh 'npm run ci-test'
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    // 							try {
    // 	                            if (env.BRANCH_NAME == 'master') {
    // 	                                withSonarQubeEnv('sonarqube-ozg-sh'){
    // 	                                    sh 'npm run ci-sonar'
    // 	                                }
    // 	                            }
    //                             } catch (Exception e) {
    //                                unstable("SonarQube failed")
    //                             }
    //                         }
    //                     }
    //                 }
    //             }
    // //            post {
    // //                always{
    // //                    junit testResults: 'goofy-client/test-report.xml', skipPublishingChecks: true
    // //                }
    // //            }
    //         }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    //         stage('Server') {
    //             steps {
    //                 script {
    //                     FAILED_STAGE=env.STAGE_NAME
    //                 }
    //                 container("maven-17"){ 
    //                     configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
    //                         sh 'mvn --version'
    //                         sh 'mvn -s $MAVEN_SETTINGS -pl -goofy-client clean install spring-boot:build-image -Dmaven.wagon.http.retryHandler.count=3'
    //                     }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    //                     script {
    //                     	try {
    // 	                        if (env.BRANCH_NAME == 'master') {
    // 	        	                dir('goofy-server'){
    // 	                                withSonarQubeEnv('sonarqube-ozg-sh'){
    // 	                                    sh 'mvn sonar:sonar'
    // 	                                }
    // 	                            }
    // 		                    }
    //                         } catch (Exception e) {
    //                             unstable("SonarQube failed")
    //                         }	                    
    //                     }
    //                 }
    //             }
    //             post {
    //                 always{
    //                     junit testResults: '**/target/surefire-reports/*.xml', skipPublishingChecks: true
    //                 }
    //             }
    //         }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    //         stage('Push Docker image') {
    //             steps {
    //                 script {
    //                     FAILED_STAGE=env.STAGE_NAME
    //                     def pom = readMavenPom file: 'pom.xml'
    //                     def version = pom.version
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    //                     container("docker") {
    //                         withCredentials([usernamePassword(credentialsId: 'jenkins-docker-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
    //                             sh 'docker login docker.ozg-sh.de -u ${USER} -p ${PASSWORD}'
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    //                             sh "docker tag docker.ozg-sh.de/goofy:build-latest docker.ozg-sh.de/goofy:${env.BRANCH_NAME}-${version}"
    //                             sh "docker push docker.ozg-sh.de/goofy:${env.BRANCH_NAME}-${version}"
    
    OZGCloud's avatar
    OZGCloud committed
    //                             if (env.BRANCH_NAME == 'master') {
    //                                 sh 'docker tag docker.ozg-sh.de/goofy:build-latest docker.ozg-sh.de/goofy:e2e-test'
    //                                 sh 'docker push docker.ozg-sh.de/goofy:e2e-test'
    //                             }
    //                         }
    //                     }
    //                 }
    //             }
    //         }
    
    OZGCloud's avatar
    OZGCloud committed
            stage('E2E-EA-kc') {
    
    OZGCloud's avatar
    OZGCloud committed
                    branch 'ozg-1784'
    
    OZGCloud's avatar
    OZGCloud committed
                
    
                steps {
                    script {
                        FAILED_STAGE=env.STAGE_NAME
    
    OZGCloud's avatar
    OZGCloud committed
                        namespace = generateNamespaceName(env.STAGE_NAME)
                        createKeycloakUser(env.STAGE_NAME)
    
    OZGCloud's avatar
    OZGCloud committed
    
                        container("k8s") {
    
                            println(namespace)
    
                            configFileProvider([configFile(fileId: 'jenkins-kuby-kubeconfig', variable: 'KUBE_CONFIG')]) {
                                sh 'mkdir ~/.kube'
                                sh 'cp ${KUBE_CONFIG} ~/.kube/config'
                            }
    
    OZGCloud's avatar
    OZGCloud committed
                            sh 'helm version'
    
                            sh 'kubectl get nodes'
    
    OZGCloud's avatar
    OZGCloud committed
                            def namespaceList = sh (script: 'kubectl get namespaces', returnStdout: true)
                            println(namespaceList)
    
    OZGCloud's avatar
    OZGCloud committed
                            if(namespaceList.contains(namespace)) {
                                error("Namespace: ${namespace} existiert bereits")
    
    OZGCloud's avatar
    OZGCloud committed
    
                            parallel(
                                startGoofy: {
                                    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.yaml --namespace ${namespace}" + ' --repo https://nexus.ozg-sh.de/repository/ozg-base-apps --username ${USER} --password ${PASSWORD} --wait --wait-for-jobs'
                                        }
                                    }
                                },
                                startPluto: {
                                    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.yaml --namespace ${namespace}" + ' --repo https://nexus.ozg-sh.de/repository/ozg-base-apps --username ${USER} --password ${PASSWORD} --wait --wait-for-jobs'
                                        }
                                    }
                                   sh "kubectl rollout status statefulset/pluto-database -n ${namespace}"
                                }
                            )
                           
    
                            println('running')
    
                            println('testing')
    
    OZGCloud's avatar
    OZGCloud committed
                        //     sh "helm uninstall goofy --namespace ${namespace} --wait"
                        //     sh "helm uninstall pluto --namespace ${namespace} --wait"
    
                        //     sh "kubectl delete namespace ${namespace}"
    
    OZGCloud's avatar
    OZGCloud committed
                        
                        // container("cypress") {
                        //     dir('goofy-client') {
                        //         sh 'npm run cypress:version'
                        //         sh 'npm run cypress:ci-run --BASE_URL=https://e2e.dev.ozg-sh.de'//TODO: durch eine dynamische url ersetzen
                        //     }
                        // }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            // stage('E2E-Tests') {
            //     when {
            //         branch 'master'
            //     }
            //     steps {
            //         script {
            //             FAILED_STAGE=env.STAGE_NAME
            //         }
    
    OZGCloud's avatar
    OZGCloud committed
            //         container("kubectl") {
            //             sh 'kubectl rollout restart deployment/goofy -n sh-e2e-dev'
            //             sh 'kubectl rollout status deployment/goofy -n sh-e2e-dev'
    
    OZGCloud's avatar
    OZGCloud committed
            //             sh 'kubectl rollout restart deployment/pluto -n sh-e2e-dev'
            //             sh 'kubectl rollout status deployment/pluto -n sh-e2e-dev'
            //         }
    
    OZGCloud's avatar
    OZGCloud committed
            //         container("cypress") {
            //             dir('goofy-client') {
            //                 sh 'npm run cypress:version'
            //                 sh 'npm run cypress:ci-run'
            //             }
            //         }
            //     }
            //     post {
            //         always {
            //             publishHTML (target: [
            //             allowMissing: false,
            //             alwaysLinkToLastBuild: false,
            //             keepAll: true,
            //             reportDir: 'goofy-client/apps/goofy-e2e/reports',
            //             reportFiles: 'report.html',
            //             reportName: "Goofy E2E-Tests"
            //             ])
            //         }
            //     }
            // }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            // stage('Deploy Maven Artifacts to Nexus') {
            //     when {
            //         anyOf {
            //             branch 'master'
            //             branch 'release'
            //         }
            //     }
            //     steps {
            //         script {
            //             FAILED_STAGE = env.STAGE_NAME
            //         }
            //         container('maven-17') {
            //             configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
            //                 sh 'mvn -s $MAVEN_SETTINGS -pl -goofy-client -DskipTests deploy'
            //             }
            //         }
            //     }
            // }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            // stage('Deploy Goofy') {
            //     when {
            //         anyOf {
            //             branch 'master'
            //             branch 'release'
            //         }
            //     }
    
    OZGCloud's avatar
    OZGCloud committed
            //     steps {
            //         script {
            //             FAILED_STAGE = env.STAGE_NAME
            //         }
            //         container("docker") {
            //             script {
            //                 withCredentials([usernamePassword(credentialsId: 'jenkins-docker-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
            //                     sh 'docker login docker.ozg-sh.de -u ${USER} -p ${PASSWORD}'
    
    OZGCloud's avatar
    OZGCloud committed
            //                     if (env.BRANCH_NAME == 'release') {
            //                         sh 'docker tag docker.ozg-sh.de/goofy:build-latest docker.ozg-sh.de/goofy:latest'
            //                         sh 'docker push docker.ozg-sh.de/goofy:latest'
            //                     }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            //                     if (env.BRANCH_NAME == 'master') {
            //                         sh 'docker tag docker.ozg-sh.de/goofy:build-latest docker.ozg-sh.de/goofy:snapshot-latest'
            //                         sh 'docker push docker.ozg-sh.de/goofy:snapshot-latest'
            //                     }
            //                 }
            //             }
            //         }
    
    OZGCloud's avatar
    OZGCloud committed
            //         container("kubectl"){
            //             script {
            //                 if (env.BRANCH_NAME == 'release') {
            //                     sh 'kubectl rollout restart deployment/goofy -n sh-kiel-test'
            //                     sh 'kubectl rollout status deployment/goofy -n sh-kiel-test'
            //                     sh 'kubectl rollout restart deployment/goofy -n sh-sl-test'
            //                     sh 'kubectl rollout status deployment/goofy -n sh-sl-test'
            //                 }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            //                 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'
            //                 }
            //             }
            //         }
            //     }
            // }
    
    OZGCloud's avatar
    OZGCloud committed
        // post {
        //     failure {
        //         script {
        //             if (env.BRANCH_NAME == 'master') {
        //                 slackSend(color: "danger", message: "Goofy: Build Failed. Stage: ${FAILED_STAGE} Build-ID: <${BLUE_OCEAN_URL}|${env.BUILD_NUMBER}>")
        //             }
        //         }
        //     }
        // }
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    String generateNamespaceName(String stage) {
    
    OZGCloud's avatar
    OZGCloud committed
        branchName = env.BRANCH_NAME.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase()
        stageName = stage.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase()
        
        return "e2e-${branchName}${stageName}-dev"
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void createKeycloakUser(String stage) {
        namespace = generateNamespaceName(stage)
    	
        e2eUserFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/user', returnStdout: true)
    
        baseUserYaml = readYaml file: "goofy-client/apps/goofy-e2e/deployment-values/goofy/user/user.yaml"
    
        e2eUserFiles.split("\\n").each { user ->
        	dir('goofy-client/apps/goofy-e2e/src/fixtures/user') {
        		userJson = readJSON file: user
    
        		baseUserYaml.metadata.name = namespace + "-" + userJson.name
        		baseUserYaml.metadata.labels.realm = namespace
        		baseUserYaml.spec.realmSelector.matchLabels.realm = namespace
    
        		baseUserYaml.spec.user.username = userJson.name
        		baseUserYaml.spec.user.credentials = [[type: 'password', value: userJson.password]]
    
                if(userJson.firstName) {
            		baseUserYaml.spec.user.firstName = userJson.firstName
                }
    
                if(userJson.lastName) {
            		baseUserYaml.spec.user.lastName = userJson.lastName
                }
    
                if(userJson.einheitlicherAnsprechpartner) {
                    baseUserYaml.spec.user.clientRoles = [namespace: ['EINHEITLICHER_ANSPRECHPARTNER']]
                }
    
                if(userJson.verwaltungUser) {
                    baseUserYaml.spec.user.clientRoles = [namespace: ['VERWALTUNG_USER']]
                }
        	}
    
            writeYaml file: "kc-user.yaml", data: baseUserYaml
    
            sh "kubectl apply -f kc-user.yaml"
            sh "rm kc-user.yaml"
    
    OZGCloud's avatar
    OZGCloud committed
        }
    
    OZGCloud's avatar
    OZGCloud committed
    }