Skip to content
Snippets Groups Projects
Jenkinsfile 11.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • OZGCloud's avatar
    OZGCloud committed
    def FAILED_STAGE
    
    OZGCloud's avatar
    OZGCloud committed
    def E2E_FAILED
    def IMAGE_TAG
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    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('Init k8s') {
    
    OZGCloud's avatar
    OZGCloud committed
                        FAILED_STAGE = env.STAGE_NAME
                        IMAGE_TAG = 'latest'
                    }
                    container("k8s") {
                        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'
                    }
    				container('nodejs') {
                    	dir('goofy-client') {
                        	sh 'npm install --no-optional'
                        }
                    }
                }
            }
            stage('E2E') {
                failFast false
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
                parallel {
                    stage('E2E-EA') {
                        when {
                            branch 'ozg-1784'
                        }
                
                        steps {
                            script {
                                def stageName = env.STAGE_NAME
                                def namespace = generateNamespaceName(stageName)
    
                                checkIfNamespaceExists(namespace)
    
                                parallel(
                                    startGoofy: {
    
    OZGCloud's avatar
    OZGCloud committed
            							startGoofy(namespace, 'ea-values.yaml', IMAGE_TAG)
    
    OZGCloud's avatar
    OZGCloud committed
                                    },
                                    startPluto: {
    		        					startPluto(namespace, 'ea-values.yaml')
                                    }
                                )
                        
                                
    							if(runTests(stageName, 'einheitlicher-ansprechpartner') != '0') {
    				            	E2E_FAILED = true
                				}
    
    OZGCloud's avatar
    OZGCloud committed
    							shutdownEnvironment(namespace)
    
    OZGCloud's avatar
    OZGCloud committed
                        }
                        post {
                            always {
                                publishHTML (target: [
                                allowMissing: false,
                                alwaysLinkToLastBuild: false,
                                keepAll: true,
                                reportDir: 'goofy-client/apps/goofy-e2e/reports/einheitlicher-ansprechpartner',
                                reportFiles: 'report.html',
                                reportName: "Goofy E2E-Tests EA"
                                ])
                            }
                        }
                    }
                    stage('E2E-main') {
                        when {
                            branch 'ozg-1784'
                        }
    
    OZGCloud's avatar
    OZGCloud committed
                        steps {
                            script {
                                def stageName = env.STAGE_NAME
                                def namespace = generateNamespaceName(stageName)
    
    OZGCloud's avatar
    OZGCloud committed
                                checkIfNamespaceExists(namespace)
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
                                parallel(
                                    startGoofy: {
    
    OZGCloud's avatar
    OZGCloud committed
    								    startGoofy(namespace, 'values.yaml', IMAGE_TAG)
    
    OZGCloud's avatar
    OZGCloud committed
                                    },
                                    startPluto: {
    								    startPluto(namespace, 'values.yaml')
    
    OZGCloud's avatar
    OZGCloud committed
                                    }
    
    OZGCloud's avatar
    OZGCloud committed
                                )
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    							if(runTests(stageName, 'main-tests') != '0') {
    				            	E2E_FAILED = true
                				}
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    							shutdownEnvironment(namespace)
                            }
                        }
                        post {
                            always {
                                publishHTML (target: [
                                allowMissing: false,
                                alwaysLinkToLastBuild: false,
                                keepAll: true,
                                reportDir: 'goofy-client/apps/goofy-e2e/reports/main-tests',
                                reportFiles: 'report.html',
                                reportName: "Goofy E2E-Tests main"
                                ])
                            }
    
    OZGCloud's avatar
    OZGCloud committed
    			post {
            		always {
    					script {
    						if (E2E_FAILED) {
    							currentBuild.result = 'FAILURE'
    						}
    					}
                	}
    			}
    
    OZGCloud's avatar
    OZGCloud committed
        }
    }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void checkIfNamespaceExists(String namespace) {
        container("k8s") {
            def namespaceList = sh (script: 'kubectl get namespaces', returnStdout: true)
    
            if(namespaceList.contains(namespace)) {
                error("Namespace: ${namespace} existiert bereits")
            }
        }
    }
    
    Void startPluto(String namespace, String values) {
        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} --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}"
        }
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void startGoofy(String namespace, String values, String imageTag) {
    
    OZGCloud's avatar
    OZGCloud committed
        container("k8s") {
            dir('goofy-client/apps/goofy-e2e/deployment-values/goofy') {
            	withCredentials([usernamePassword(credentialsId: 'jenkins-nexus-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
    
    OZGCloud's avatar
    OZGCloud committed
                	sh "helm upgrade --install --create-namespace goofy goofy -f ${values} --set image.tag=${imageTag} --namespace ${namespace}" + ' --repo https://nexus.ozg-sh.de/repository/ozg-base-apps --username ${USER} --password ${PASSWORD} --wait --wait-for-jobs'
    
    OZGCloud's avatar
    OZGCloud committed
            createKeycloakGroups(namespace)
            generateKeycloakUserYaml(namespace)
            applyKeycloakUser(namespace)
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    Void runTests(String stageName, String reportFolder) {
    	container("cypress") {
        	def configFile = generateCypressConfig(stageName, reportFolder)
            dir("goofy-client") {
            	sh "npm run cypress:version"
                return sh (script: "npm run cypress:ci-run --CONFIG_FILE=${configFile} --REPORT_FOLDER=${reportFolder}", returnStatus: true)
            }
        }
    }
    
    Void shutdownEnvironment(String namespace) {
    	container("k8s") {
            sh "helm uninstall goofy --namespace ${namespace} --wait"
            sh "helm uninstall pluto --namespace ${namespace} --wait"
    
    
    OZGCloud's avatar
    OZGCloud committed
            removeKeycloakUser(namespace)
    
    OZGCloud's avatar
    OZGCloud committed
    
            sh "kubectl delete namespace ${namespace}"
        }
    }
    
    String makeUrlConform(String input) {
        return input.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase()
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    String generateNamespaceName(String stage) {
    
    OZGCloud's avatar
    OZGCloud committed
        def branchName = makeUrlConform(env.BRANCH_NAME)
        def stageName = makeUrlConform(stage)
    
    OZGCloud's avatar
    OZGCloud committed
        
        return "e2e-${branchName}${stageName}-dev"
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void generateKeycloakUserYaml(String namespace) {
    
    OZGCloud's avatar
    OZGCloud committed
        def e2eUserFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/user', returnStdout: true)
    
    OZGCloud's avatar
    OZGCloud committed
    
        e2eUserFiles.split("\\n").each { user ->
    
    OZGCloud's avatar
    OZGCloud committed
            def newUserYaml = readYaml file: "goofy-client/apps/goofy-e2e/deployment-values/goofy/user/user.yaml"
       		def userJson = readJSON file: 'goofy-client/apps/goofy-e2e/src/fixtures/user/'+user
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
       		newUserYaml.metadata.name = namespace + "-" + userJson.name
       		newUserYaml.metadata.labels.realm = namespace
       		newUserYaml.spec.realmSelector.matchLabels.realm = namespace
       		newUserYaml.spec.user.username = userJson.name
       		newUserYaml.spec.user.credentials = [[type: 'password', value: userJson.password]]
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            if(userJson.firstName) {
                newUserYaml.spec.user.firstName = userJson.firstName
            }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            if(userJson.lastName) {
           	    newUserYaml.spec.user.lastName = userJson.lastName
            }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            if(userJson.realmRoles) {
                newUserYaml.spec.user.realmRoles += userJson.realmRoles
            }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    		if(userJson.groups) {
    			newUserYaml.spec.user.groups = userJson.groups
    		}
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            dir (namespace) {
                writeYaml file: userJson.name+".yaml", data: newUserYaml
            }
        }
    }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void createKeycloakGroups(String realm) {
    	def groupFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/group', returnStdout: true)
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
        groupFiles.split("\\n").each { group ->
    
    OZGCloud's avatar
    OZGCloud committed
            def groupJson = sh (script: "cat goofy-client/apps/goofy-e2e/src/fixtures/group/${group}", returnStdout: true)
    
    OZGCloud's avatar
    OZGCloud committed
    
    		sh """curl -X POST 'https://sso.dev.ozg-sh.de/auth/admin/realms/${realm}/groups' \
    				-H 'Content-Type: application/json' \
    				-H 'Authorization: bearer ${getKeycloakAccessToken()}' \
    				--data-raw '${groupJson}'
    				"""
    	}
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void applyKeycloakUser(String namespace) {
        dir(namespace){
    
    OZGCloud's avatar
    OZGCloud committed
            def kcUserFiles = sh (script: "ls", returnStdout: true)
    
            kcUserFiles.split("\\n").each { user ->
                sh "kubectl apply -f ${user}"
            }
        }
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void removeKeycloakUser(String namespace) {	
        dir(namespace){
    
    OZGCloud's avatar
    OZGCloud committed
            def kcUserFiles = sh (script: "ls", returnStdout: true)
    
            kcUserFiles.split("\\n").each { user ->
                sh "kubectl delete -f ${user}"
            }
        }
    }
    
    String generateCypressConfig(String stage, String testFolder) {
        def namespace = generateNamespaceName(stage)
        def configName = "cypress-ci-"+testFolder+".json"
    
        dir('goofy-client/apps/goofy-e2e/'){
            def config = readJSON file: 'cypress-ci.json'
    
            config.baseUrl = "https://${makeUrlConform(env.BRANCH_NAME)}${makeUrlConform(stage)}.dev.ozg-sh.de" as String
            config.env.dbUrl = "mongodb+srv://pluto-database-user:XnHhfznNWg65NNd@pluto-database-svc.${namespace}.svc.cluster.local/admin?ssl=false" as String
            config.env.keycloakRealm = namespace as String
            config.env.keycloakClient = namespace + "-goofy" as String
    
    OZGCloud's avatar
    OZGCloud committed
    		config.env.sabineUuid = getKeycloakUuid(namespace, "sabine") as String
    
    OZGCloud's avatar
    OZGCloud committed
            config.integrationFolder = "./src/integration/${testFolder}" as String
            config.videosFolder = "./reports/${testFolder}/videos" as String
    	    config.screenshotsFolder = "./reports/${testFolder}/screenshots" as String
            config.reporterOptions.reportDir = "./reports/${testFolder}/mochawesome-report" as String
    
            writeJSON file: configName, json: config
    
    OZGCloud's avatar
    OZGCloud committed
        }
    
    OZGCloud's avatar
    OZGCloud committed
    
        return configName
    }
    
    String getKeycloakUuid(realm, userName) {
    
    OZGCloud's avatar
    OZGCloud committed
        def shScript = """curl -H 'Content-Type: application/json' \
                            -H 'Authorization: bearer ${getKeycloakAccessToken()}' \
                            'https://sso.dev.ozg-sh.de/auth/admin/realms/${realm}/users'
                        """
    
    	def users = readJSON text: sh(script: shScript, returnStdout: true)
    
    	for(user in users) {
    
    OZGCloud's avatar
    OZGCloud committed
    		if (user.username == userName) {
    
    OZGCloud's avatar
    OZGCloud committed
                return user.id
    
    OZGCloud's avatar
    OZGCloud committed
    		}
    	}
    }
    
    String getKeycloakAccessToken() {
    	withCredentials([usernamePassword(credentialsId: 'keycloak-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
    
    OZGCloud's avatar
    OZGCloud committed
    		def token = readJSON text: sh (script: 'curl -d "client_id=admin-cli" -d "username=$USER" -d "password=$PASSWORD" -d "grant_type=password" "https://sso.dev.ozg-sh.de/auth/realms/master/protocol/openid-connect/token"', returnStdout: true)
    
    		return token.access_token
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    OZGCloud's avatar
    OZGCloud committed
    }