Skip to content
Snippets Groups Projects
Jenkinsfile 29.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • OZGCloud's avatar
    OZGCloud committed
    pipeline {
    
    OZGCloud's avatar
    OZGCloud committed
        agent {
            node {
    
                label 'jenkins-build-agent-nodejs-16'
    
    OZGCloud's avatar
    OZGCloud committed
            }
    
    OZGCloud's avatar
    OZGCloud committed
        }
    
    
        environment {
    
            BLUE_OCEAN_URL = "https://jenkins.ozg-sh.de/job/goofy/job/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/"
    
            RELEASE_REGEX = /\d+.\d+.\d+/
            SNAPSHOT_REGEX = /\d+.\d+.\d+-SNAPSHOT/
    
            BUNDESLAND = "by"
    
            SSO_URL = "sso.dev.by.kop-cloud.de"
    
    OZGCloud's avatar
    OZGCloud committed
            CLUSTER_BASE_URL = "dev.by.kop-cloud.de"
    
    OZGCloud's avatar
    OZGCloud committed
            FAILED_STAGE = ""
            IMAGE_TAG = ""
            VERSION = ""
            E2E_FAILED = ""
    
    OZGCloud's avatar
    OZGCloud committed
            HELM_CHART_VERSION = ""
    
    OZGCloud's avatar
    OZGCloud committed
            SH_SUCCESS_STATUS_CODE = 0
    
    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'
                        VERSION = rootPom.version
    
                        def serverPom = readMavenPom file: 'goofy-server/pom.xml'
                        def serverVersion = serverPom.parent.version
    
                        def clientPom = readMavenPom file: 'goofy-client/pom.xml'
                        def clientVersion = clientPom.parent.version
    
    
    OZGCloud's avatar
    OZGCloud committed
                        if(isReleaseBranch()){
    
    OZGCloud's avatar
    OZGCloud committed
                            if ( !(VERSION ==~ RELEASE_REGEX) || !(serverVersion ==~ RELEASE_REGEX) || !(clientVersion ==~ RELEASE_REGEX)) {
                                error("Keine Release Version für Branch ${env.BRANCH_NAME}.")
                            }
                        } else {
                            if ( !(VERSION ==~ SNAPSHOT_REGEX) || !(serverVersion ==~ SNAPSHOT_REGEX) || !(clientVersion ==~ SNAPSHOT_REGEX)) {
                                error("Keine Snapshot Version für Branch ${env.BRANCH_NAME}.")
                            }
                        }
    
    OZGCloud's avatar
    OZGCloud committed
                        if( !(VERSION == serverVersion && VERSION == clientVersion)){
    
                            error("Versionen sind nicht identisch")
    
    OZGCloud's avatar
    OZGCloud committed
                        }
                    }
                }
            }
            stage('Client') {
                steps {
    
    OZGCloud's avatar
    OZGCloud committed
                    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=https://nexus.ozg-sh.de/repository/npm-proxy" >> ~/.npmrc'
    
                            sh 'echo "//nexus.ozg-sh.de/:_auth=amVua2luczpQaihzX0ZNNFU5ZC8=" >> ~/.npmrc'
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
    						sh 'npm cache verify'
    
                            sh 'npm install'
    
    OZGCloud's avatar
    OZGCloud committed
                            if (isReleaseBranch()) {
    
    OZGCloud's avatar
    OZGCloud committed
                                sh 'npm run ci-prodBuild'
                            }
                            else {
                                sh 'npm run ci-build'
                            }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
                            sh 'npm run ci-test'
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
        					try {
    
    OZGCloud's avatar
    OZGCloud committed
    	                        if (isMasterBranch()) {
    
    OZGCloud's avatar
    OZGCloud committed
    	                            withSonarQubeEnv('sonarqube-ozg-sh'){
    	                                sh 'npm run ci-sonar'
    
    OZGCloud's avatar
    OZGCloud committed
    	                            }
    
    OZGCloud's avatar
    OZGCloud committed
    	                        }
                            } catch (Exception e) {
                                unstable("SonarQube failed")
    
    OZGCloud's avatar
    OZGCloud committed
                            }
                        }
                    }
                }
    //            post {
    //                always{
    //                    junit testResults: 'goofy-client/test-report.xml', skipPublishingChecks: true
    //                }
    //            }
            }
            stage('Server') {
                steps {
                    script {
                        FAILED_STAGE=env.STAGE_NAME
    
                        IMAGE_TAG = generateImageTag()
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
                        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 -Dspring-boot.build-image.imageName=docker.ozg-sh.de/goofy:${IMAGE_TAG} -Dspring-boot.build-image.publish -Dmaven.wagon.http.retryHandler.count=3"
    
                           	try {
    
    OZGCloud's avatar
    OZGCloud committed
                                if (isMasterBranch()) {
    
    OZGCloud's avatar
    OZGCloud committed
            	                    dir('goofy-server'){
                                        withSonarQubeEnv('sonarqube-ozg-sh'){
                                            sh 'mvn -s $MAVEN_SETTINGS sonar:sonar'
    
    OZGCloud's avatar
    OZGCloud committed
                                        }
    
    OZGCloud's avatar
    OZGCloud committed
                                    }
    	                        }
                            } catch (Exception e) {
                                unstable("SonarQube failed")
    
                post {
                    always{
                        junit testResults: '**/target/surefire-reports/*.xml', skipPublishingChecks: true
                    }
                }
    
    OZGCloud's avatar
    OZGCloud committed
            }
            stage('Deploy Maven Artifacts to Nexus') {
                when {
                    anyOf {
                        branch 'master'
                        branch 'release'
                    }
                }
                steps {
                    script {
                        FAILED_STAGE = env.STAGE_NAME
                    }
    
    OZGCloud's avatar
    OZGCloud committed
    
                    configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
                        sh 'mvn -s $MAVEN_SETTINGS -pl -goofy-client -DskipTests deploy'
    
            stage('Tag and Push Docker Image') {
    
    OZGCloud's avatar
    OZGCloud committed
                when {
                    anyOf {
                        branch 'master'
                        branch 'release'
                    }
                }
    
    OZGCloud's avatar
    OZGCloud committed
                steps {
                    script {
                        FAILED_STAGE = env.STAGE_NAME
    
    
    OZGCloud's avatar
    OZGCloud committed
                        if (isMasterBranch()) {
    
                            tagAndPushDockerImage('snapshot-latest')
    
    OZGCloud's avatar
    OZGCloud committed
                        }
    
    OZGCloud's avatar
    OZGCloud committed
                        else if (isReleaseBranch()) {
    
                            tagAndPushDockerImage('latest')
    
            stage('Test, build and deploy Helm Chart') {
                steps {
    
    OZGCloud's avatar
    OZGCloud committed
                    script {
                        FAILED_GOOFY_STAGE=env.STAGE_NAME
                        HELM_CHART_VERSION = generateHelmChartVersion()
    
    OZGCloud's avatar
    OZGCloud committed
                        dir('src/main/helm') {
                            sh "helm lint -f test-values.yaml"
    
                            sh "helm unittest -f '../../test/helm/*.yaml' -v '../../test/unit-values.yaml' ."
    
    OZGCloud's avatar
    OZGCloud committed
                            sh "helm package --version=${HELM_CHART_VERSION} ."
    
    OZGCloud's avatar
    OZGCloud committed
                            deployHelmChart(HELM_CHART_VERSION)
    
    OZGCloud's avatar
    OZGCloud committed
                        }
    
            stage('Trigger Dev rollout') {
    
    OZGCloud's avatar
    OZGCloud committed
                when {
    
    OZGCloud's avatar
    OZGCloud committed
                }
                steps {
                    script {
    
    OZGCloud's avatar
    OZGCloud committed
                        FAILED_STAGE = env.STAGE_NAME
    
    OZGCloud's avatar
    OZGCloud committed
                        cloneRolloutGitopsRepo()
    
    OZGCloud's avatar
    OZGCloud committed
                        setNewDevGoofyVersion()
                        pushRolloutGitopsRepo()
    
    OZGCloud's avatar
    OZGCloud committed
                stages {
    
                    stage('Init k8s') {
                        steps {
                            script {
                                FAILED_STAGE = env.STAGE_NAME
                                E2E_FAILED = ""
    
                                configFileProvider([configFile(fileId: 'kubeconfig-dev-cluster', variable: 'KUBE_CONFIG')]) {
                                    sh 'mkdir ~/.kube'
                                    sh 'cp ${KUBE_CONFIG} ~/.kube/config'
                                }
    
                                sh 'helm version'
                            }
                        }
                    }
    
    OZGCloud's avatar
    OZGCloud committed
                    stage('Install cypress') {
    
    OZGCloud's avatar
    OZGCloud committed
                                FAILED_STAGE = env.STAGE_NAME
    
                                E2E_FAILED = ""
    
    OZGCloud's avatar
    OZGCloud committed
                                dir('goofy-client') {
    
    OZGCloud's avatar
    OZGCloud committed
                                    sh "npm run cypress:install"
    
                    stage('Rollout E2E Namespaces') {
                        steps {
                            script {
                                FAILED_STAGE = env.STAGE_NAME
                                E2E_FAILED = ""
    
    
    OZGCloud's avatar
    OZGCloud committed
                                cloneE2eGitopsRepo()
    
    
                                env.EA_BEZEICHNER = generateBezeichner("e2e-ea")
                                env.MAIN_BEZEICHNER = generateBezeichner("e2e-main")
    
                                deleteKopStack([env.EA_BEZEICHNER, env.MAIN_BEZEICHNER])
    
    
    OZGCloud's avatar
    OZGCloud committed
                                generateEaNamespaceYaml()
                                generateMainNamespaceYaml()
    
    OZGCloud's avatar
    OZGCloud committed
                                pushE2eGitopsRepo()
    
    
                                rolloutKopStack([env.EA_BEZEICHNER, env.MAIN_BEZEICHNER])
                            }
                        }
                    }
                    stage('Add E2E User') {
                        steps {
                            script {
                                FAILED_STAGE = env.STAGE_NAME
                                E2E_FAILED = ""
    
    
    OZGCloud's avatar
    OZGCloud committed
                                cloneProvisioningRepo()
    
    OZGCloud's avatar
    OZGCloud committed
                                initEaKeycloak()
    
    OZGCloud's avatar
    OZGCloud committed
                                initMainKeycloak()
    
    OZGCloud's avatar
    OZGCloud committed
                    stage('Run E2E-Tests') {
                        failFast false
    
    OZGCloud's avatar
    OZGCloud committed
                        parallel {
                            stage('E2E-EA') {
                                steps {
    
    OZGCloud's avatar
    OZGCloud committed
                                    catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
                                        script {
                                            def stageName = env.STAGE_NAME
                                            def bezeichner = env.EA_BEZEICHNER
                                            def dbPort = 27018
    
    OZGCloud's avatar
    OZGCloud committed
                                            exposeDatenbank(generateNamespace(bezeichner), dbPort)
                                            exposeElasticSearch()
    
    OZGCloud's avatar
    OZGCloud committed
                                            def testResult = runTests(bezeichner, 'einheitlicher-ansprechpartner', dbPort)
    
    OZGCloud's avatar
    OZGCloud committed
                                            if(!testResult) {
                                                E2E_FAILED += "${stageName}, "
                                                error("Fehler in Stage ${stageName}")
                                            }
    
    OZGCloud's avatar
    OZGCloud committed
                                }
    
    OZGCloud's avatar
    OZGCloud committed
                                post {
                                    always {
                                        script {
                                            publishE2ETestResult("einheitlicher-ansprechpartner", "Goofy E2E-Tests EA")
                                        }
                                    }
    
    OZGCloud's avatar
    OZGCloud committed
                            stage('E2E-main') {
                                steps {
    
    OZGCloud's avatar
    OZGCloud committed
                                    catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
                                        script {
                                            def stageName = env.STAGE_NAME
                                            def bezeichner = env.MAIN_BEZEICHNER
                                            def dbPort = 27019
    
    OZGCloud's avatar
    OZGCloud committed
                                            exposeDatenbank(generateNamespace(bezeichner), dbPort)
                                            exposeElasticSearch()
    
    OZGCloud's avatar
    OZGCloud committed
                                            def testResult = runTests(bezeichner, 'main-tests', dbPort)
    
    OZGCloud's avatar
    OZGCloud committed
                                            if(!testResult) {
                                                E2E_FAILED += "${stageName}, "
                                                error("Fehler in Stage ${stageName}")
                                            }
    
    OZGCloud's avatar
    OZGCloud committed
                                }
    
    OZGCloud's avatar
    OZGCloud committed
                                post {
                                    always {
                                        script {
                                            publishE2ETestResult("main-tests", "Goofy E2E-Tests main")
                                        }
                                    }
    
                    stage('Delete E2E Namespaces') {
                        steps {
                            script {
                                FAILED_STAGE = env.STAGE_NAME
                                E2E_FAILED = ""
    
                                deleteKopStack([env.EA_BEZEICHNER, env.MAIN_BEZEICHNER])
                            }
                        }
                    }
    
                }
    			post {
            		always {
    					script {
    						if (E2E_FAILED) {
    							FAILED_STAGE = "E2E (${E2E_FAILED.substring(0, E2E_FAILED.length() - 2)})"
                                error("Fehler in E2E-Tests")
    						}
    					}
                	}
    			}
            }
    
            stage('Trigger Test rollout') {
    
                when {
                    branch 'release'
                }
    
                steps {
                    script {
                        FAILED_STAGE = env.STAGE_NAME
    
    
    OZGCloud's avatar
    OZGCloud committed
                        cloneRolloutGitopsRepo()
    
    OZGCloud's avatar
    OZGCloud committed
                        setNewTestGoofyVersion()
                        pushRolloutGitopsRepo()
    
    OZGCloud's avatar
    OZGCloud committed
        }
        post {
            failure {
                script {
    
    OZGCloud's avatar
    OZGCloud committed
                    if (isMasterBranch() || isReleaseBranch()) {
    
                        sendFailureMessage()
    
    OZGCloud's avatar
    OZGCloud committed
        }
    }
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    Void deployHelmChart(String helmChartVersion) {
    
        withCredentials([usernamePassword(credentialsId: 'jenkins-nexus-login', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]){
    
    OZGCloud's avatar
    OZGCloud committed
            if (isReleaseBranch()) {
    
    OZGCloud's avatar
    OZGCloud committed
                result = sh script: '''curl -u $USERNAME:$PASSWORD https://nexus.ozg-sh.de/service/rest/v1/components?repository=ozg-base-apps -F file=@goofy-'''+helmChartVersion+'''.tgz''', returnStdout: true
    
    OZGCloud's avatar
    OZGCloud committed
                result = sh script: '''curl -u $USERNAME:$PASSWORD https://nexus.ozg-sh.de/service/rest/v1/components?repository=ozg-base-apps-snapshot -F file=@goofy-'''+helmChartVersion+'''.tgz''', returnStdout: true
    
    OZGCloud's avatar
    OZGCloud committed
    String generateHelmChartVersion() {
        def chartVersion = "${VERSION}"
    
    OZGCloud's avatar
    OZGCloud committed
        if (isMasterBranch()) {
    
    OZGCloud's avatar
    OZGCloud committed
            chartVersion += "-${env.GIT_COMMIT.take(7)}"
        }
        else if (env.BRANCH_NAME != 'release') {
            chartVersion += "-${env.BRANCH_NAME}"
        }
    
        return chartVersion.replaceAll("_", "-")
    
    Void tagAndPushDockerImage(String newTag){
    
    OZGCloud's avatar
    OZGCloud committed
        withCredentials([usernamePassword(credentialsId: 'jenkins-nexus-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
    
    OZGCloud's avatar
    OZGCloud committed
            sh 'docker login docker.ozg-sh.de -u ${USER} -p ${PASSWORD}'
    
    OZGCloud's avatar
    OZGCloud committed
            sh "docker tag docker.ozg-sh.de/goofy:${IMAGE_TAG} docker.ozg-sh.de/goofy:${newTag}"
            sh "docker push docker.ozg-sh.de/goofy:${newTag}"
    
        }
    }
    
    String generateImageTag() {
        def imageTag = "${env.BRANCH_NAME}-${VERSION}"
    
    
    OZGCloud's avatar
    OZGCloud committed
        if (isMasterBranch()) {
    
            imageTag += "-${env.GIT_COMMIT.take(7)}"
        }
    
        return imageTag
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void cloneE2eGitopsRepo() {
        cloneGitopsRepo("e2e")
        checkoutE2eBranch("e2e")
    }
    
    Void cloneRolloutGitopsRepo() {
        cloneGitopsRepo("rollout")
    }
    
    
    Void cloneGitopsRepo(String directory) {
        withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
            dir(directory) {
                sh 'git clone https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/gitops.git'
            }
    
    OZGCloud's avatar
    OZGCloud committed
        }
    
        configureGit("${directory}/gitops")
    
    OZGCloud's avatar
    OZGCloud committed
    Void cloneProvisioningRepo() {
    
        withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
    
    OZGCloud's avatar
    OZGCloud committed
            dir("e2e") {
    
    OZGCloud's avatar
    OZGCloud committed
                sh 'git clone https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/provisioning.git'
    
    OZGCloud's avatar
    OZGCloud committed
            }
    
    OZGCloud's avatar
    OZGCloud committed
    Void pushE2eGitopsRepo() {
        pushGitopsRepo("e2e")
    }
    
    Void pushRolloutGitopsRepo() {
        pushGitopsRepo("rollout")
    }
    
    
    Void pushGitopsRepo(String directory) {
        withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
            dir("${directory}/gitops") {
    
    OZGCloud's avatar
    OZGCloud committed
                if (hasUnpushedCommits()) {
    
    OZGCloud's avatar
    OZGCloud committed
                    sh 'git push https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/gitops.git'
                }
    
    OZGCloud's avatar
    OZGCloud committed
    Boolean hasUnpushedCommits() {
    
    OZGCloud's avatar
    OZGCloud committed
        return sh (script: "git cherry -v | grep .", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer
    
    Void configureGit(String directory) {
    
    OZGCloud's avatar
    OZGCloud committed
        final email = "jenkins@ozg-sh.de"
        final name = "jenkins"
    
    
        dir(directory) {
    
    OZGCloud's avatar
    OZGCloud committed
            sh "git config user.email '${email}'"
            sh "git config user.name '${name}'"
    
    Void checkoutE2eBranch(String directory) {
        dir("${directory}/gitops") {
            sh 'git checkout e2e'
    
    OZGCloud's avatar
    OZGCloud committed
    Void generateEaNamespaceYaml() {
        generateNamespaceYaml(env.EA_BEZEICHNER, "goofy-client/apps/goofy-e2e/src/fixtures/argocd/by-ea-dev.yaml")
    }
    
    Void generateMainNamespaceYaml() {
        generateNamespaceYaml(env.MAIN_BEZEICHNER, "goofy-client/apps/goofy-e2e/src/fixtures/argocd/by-main-dev.yaml")
    }
    
    Void generateNamespaceYaml(String bezeichner, String valuesPath) {
        def envValues = readYaml file: valuesPath
    
        envValues.kop.bezeichner = bezeichner
    
        envValues.goofy.put("image", ['tag': IMAGE_TAG])
        envValues.goofy.put("helm", ['version': HELM_CHART_VERSION])
    
    OZGCloud's avatar
    OZGCloud committed
        if (isReleaseBranch()) {
    
            envValues.put("argocd", ['source': ['path': 'test/application']])
    
    OZGCloud's avatar
    OZGCloud committed
        }
    
        writeYaml file: "e2e/gitops/dev/namespace/namespaces/by-${bezeichner}-dev.yaml", data: envValues, overwrite: true
    
        sh "cat e2e/gitops/dev/namespace/namespaces/by-${bezeichner}-dev.yaml"
    
        dir("e2e/gitops") {
            sh "git add dev/namespace/namespaces/by-${bezeichner}-dev.yaml"
            sh "git commit -m 'add e2e by-${bezeichner}-dev'"
        }
    
    OZGCloud's avatar
    OZGCloud committed
    Void initEaKeycloak() {
        addKeycloakGroups(env.EA_BEZEICHNER)
        addKeycloakUser(env.EA_BEZEICHNER)
    }
    
    OZGCloud's avatar
    OZGCloud committed
    Void initMainKeycloak() {
        addKeycloakGroups(env.MAIN_BEZEICHNER)
        addKeycloakUser(env.MAIN_BEZEICHNER)
    
    OZGCloud's avatar
    OZGCloud committed
    Void addKeycloakGroups(String bezeichner) {
    
    OZGCloud's avatar
    OZGCloud committed
        def groupFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/group', returnStdout: true)
    
    OZGCloud's avatar
    OZGCloud committed
        groupFiles.split("\\n").each { group ->
            def groupJson = sh (script: "cat goofy-client/apps/goofy-e2e/src/fixtures/group/${group}", returnStdout: true)
    
    OZGCloud's avatar
    OZGCloud committed
            def ansibleVars = """{"k8s_context":"ozg-dev", \
                "kop_env":"dev", \
    
    OZGCloud's avatar
    OZGCloud committed
                "keycloak_realm":"${generateNamespace(bezeichner)}", \
    
    OZGCloud's avatar
    OZGCloud committed
                "group":${groupJson}
    
    OZGCloud's avatar
    OZGCloud committed
            dir("e2e/provisioning") {
                if (isReleaseBranch()) {
    
    OZGCloud's avatar
    OZGCloud committed
                    sh "ansible-playbook playbook/add-keycloak-group.yml --extra-vars '${ansibleVars}'"
    
    OZGCloud's avatar
    OZGCloud committed
                }
    
    OZGCloud's avatar
    OZGCloud committed
                else {
                    sh "ansible-playbook playbooks/add-keycloak-group.yml --extra-vars '${ansibleVars}'"
                }
            }
    	}
    
    OZGCloud's avatar
    OZGCloud committed
    Void addKeycloakUser(String bezeichner) {
    
    OZGCloud's avatar
    OZGCloud committed
        def userFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/user', returnStdout: true)
    
    OZGCloud's avatar
    OZGCloud committed
        userFiles.split("\\n").each { user ->
            def userJson = sh (script: "cat goofy-client/apps/goofy-e2e/src/fixtures/user/${user}", returnStdout: true)
    
    OZGCloud's avatar
    OZGCloud committed
            def ansibleVars = """{"k8s_context":"ozg-dev", \
                "kop_env":"dev", \
    
    OZGCloud's avatar
    OZGCloud committed
                "keycloak_realm":"${generateNamespace(bezeichner)}", \
    
    OZGCloud's avatar
    OZGCloud committed
                "user":${userJson}
            }"""
    
    OZGCloud's avatar
    OZGCloud committed
            dir("e2e/provisioning") {
                if (isReleaseBranch()) {
    
    OZGCloud's avatar
    OZGCloud committed
                    sh "ansible-playbook playbook/add-keycloak-user.yml --extra-vars '${ansibleVars}'"
                }
                else {
                    sh "ansible-playbook playbooks/add-keycloak-user.yml --extra-vars '${ansibleVars}'"
    
    Void deleteKopStack(kopBezeichner) {
    
        for(bezeichner in kopBezeichner) {
            dir("e2e/gitops/dev/namespace/namespaces") {
                if (sh (script: "ls | grep 'by-${bezeichner}-dev.yaml'", returnStatus: true) == 1) {
                    return
                }
    
                sh "rm by-${bezeichner}-dev.yaml"
                sh "git add by-${bezeichner}-dev.yaml"
                sh "git commit -m 'delete e2e by-${bezeichner}-dev.yaml'"
    
        }
    
        pushGitopsRepo("e2e")
    
        for(bezeichner in kopBezeichner) {
            waitForDeletion(bezeichner)
        }
    }
    
    Void waitForDeletion(String bezeichner) {
        try {
            sh "kubectl wait --for=delete applications/by-${bezeichner}-dev-application -n argocd --timeout=300s"
        } catch (Exception e) {
            error("Application by-${bezeichner}-dev-application konnte nicht gelöscht werden")
        }
    }
    
    
    Void rolloutKopStack(kopBezeichner) {
    
        pushGitopsRepo("e2e")
    
        for(bezeichner in kopBezeichner) {
    
            waitForRollout(bezeichner)
    
        }
    }
    
    Void waitForRollout(String bezeichner) {
        waitForHealthyApplication(bezeichner, 'application')
        waitForHealthyApplication(bezeichner, 'user-manager')
        waitForHealthyApplication(bezeichner, 'pluto')
        waitForHealthyApplication(bezeichner, 'goofy')
    }
    
    
    Void waitForHealthyApplication(String bezeichner, String application) {
    
        try {
            def countRetry = 0
            def maxRetry = 12
    
    
    OZGCloud's avatar
    OZGCloud committed
            while (!isApplicationPresent(bezeichner, application) && countRetry < maxRetry ) {
    
                countRetry++
                sh "sleep 5"
            }
    
    
    OZGCloud's avatar
    OZGCloud committed
            if (!isApplicationHealthy(bezeichner, application)) {
                waitForHealthyStatus(bezeichner, application)
    
    OZGCloud's avatar
    OZGCloud committed
            }
    
        } catch (Exception e) {
            error("Application ${application} unhealthy")
    
    OZGCloud's avatar
    OZGCloud committed
    Boolean isApplicationPresent(String bezeichner, String application) {
    
    OZGCloud's avatar
    OZGCloud committed
        return sh (script: "kubectl get applications -n argocd | grep 'by-${bezeichner}-dev-${application}'", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    Boolean isApplicationHealthy(String bezeichner, String application) {
    
    OZGCloud's avatar
    OZGCloud committed
        return sh (script: "kubectl get application/by-${bezeichner}-dev-${application} -n argocd -o=jsonpath='{.status.health.status}' | grep Healthy", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    Void waitForHealthyStatus(String bezeichner, String application) {
        sh "kubectl wait --for=jsonpath='{.status.health.status}'=Healthy applications/by-${bezeichner}-dev-${application} -n argocd --timeout=300s"
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void publishE2ETestResult(String reportFolder, String reportName) {
        publishHTML (
            target: [
                allowMissing: false,
                alwaysLinkToLastBuild: false,
                keepAll: true,
                reportDir: "goofy-client/apps/goofy-e2e/reports/${reportFolder}",
                reportFiles: 'report.html',
                reportName: reportName
            ]
        )
    }
    
    
    String runTests(String bezeichner, String reportFolder, Integer dbPort) {
        def configFile = generateCypressConfig(bezeichner, reportFolder, dbPort)
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
        try {
            dir("goofy-client") {
          	    sh "npm run cypress:version"
                sh "npm run cypress:ci-run --CONFIG_FILE=${configFile} --REPORT_FOLDER=${reportFolder}"
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
                return true
    
    OZGCloud's avatar
    OZGCloud committed
            }
    
    OZGCloud's avatar
    OZGCloud committed
        } catch (Exception e) {
    
    OZGCloud's avatar
    OZGCloud committed
            if (hasNpmDebugLog()) {
                sh "cat /root/.npm/_logs/*-debug.log"
            }
    
    OZGCloud's avatar
    OZGCloud committed
            else {
                echo "Npm debug log not found"
            }
    
    OZGCloud's avatar
    OZGCloud committed
        
    
    OZGCloud's avatar
    OZGCloud committed
            return false
    
    OZGCloud's avatar
    OZGCloud committed
        }
    }
    
    String makeUrlConform(String input) {
        return input.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase()
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    String generateBezeichner(String stage) {
    
    OZGCloud's avatar
    OZGCloud committed
        def branchName = makeUrlConform(env.BRANCH_NAME)
        def stageName = makeUrlConform(stage)
    
    OZGCloud's avatar
    OZGCloud committed
        return "${cutBranchNameForKeycloakRealm(branchName, stageName)}${stageName}"
    }
    
    String cutBranchNameForKeycloakRealm(String branchName, String stageName) {
        def maxKeycloakRealmLength = 30
        def postPrefixLength = 8
        def cutBranchNamePosition = maxKeycloakRealmLength - (branchName.length() + stageName.length() + postPrefixLength)
    
    OZGCloud's avatar
    OZGCloud committed
    
        if(cutBranchNamePosition < 0) {
            branchName = branchName[0..cutBranchNamePosition]
        }
    
    
    OZGCloud's avatar
    OZGCloud committed
        return branchName
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    
    String generateCypressConfig(String bezeichner, String testFolder, Integer dbPort) {
    
    OZGCloud's avatar
    OZGCloud committed
        def namespace = generateNamespace(bezeichner)
    
    OZGCloud's avatar
    OZGCloud committed
        def configName = "cypress-ci-"+testFolder+".json"
    
    OZGCloud's avatar
    OZGCloud committed
        dir('goofy-client/apps/goofy-e2e/'){
            def config = readJSON file: 'cypress-ci.json'
    
    OZGCloud's avatar
    OZGCloud committed
            def elasticsearchEnv = getElasticsearchEnv(namespace)
    
    OZGCloud's avatar
    OZGCloud committed
    
    
            config.baseUrl = "https://${bezeichner}.${env.CLUSTER_BASE_URL}" as String
    
    OZGCloud's avatar
    OZGCloud committed
            config.env.dbUrl = "mongodb://pluto-database-user:XnHhfznNWg65NNd@localhost:${dbPort}/admin?ssl=false&directConnection=true" as String
    
    OZGCloud's avatar
    OZGCloud committed
            config.env.keycloakUrl = "https://${env.SSO_URL}/" as String
    
    OZGCloud's avatar
    OZGCloud committed
            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
    
    
    OZGCloud's avatar
    OZGCloud committed
            environment = config.env
    
            environment.put("search", elasticsearchEnv)
    
            environment.put("userManager", getUserManagerEnv(dbPort));
    
    OZGCloud's avatar
    OZGCloud committed
    
    
    OZGCloud's avatar
    OZGCloud committed
            writeJSON file: configName, json: config
    
    OZGCloud's avatar
    OZGCloud committed
    
            sh "cat ${configName}"
    
    OZGCloud's avatar
    OZGCloud committed
        }
    
    OZGCloud's avatar
    OZGCloud committed
    
        return configName
    }
    
    
    String getUserManagerEnv(dbPort){
    
    	return readJSON(text: """{
    
            "dbUrl":"mongodb://user-manager-database-user:5M3N2sVEq5c8@localhost:${dbPort}/admin?ssl=false&directConnection=true", \
    
            "database":"user-manager-database"}""");
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    String getKeycloakUuid(realm, userName) {
    
    OZGCloud's avatar
    OZGCloud committed
        def shScript = """curl -H 'Content-Type: application/json' \
                            -H 'Authorization: bearer ${getKeycloakAccessToken()}' \
    
    OZGCloud's avatar
    OZGCloud committed
                            'https://${env.SSO_URL}/admin/realms/${realm}/users'
    
    OZGCloud's avatar
    OZGCloud committed
                        """
    
    	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-dev-cluster', 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_URL/realms/master/protocol/openid-connect/token', returnStdout: true)
    
    OZGCloud's avatar
    OZGCloud committed
    
    		return token.access_token
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    Void sendFailureMessage() {
        def room = ''
        def data = """{"msgtype":"m.text", \
                        "body":"Goofy: Build Failed. Stage: ${FAILED_STAGE} Build-ID: ${env.BUILD_NUMBER} Link: ${BLUE_OCEAN_URL}", \
                        "format": "org.matrix.custom.html", \
                        "formatted_body":"Goofy: Build Failed. Stage: ${FAILED_STAGE} Build-ID: <a href='${BLUE_OCEAN_URL}'>${env.BUILD_NUMBER}</a>"}"""
    
    OZGCloud's avatar
    OZGCloud committed
        if (isMasterBranch()) {
    
            room = "!iQPAvQIiRwRpNOszjw:matrix.ozg-sh.de"
        }
    
    OZGCloud's avatar
    OZGCloud committed
        else if (isReleaseBranch()) {
    
            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
        }
    
    OZGCloud's avatar
    OZGCloud committed
    String getElasticsearchEnv(String namespace) {
        def elasticsearchSecret = getElasticsearchSecret(namespace)
    
    
    OZGCloud's avatar
    OZGCloud committed
        def env = """{
    
    OZGCloud's avatar
    OZGCloud committed
            "user":"${decodeString(elasticsearchSecret.username)}", \
            "password":"${decodeString(elasticsearchSecret.password)}", \
            "index":"${decodeString(elasticsearchSecret.index)}", \
    
            "url":"https://localhost:9200"}"""
    
    OZGCloud's avatar
    OZGCloud committed
        return readJSON ( text: env)
    }
    
    
    OZGCloud's avatar
    OZGCloud committed
    Void setNewDevGoofyVersion() {
        setNewGoofyGitopsVersion("rollout", "dev")
    }
    
    Void setNewTestGoofyVersion() {
        setNewGoofyGitopsVersion("rollout", "test")
    }
    
    
    Void setNewGoofyGitopsVersion(String directory, String environment) {
        dir("${directory}/gitops") {
            def envFile = "${environment}/application/values/goofy-values.yaml"
    
    OZGCloud's avatar
    OZGCloud committed
            def envVersions = readYaml file: envFile
    
    
            envVersions.goofy.image.tag = IMAGE_TAG
            envVersions.goofy.helm.version = HELM_CHART_VERSION
    
    OZGCloud's avatar
    OZGCloud committed
    
            writeYaml file: envFile, data: envVersions, overwrite: true
    
    
    OZGCloud's avatar
    OZGCloud committed
            if (hasGoofyValuesFileChanged(environment)) {
                sh "git add ${envFile}"
                sh "git commit -m 'jenkins rollout ${environment} goofy version ${IMAGE_TAG}'"
            }
    
    OZGCloud's avatar
    OZGCloud committed
        }
    
    OZGCloud's avatar
    OZGCloud committed
    Boolean hasGoofyValuesFileChanged(String environment) {
    
    OZGCloud's avatar
    OZGCloud committed
        return sh (script: "git status | grep '${environment}/application/values/goofy-values.yaml'", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer
    
    OZGCloud's avatar
    OZGCloud committed
    Void exposeElasticSearch() {
        portStatus = sh (script: "lsof -i -P -n | grep LISTEN | grep :9200", returnStatus: true) as Integer
        if(portStatus == 1) {
            sh "kubectl port-forward ozg-search-cluster-es-ozg-search-0 9200:9200 -n elastic-system &"
        }
    }
    
    Void exposeDatenbank(String namespace, port) {
        sh "kubectl port-forward pluto-database-0 ${port}:27017 -n ${namespace} &"
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    String generateNamespace(String bezeichner) {
        return "${env.BUNDESLAND}-${bezeichner}-dev"
    }
    
    Boolean isReleaseBranch() {
        return env.BRANCH_NAME == 'release'
    }
    
    Boolean isMasterBranch() {
        return env.BRANCH_NAME == 'master'
    }
    
    String decodeString(String encoded) {
        return sh (script: "echo -n ${encoded} | base64 --decode", returnStdout: true)
    }
    
    Map getElasticsearchSecret(String namespace) {
        return readJSON ( text: sh (script: "kubectl get secret elasticsearch-credentials -n ${namespace} -o jsonpath={.data}", returnStdout: true))
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    Boolean hasNpmDebugLog() {
        return sh (script: "ls -l /root/.npm/_logs/*-debug.log", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer