diff --git a/Jenkinsfile b/Jenkinsfile index 8e141c262cf9edb269b31cc7836fdd5f3b0f7e2c..54218640434873d0d450b7db5ab0385f3e5aea11 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -9,14 +9,7 @@ pipeline { 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" - CLUSTER_BASE_URL = "dev.by.kop-cloud.de" FAILED_STAGE = "" - IMAGE_TAG = "" - VERSION = "" - E2E_FAILED = "" - HELM_CHART_VERSION = "" SH_SUCCESS_STATUS_CODE = 0 } @@ -31,26 +24,21 @@ pipeline { 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 + VERSION = getRootPomVersion() + def serverVersion = getParentPomVersion('goofy-server/pom.xml') + def clientVersion = getParentPomVersion('goofy-client/pom.xml') if(isReleaseBranch()){ - if ( !(VERSION ==~ RELEASE_REGEX) || !(serverVersion ==~ RELEASE_REGEX) || !(clientVersion ==~ RELEASE_REGEX)) { + if ( !isReleaseVersion([VERSION, serverVersion, clientVersion]) ) { error("Keine Release Version für Branch ${env.BRANCH_NAME}.") } } else { - if ( !(VERSION ==~ SNAPSHOT_REGEX) || !(serverVersion ==~ SNAPSHOT_REGEX) || !(clientVersion ==~ SNAPSHOT_REGEX)) { + if ( !isSnapshotVersion([VERSION, serverVersion, clientVersion]) ) { error("Keine Snapshot Version für Branch ${env.BRANCH_NAME}.") } } - if( !(VERSION == serverVersion && VERSION == clientVersion)){ + if( !isSameVersion([serverVersion, clientVersion], VERSION) ){ error("Versionen sind nicht identisch") } } @@ -192,163 +180,14 @@ pipeline { script { FAILED_STAGE = env.STAGE_NAME - cloneRolloutGitopsRepo() + cloneGitopsRepo() setNewDevGoofyVersion() - pushRolloutGitopsRepo() + pushGitopsRepo() } } } - stage('E2E') { - 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' - } - } - } - stage('Install cypress') { - steps { - script { - FAILED_STAGE = env.STAGE_NAME - E2E_FAILED = "" - - dir('goofy-client') { - sh "npm run cypress:install" - } - } - } - } - stage('Rollout E2E Namespaces') { - steps { - script { - FAILED_STAGE = env.STAGE_NAME - E2E_FAILED = "" - - cloneE2eGitopsRepo() - - env.EA_BEZEICHNER = generateBezeichner("e2e-ea") - env.MAIN_BEZEICHNER = generateBezeichner("e2e-main") - - deleteKopStack([env.EA_BEZEICHNER, env.MAIN_BEZEICHNER]) - - generateEaNamespaceYaml() - generateMainNamespaceYaml() - - pushE2eGitopsRepo() - - rolloutKopStack([env.EA_BEZEICHNER, env.MAIN_BEZEICHNER]) - } - } - } - stage('Add E2E User') { - steps { - script { - FAILED_STAGE = env.STAGE_NAME - E2E_FAILED = "" - - cloneProvisioningRepo() - - initEaKeycloak() - - initMainKeycloak() - } - } - } - stage('Run E2E-Tests') { - failFast false - - parallel { - stage('E2E-EA') { - steps { - catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { - script { - def stageName = env.STAGE_NAME - def bezeichner = env.EA_BEZEICHNER - def dbPort = 27018 - - exposeDatenbank(generateNamespace(bezeichner), dbPort) - exposeElasticSearch() - - def testResult = runTests(bezeichner, 'einheitlicher-ansprechpartner', dbPort) - - if(!testResult) { - E2E_FAILED += "${stageName}, " - error("Fehler in Stage ${stageName}") - } - } - } - } - post { - always { - script { - publishE2ETestResult("einheitlicher-ansprechpartner", "Goofy E2E-Tests EA") - } - } - } - } - stage('E2E-main') { - steps { - catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') { - script { - def stageName = env.STAGE_NAME - def bezeichner = env.MAIN_BEZEICHNER - def dbPort = 27019 - - exposeDatenbank(generateNamespace(bezeichner), dbPort) - exposeElasticSearch() - - def testResult = runTests(bezeichner, 'main-tests', dbPort) - - if(!testResult) { - E2E_FAILED += "${stageName}, " - error("Fehler in Stage ${stageName}") - } - } - } - } - 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' @@ -358,10 +197,10 @@ pipeline { script { FAILED_STAGE = env.STAGE_NAME - cloneRolloutGitopsRepo() + cloneGitopsRepo() setNewTestGoofyVersion() - pushRolloutGitopsRepo() + pushGitopsRepo() } } } @@ -424,43 +263,16 @@ String generateImageTag() { return imageTag } -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' - } - } - configureGit("${directory}/gitops") -} - -Void cloneProvisioningRepo() { +Void cloneGitopsRepo() { withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) { - dir("e2e") { - sh 'git clone https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/provisioning.git' - } + sh 'git clone https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/gitops.git' } + configureGit() } -Void pushE2eGitopsRepo() { - pushGitopsRepo("e2e") -} - -Void pushRolloutGitopsRepo() { - pushGitopsRepo("rollout") -} - -Void pushGitopsRepo(String directory) { +Void pushGitopsRepo() { withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) { - dir("${directory}/gitops") { + dir("gitops") { if (hasUnpushedCommits()) { sh 'git push https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/gitops.git' } @@ -472,299 +284,16 @@ Boolean hasUnpushedCommits() { return sh (script: "git cherry -v | grep .", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer } -Void configureGit(String directory) { +Void configureGit() { final email = "jenkins@ozg-sh.de" final name = "jenkins" - dir(directory) { + dir("gitops") { sh "git config user.email '${email}'" sh "git config user.name '${name}'" } } -Void checkoutE2eBranch(String directory) { - dir("${directory}/gitops") { - sh 'git checkout e2e' - } -} - -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]) - - if (isReleaseBranch()) { - envValues.put("argocd", ['source': ['path': 'test/application']]) - } - - 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'" - } -} - -Void initEaKeycloak() { - addKeycloakGroups(env.EA_BEZEICHNER) - addKeycloakUser(env.EA_BEZEICHNER) -} - -Void initMainKeycloak() { - addKeycloakGroups(env.MAIN_BEZEICHNER) - addKeycloakUser(env.MAIN_BEZEICHNER) -} - -Void addKeycloakGroups(String bezeichner) { - def groupFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/group', returnStdout: true) - - groupFiles.split("\\n").each { group -> - def groupJson = sh (script: "cat goofy-client/apps/goofy-e2e/src/fixtures/group/${group}", returnStdout: true) - - def ansibleVars = """{"k8s_context":"ozg-dev", \ - "kop_env":"dev", \ - "keycloak_realm":"${generateNamespace(bezeichner)}", \ - "group":${groupJson} - }""" - - dir("e2e/provisioning") { - if (isReleaseBranch()) { - sh "ansible-playbook playbook/add-keycloak-group.yml --extra-vars '${ansibleVars}'" - } - else { - sh "ansible-playbook playbooks/add-keycloak-group.yml --extra-vars '${ansibleVars}'" - } - } - } -} - -Void addKeycloakUser(String bezeichner) { - def userFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/user', returnStdout: true) - - userFiles.split("\\n").each { user -> - def userJson = sh (script: "cat goofy-client/apps/goofy-e2e/src/fixtures/user/${user}", returnStdout: true) - - def ansibleVars = """{"k8s_context":"ozg-dev", \ - "kop_env":"dev", \ - "keycloak_realm":"${generateNamespace(bezeichner)}", \ - "user":${userJson} - }""" - - dir("e2e/provisioning") { - if (isReleaseBranch()) { - 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 - - while (!isApplicationPresent(bezeichner, application) && countRetry < maxRetry ) { - countRetry++ - sh "sleep 5" - } - - if (!isApplicationHealthy(bezeichner, application)) { - waitForHealthyStatus(bezeichner, application) - } - } catch (Exception e) { - error("Application ${application} unhealthy") - } -} - -Boolean isApplicationPresent(String bezeichner, String application) { - return sh (script: "kubectl get applications -n argocd | grep 'by-${bezeichner}-dev-${application}'", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer -} - -Boolean isApplicationHealthy(String bezeichner, String application) { - 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 -} - -Void waitForHealthyStatus(String bezeichner, String application) { - sh "kubectl wait --for=jsonpath='{.status.health.status}'=Healthy applications/by-${bezeichner}-dev-${application} -n argocd --timeout=300s" -} - -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) - - try { - dir("goofy-client") { - sh "npm run cypress:version" - sh "npm run cypress:ci-run --CONFIG_FILE=${configFile} --REPORT_FOLDER=${reportFolder}" - - return true - } - } catch (Exception e) { - if (hasNpmDebugLog()) { - sh "cat /root/.npm/_logs/*-debug.log" - } - else { - echo "Npm debug log not found" - } - - return false - } -} - -String makeUrlConform(String input) { - return input.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase() -} - -String generateBezeichner(String stage) { - def branchName = makeUrlConform(env.BRANCH_NAME) - def stageName = makeUrlConform(stage) - - 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) - - if(cutBranchNamePosition < 0) { - branchName = branchName[0..cutBranchNamePosition] - } - - return branchName -} - -String generateCypressConfig(String bezeichner, String testFolder, Integer dbPort) { - def namespace = generateNamespace(bezeichner) - def configName = "cypress-ci-"+testFolder+".json" - - dir('goofy-client/apps/goofy-e2e/'){ - def config = readJSON file: 'cypress-ci.json' - def elasticsearchEnv = getElasticsearchEnv(namespace) - - config.baseUrl = "https://${bezeichner}.${env.CLUSTER_BASE_URL}" as String - config.env.dbUrl = "mongodb://pluto-database-user:XnHhfznNWg65NNd@localhost:${dbPort}/admin?ssl=false&directConnection=true" as String - config.env.keycloakUrl = "https://${env.SSO_URL}/" 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 - - environment = config.env - environment.put("search", elasticsearchEnv) - - environment.put("userManager", getUserManagerEnv(dbPort)); - - writeJSON file: configName, json: config - - sh "cat ${configName}" - } - - 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"}"""); -} - -String getKeycloakUuid(realm, userName) { - def shScript = """curl -H 'Content-Type: application/json' \ - -H 'Authorization: bearer ${getKeycloakAccessToken()}' \ - 'https://${env.SSO_URL}/admin/realms/${realm}/users' - """ - - def users = readJSON text: sh(script: shScript, returnStdout: true) - - for(user in users) { - if (user.username == userName) { - return user.id - } - } -} - -String getKeycloakAccessToken() { - withCredentials([usernamePassword(credentialsId: 'keycloak-dev-cluster', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) { - 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) - - return token.access_token - } -} - Void sendFailureMessage() { def room = '' def data = """{"msgtype":"m.text", \ @@ -788,28 +317,16 @@ String getElementAccessToken() { } } -String getElasticsearchEnv(String namespace) { - def elasticsearchSecret = getElasticsearchSecret(namespace) - - def env = """{ - "user":"${decodeString(elasticsearchSecret.username)}", \ - "password":"${decodeString(elasticsearchSecret.password)}", \ - "index":"${decodeString(elasticsearchSecret.index)}", \ - "url":"https://localhost:9200"}""" - - return readJSON ( text: env) -} - Void setNewDevGoofyVersion() { - setNewGoofyGitopsVersion("rollout", "dev") + setNewGoofyGitopsVersion("dev") } Void setNewTestGoofyVersion() { - setNewGoofyGitopsVersion("rollout", "test") + setNewGoofyGitopsVersion("test") } -Void setNewGoofyGitopsVersion(String directory, String environment) { - dir("${directory}/gitops") { +Void setNewGoofyGitopsVersion(String environment) { + dir("gitops") { def envFile = "${environment}/application/values/goofy-values.yaml" def envVersions = readYaml file: envFile @@ -830,37 +347,49 @@ Boolean hasGoofyValuesFileChanged(String environment) { return sh (script: "git status | grep '${environment}/application/values/goofy-values.yaml'", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer } -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 &" - } +Boolean isReleaseBranch() { + return env.BRANCH_NAME == 'release' } -Void exposeDatenbank(String namespace, port) { - sh "kubectl port-forward pluto-database-0 ${port}:27017 -n ${namespace} &" +Boolean isMasterBranch() { + return env.BRANCH_NAME == 'master' } -String generateNamespace(String bezeichner) { - return "${env.BUNDESLAND}-${bezeichner}-dev" +Boolean isReleaseVersion(List versions) { + return matchRegexVersion(versions, RELEASE_REGEX) } -Boolean isReleaseBranch() { - return env.BRANCH_NAME == 'release' +Boolean isSnapshotVersion(List versions) { + return matchRegexVersion(versions, SNAPSHOT_REGEX) } -Boolean isMasterBranch() { - return env.BRANCH_NAME == 'master' +Boolean matchRegexVersion(List versions, String regex) { + for (version in versions) { + println version + if ( !(version ==~ regex) ) { + return false + } + } + + return true } -String decodeString(String encoded) { - return sh (script: "echo -n ${encoded} | base64 --decode", returnStdout: true) +Boolean isSameVersion(List versions, String expectedVersion) { + for (version in versions) { + if ( version != expectedVersion ) { + return false + } + } + + return true } -Map getElasticsearchSecret(String namespace) { - return readJSON ( text: sh (script: "kubectl get secret elasticsearch-credentials -n ${namespace} -o jsonpath={.data}", returnStdout: true)) +String getRootPomVersion() { + def rootPom = readMavenPom file: 'pom.xml' + return rootPom.version } -Boolean hasNpmDebugLog() { - return sh (script: "ls -l /root/.npm/_logs/*-debug.log", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer +String getParentPomVersion(String filePath) { + def pom = readMavenPom file: filePath + return pom.parent.version } \ No newline at end of file