diff --git a/Jenkinsfile b/Jenkinsfile index 4ab8e499fa2398ba259c2367e39440597822d448..a8c40fd3070b667b608e8002afb04a5347520838 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,4 +1,6 @@ def FAILED_STAGE +def E2E_FAILED +def IMAGE_TAG pipeline { agent { @@ -20,364 +22,306 @@ pipeline { } stages { -// stage('Check Version') { -// steps { -// script { -// FAILED_STAGE = env.STAGE_NAME -// def rootPom = readMavenPom file: 'pom.xml' -// def rootVersion = 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 - -// 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}.") -// } -// } - -// if( !(rootVersion == serverVersion && rootVersion == clientVersion)){ -// error("Versionen sind nicht identisch") -// } -// } -// } -// } -// stage('Client') { -// steps { -// container("nodejs"){ -// script { -// FAILED_STAGE=env.STAGE_NAME - -// sh 'npm --version' -// dir('goofy-client') { -// sh 'echo "registry=http://nexus.ozg-sh.de/repository/npm-proxy" >> ~/.npmrc' -// sh 'echo "_auth=amVua2luczpQaihzX0ZNNFU5ZC8=" >> ~/.npmrc' - -// sh 'npm install --no-optional' - -// if (env.BRANCH_NAME == 'release') { -// sh 'npm run ci-prodBuild' -// } -// else { -// sh 'npm run ci-build' -// } - -// sh 'npm run ci-test' - -// 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 -// // } -// // } -// } - -// 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' -// } - -// 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 -// } -// } -// } - -// stage('Push Docker image') { -// steps { -// script { -// FAILED_STAGE=env.STAGE_NAME -// def pom = readMavenPom file: 'pom.xml' -// def version = pom.version - -// container("docker") { -// withCredentials([usernamePassword(credentialsId: 'jenkins-docker-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) { -// sh 'docker login docker.ozg-sh.de -u ${USER} -p ${PASSWORD}' - -// 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}" - -// 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' -// } -// } -// } -// } -// } -// } - - stage('E2E-EA-kc') { - when { - branch 'ozg-1784' - } - + stage('Init k8s') { steps { script { - FAILED_STAGE=env.STAGE_NAME + 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' + } - namespace = generateNamespaceName(env.STAGE_NAME) - createKeycloakUser(env.STAGE_NAME) + sh 'helm version' + } + container('nodejs') { + dir('goofy-client') { + sh 'npm install --no-optional' + } + } + } + } + stage('E2E') { + failFast false - container("k8s") { - println(namespace) + parallel { + stage('E2E-EA') { + when { + branch 'ozg-1784' + } + + steps { + script { + def stageName = env.STAGE_NAME + def namespace = generateNamespaceName(stageName) + + checkIfNamespaceExists(namespace) + + parallel( + startGoofy: { + startGoofy(namespace, stageName, 'ea-values.yaml') + }, + startPluto: { + startPluto(namespace, 'ea-values.yaml') + } + ) + + + if(runTests(stageName, 'einheitlicher-ansprechpartner') != '0') { + E2E_FAILED = true + } - configFileProvider([configFile(fileId: 'jenkins-kuby-kubeconfig', variable: 'KUBE_CONFIG')]) { - sh 'mkdir ~/.kube' - sh 'cp ${KUBE_CONFIG} ~/.kube/config' + shutdownEnvironment(namespace) } + } + 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' + } - sh 'helm version' - sh 'kubectl get nodes' - - def namespaceList = sh (script: 'kubectl get namespaces', returnStdout: true) - println(namespaceList) + steps { + script { + def stageName = env.STAGE_NAME + def namespace = generateNamespaceName(stageName) - if(namespaceList.contains(namespace)) { - error("Namespace: ${namespace} existiert bereits") - } + checkIfNamespaceExists(namespace) - 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' - } + parallel( + startGoofy: { + startGoofy(namespace, stageName, 'values.yaml') + }, + startPluto: { + startPluto(namespace, 'values.yaml') } - sh "kubectl rollout status statefulset/pluto-database -n ${namespace}" - } - ) - + ) - println('running') + if(runTests(stageName, 'main-tests') != '0') { + E2E_FAILED = true + } - println('testing') - - // sh "helm uninstall goofy --namespace ${namespace} --wait" - // sh "helm uninstall pluto --namespace ${namespace} --wait" - - // sh "kubectl delete namespace ${namespace}" + 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" + ]) + } } - - // 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 - // } - // } } } + post { + always { + script { + if (E2E_FAILED) { + currentBuild.result = 'FAILURE' + } + } + } + } } + } +} - // stage('E2E-Tests') { - // when { - // branch 'master' - // } - // steps { - // script { - // FAILED_STAGE=env.STAGE_NAME - // } - - // container("kubectl") { - // sh 'kubectl rollout restart deployment/goofy -n sh-e2e-dev' - // sh 'kubectl rollout status deployment/goofy -n sh-e2e-dev' - - // sh 'kubectl rollout restart deployment/pluto -n sh-e2e-dev' - // sh 'kubectl rollout status deployment/pluto -n sh-e2e-dev' - // } - - // 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" - // ]) - // } - // } - // } - - // 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' - // } - // } - // } - // } - - // stage('Deploy Goofy') { - // when { - // anyOf { - // branch 'master' - // branch 'release' - // } - // } - - // 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}' - - // 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' - // } - - // 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' - // } - // } - // } - // } - - // 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' - // } - - // 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' - // } - // } - // } - // } - // } +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}" + } +} + +Void startGoofy(String stageName, String namespace, String values) { + 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=${IMAGE_TAG} --namespace ${namespace}" + ' --repo https://nexus.ozg-sh.de/repository/ozg-base-apps --username ${USER} --password ${PASSWORD} --wait --wait-for-jobs' + } + } + + createKeycloakGroups(stageName) + generateKeycloakUserYaml(stageName) + applyKeycloakUser(stageName) } - // 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}>") - // } - // } - // } - // } +} + +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" + + removeKeycloakUser(stageName) + + sh "kubectl delete namespace ${namespace}" + } +} + +String makeUrlConform(String input) { + return input.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase() } String generateNamespaceName(String stage) { - branchName = env.BRANCH_NAME.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase() - stageName = stage.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase() + def branchName = makeUrlConform(env.BRANCH_NAME) + def stageName = makeUrlConform(stage) return "e2e-${branchName}${stageName}-dev" } -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" +Void generateKeycloakUserYaml(String stage) { + def namespace = generateNamespaceName(stage) + def e2eUserFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/user', returnStdout: true) e2eUserFiles.split("\\n").each { user -> - dir('goofy-client/apps/goofy-e2e/src/fixtures/user') { - userJson = readJSON file: user + 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 - baseUserYaml.metadata.name = namespace + "-" + userJson.name - baseUserYaml.metadata.labels.realm = namespace - baseUserYaml.spec.realmSelector.matchLabels.realm = namespace + 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]] - baseUserYaml.spec.user.username = userJson.name - baseUserYaml.spec.user.credentials = [[type: 'password', value: userJson.password]] + if(userJson.firstName) { + newUserYaml.spec.user.firstName = userJson.firstName + } - if(userJson.firstName) { - baseUserYaml.spec.user.firstName = userJson.firstName - } + if(userJson.lastName) { + newUserYaml.spec.user.lastName = userJson.lastName + } - if(userJson.lastName) { - baseUserYaml.spec.user.lastName = userJson.lastName - } + if(userJson.realmRoles) { + newUserYaml.spec.user.realmRoles += userJson.realmRoles + } - if(userJson.einheitlicherAnsprechpartner) { - baseUserYaml.spec.user.clientRoles = [namespace: ['EINHEITLICHER_ANSPRECHPARTNER']] - } + if(userJson.groups) { + newUserYaml.spec.user.groups = userJson.groups + } - if(userJson.verwaltungUser) { - baseUserYaml.spec.user.clientRoles = [namespace: ['VERWALTUNG_USER']] - } - } + dir (namespace) { + writeYaml file: userJson.name+".yaml", data: newUserYaml + } + } +} - writeYaml file: "kc-user.yaml", data: baseUserYaml +Void createKeycloakGroups(String realm) { + def groupFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/group', returnStdout: true) - sh "kubectl apply -f kc-user.yaml" - sh "rm kc-user.yaml" + groupFiles.split("\\n").each { group -> + def groupJson = readJSON file: 'goofy-client/apps/goofy-e2e/src/fixtures/group/'+group + + 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}' + """ + } +} + +Void applyKeycloakUser(String stage) { + dir(generateNamespaceName(stage)){ + def kcUserFiles = sh (script: "ls", returnStdout: true) + + kcUserFiles.split("\\n").each { user -> + sh "kubectl apply -f ${user}" + } + } +} + +Void removeKeycloakUser(String stage) { + dir(generateNamespaceName(stage)){ + 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 + config.env.sabineUuid = getKeycloakUuid(namespace, 'sabine') as String + 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 } + + return configName +} + +String getKeycloakUuid(realm, userName) { + def users = sh (script: """curl -X POST 'https://sso.dev.ozg-sh.de/auth/admin/realms/${realm}/users' \ + -H 'Content-Type: application/json' \ + -H 'Authorization: bearer ${getKeycloakAccessToken()}' + """, returnStdout: true) + + users.each { user -> + if (user.username == userName) { + return user.id + } + } +} + +String getKeycloakAccessToken() { + withCredentials([usernamePassword(credentialsId: 'keycloak-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) { + def token = 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 access_token = token.split('"')[3] + } } \ No newline at end of file