pipeline {
    agent {
        node {
            label 'jenkins-build-agent'
        }
    }

    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"
        CLUSTER_BASE_URL = "dev.by.kop-cloud.de"
        FAILED_STAGE = ""
        IMAGE_TAG = ""
        VERSION = ""
        E2E_FAILED = ""
        HELM_CHART_VERSION = ""
    }

    options {
        timeout(time: 1, unit: 'HOURS')
        disableConcurrentBuilds()
        buildDiscarder(logRotator(numToKeepStr: '5'))
    }

    stages {
        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

                    if(env.BRANCH_NAME == 'release'){
                        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}.")
                        }
                    }

                    if( !(VERSION == serverVersion && VERSION == clientVersion)){
                        error("Versionen sind nicht identisch")
                    }
                }
            }
        }
        stage('Client') {
            steps {
                script {
                    FAILED_STAGE=env.STAGE_NAME

                    sh 'npm --version'
                    dir('goofy-client') {
                        sh 'echo "registry=https://nexus.ozg-sh.de/repository/npm-proxy" >> ~/.npmrc'
                        sh 'echo "_auth=amVua2luczpQaihzX0ZNNFU5ZC8=" >> ~/.npmrc'

						sh 'npm cache verify'
                        sh 'npm install --legacy-peer-deps'

                        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
                    IMAGE_TAG = generateImageTag()

                    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 {
                            if (env.BRANCH_NAME == 'master') {
        	                    dir('goofy-server'){
                                    withSonarQubeEnv('sonarqube-ozg-sh'){
                                        sh 'mvn -s $MAVEN_SETTINGS sonar:sonar'
                                    }
                                }
	                        }
                        } catch (Exception e) {
                            unstable("SonarQube failed")
                        }
                    }
                }
            }
            post {
                always{
                    junit testResults: '**/target/surefire-reports/*.xml', skipPublishingChecks: true
                }
            }
        }
        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'
                    }

                    initHelmRepo()

                    sh 'helm version'
                }
            }
        }
        stage('Deploy Maven Artifacts to Nexus') {
            when {
                anyOf {
                    branch 'master'
                    branch 'release'
                }
            }
            steps {
                script {
                    FAILED_STAGE = env.STAGE_NAME
                }

                configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
                    sh 'mvn -s $MAVEN_SETTINGS -pl -goofy-client -DskipTests deploy'
                }
            }
        }
        stage('Tag and Push Docker Image') {
            when {
                anyOf {
                    branch 'master'
                    branch 'release'
                }
            }

            steps {
                script {
                    FAILED_STAGE = env.STAGE_NAME

                    if (env.BRANCH_NAME == 'master') {
                        tagAndPushDockerImage('snapshot-latest')
                    }
                    else if (env.BRANCH_NAME == 'release') {
                        tagAndPushDockerImage('latest')
                    }
                }
            }
        }
        
        stage('Test, build and deploy Helm Chart') {
            steps {
                script {
                    FAILED_GOOFY_STAGE=env.STAGE_NAME
                    HELM_CHART_VERSION = generateHelmChartVersion()

                    dir('src/main/helm') {
                        sh "helm lint -f test-values.yaml"

                        sh "helm unittest -f '../../test/helm/*.yaml' -v '../../test/unit-values.yaml' ."

                        sh "helm package --version=${HELM_CHART_VERSION} ."

                        deployHelmChart(HELM_CHART_VERSION)
                    }
                }
            }
        }

        stage('Trigger Dev rollout') {
            when {
                branch 'master'
            }
            steps {
                script {
               	 	if(currentBuild.changeSets.size() > 0) {
						FAILED_STAGE = env.STAGE_NAME
	
	                    checkoutProvisioningRepo()
	
	                    setNewGoofyProvisioningVersion('dev')
	
	                    pushNewProvisioningVersion('dev')
					}
					else {
					     sh 'echo "no code changes found"'
					}
                }
            }
        }
        stage('E2E') {
            stages {

                stage('Install cypress') {
                    steps {
                        script {
                            FAILED_STAGE = env.STAGE_NAME
                            dir('goofy-client') {
                                sh "npm run cypress:install"
                            }
                        }
                    }
                }
                stage('Run E2E-Tests') {
                    failFast false

                    parallel {
                        stage('E2E-EA') {
                            steps {
                                script {
                                    def stageName = env.STAGE_NAME
                                    def bezeichner = generateBezeichner(stageName)
                                    def dbPort = 27018

                                    startEnvironment(bezeichner, stageName, IMAGE_TAG, true, HELM_CHART_VERSION, dbPort)

                                    def testResult = runTests(stageName, bezeichner, 'einheitlicher-ansprechpartner', dbPort)

        							if (env.BRANCH_NAME != 'master') {
                                    	deleteKopStack(bezeichner, stageName)
                                    }

                                    if(!testResult) {
                                        E2E_FAILED += "${stageName}, "
                                        error("Fehler in Stage ${stageName}")
                                    }
                                }
                            }
                            post {
                                always {
                                    script {
                                        publishE2ETestResult("einheitlicher-ansprechpartner", "Goofy E2E-Tests EA")
                                    }
                                }
                            }
                        }
                        stage('E2E-main') {
                            steps {
                                script {
                                    def stageName = env.STAGE_NAME
                                    def bezeichner = generateBezeichner(stageName)
                                    def dbPort = 27019

                                    startEnvironment(bezeichner, stageName, IMAGE_TAG, false, HELM_CHART_VERSION, dbPort)

                                    def testResult = runTests(stageName, bezeichner, 'main-tests', dbPort)

                                    if (env.BRANCH_NAME != 'master') {
                                    	deleteKopStack(bezeichner, stageName)
                                    }

                                    if(!testResult) {
                                        E2E_FAILED += "${stageName}, "
                                        error("Fehler in Stage ${stageName}")
                                    }
                                }
                            }
                            post {
                                always {
                                    script {
                                        publishE2ETestResult("main-tests", "Goofy E2E-Tests main")
                                    }
                                }
                            }
                        }
                    }
                }
            }
			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 | Stage rollout') {
            when {
                branch 'release'
            }

            steps {
                script {
                    FAILED_STAGE = env.STAGE_NAME

                    checkoutProvisioningRepo()

                    setNewGoofyProvisioningVersion('test')
                    setNewGoofyProvisioningVersion('stage')

                    pushNewProvisioningVersion('test stage')
                }
            }
        }
    }
    post {
        failure {
            script {
                if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME == 'release') {
                    sendFailureMessage()
                }
            }
        }
    }
}

Void deployHelmChart(String helmChartVersion) {       
    withCredentials([usernamePassword(credentialsId: 'jenkins-nexus-login', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]){
        if (env.BRANCH_NAME == 'release') {
            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
        }
        else {
            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
        }

        if (result != '') {
            error(result)
        }
    }
}

String generateHelmChartVersion() {
    def chartVersion = "${VERSION}"

    if (env.BRANCH_NAME == 'master') {
        chartVersion += "-${env.GIT_COMMIT.take(7)}"
    }
    else if (env.BRANCH_NAME != 'release') {
        chartVersion += "-${env.BRANCH_NAME}"
    }

    return chartVersion
}

Void tagAndPushDockerImage(String newTag){
    withCredentials([usernamePassword(credentialsId: 'jenkins-nexus-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
        sh 'docker login docker.ozg-sh.de -u ${USER} -p ${PASSWORD}'

        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}"

    if (env.BRANCH_NAME == 'master') {
        imageTag += "-${env.GIT_COMMIT.take(7)}"
    }

    return imageTag
}

Void startEnvironment(String bezeichner, String stage, String imageTag, Boolean isEa, String chartVersion, Integer dbPort) {
    setupAnsible(imageTag, stage, isEa, chartVersion)

    try {
        deleteKopStack(bezeichner, stage)
    } catch (Exception e) {
        echo "deleteKopStack Exception"
    }

    rolloutKopStack(bezeichner, stage)
    addKeycloakGroups(bezeichner, stage)
    addKeycloakUser(bezeichner, stage)
    exposeDatenbank("${env.BUNDESLAND}-${bezeichner}-dev", dbPort)
    exposeElasticSearch()
}

Void setupAnsible(String imageTag, String stage, Boolean isEa, String chartVersion) {
    checkoutProvisioningRepo(stage)

    if (env.BRANCH_NAME == 'release') {
        copyTestEnvironmentToDev(stage)
    }

    editEnvironemntVersion(stage, imageTag, isEa, chartVersion)

    if (isEa) {
        setupEaEnvironment(stage)
    }

    setPlutoDatabasePassword(stage)
}

Void setAnsibleKubeConfig() {
    configFileProvider([configFile(fileId: 'kubeconfig-dev-cluster', variable: 'KUBE_CONFIG')]) {
        sh 'mkdir ~/.kube'
        sh 'cp ${KUBE_CONFIG} ~/.kube/config'
    }
}

Void checkoutProvisioningRepo(String stage="") {
    withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
        dir(stage) {
            sh 'git clone https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/provisioning.git'

            if (env.BRANCH_NAME == 'release') {
                dir('provisioning') {
                    sh 'git checkout release'
                }
            }
        }
    }
}

Void copyTestEnvironmentToDev(stage) {
    dir("${stage}/provisioning") {
        def devEnvFile = "inventories/group_vars/dev/versions"
        def testEnvFile = "inventories/group_vars/test/versions"

        def devVersions = readYaml file: devEnvFile
        def testVersions = readYaml file: testEnvFile

        devVersions.charts = testVersions.charts
        devVersions.versions = testVersions.versions

        writeYaml file: devEnvFile, data: devVersions, overwrite: true
    }
}

Void editEnvironemntVersion(String stage, String imageTag, Boolean isEa, String chartVersion) {
    dir("${stage}/provisioning") {
        def editFile = "inventories/group_vars/dev/versions"

        def devVersions = readYaml file: editFile

        overrideSpringProfiles = getSpringProfile(isEa)

        devVersions.values.goofy.put('env', ['overrideSpringProfiles': overrideSpringProfiles])
        devVersions.values.pluto.put('env', ['overrideSpringProfiles': overrideSpringProfiles])
        devVersions.values.goofy.put('ingress', ['use_staging_cert': true])

        devVersions.versions.goofy.image.tag = imageTag
        devVersions.charts.goofy.version = chartVersion

        writeYaml file: editFile, data: devVersions, overwrite: true
    }
}

String getSpringProfile(Boolean isEa) {
    if (isEa) {
        return "oc,ea,e2e,dev"
    }

    return "oc,e2e,dev"
}

Void setupEaEnvironment(String stage) {
    dir("${stage}/provisioning") {
        def editFile = "inventories/group_vars/all/env"

        def groupVars = readYaml file: editFile

        groupVars.kop_einheitlicher_ansprechpartner = true

        writeYaml file: editFile, data: groupVars, overwrite: true
    }
}

Void setPlutoDatabasePassword(String stage) {
    dir("${stage}/provisioning") {
        def editFile = "inventories/group_vars/dev/versions"

        def devVars = readYaml file: editFile

        devVars.values.pluto.database.password = "XnHhfznNWg65NNd"

        writeYaml file: editFile, data: devVars, overwrite: true
    }
}

Void rolloutKopStack(String bezeichner, String stage) {
    dir("${stage}/provisioning") {
        def ansibleVars = """{"k8s_context":"ozg-dev", \
            "kop_env":"dev", \
            "kop_bezeichner":${bezeichner}, \
            "kop_displayname":${bezeichner}, \
            "kop_postfach_api_key":"", \
            "install_afm_adapter":false, \
            "install_fs_adapter":false, \
            "external_db_enabled":false, \
            "disable_update":true}"""

        if (env.BRANCH_NAME == 'release') {
            sh "ansible-playbook playbook/rollout.yml --extra-vars '${ansibleVars}'"
        }
        else {
            sh "ansible-playbook playbooks/rollout.yml --extra-vars '${ansibleVars}'"
        }
    }
}

Void addKeycloakGroups(String bezeichner, String stage) {
    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":"${env.BUNDESLAND}-${bezeichner}-dev", \
            "group":${groupJson}
            }"""

        dir("${stage}/provisioning") {
            if (env.BRANCH_NAME == 'release') {
                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, String stage) {
    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":"${env.BUNDESLAND}-${bezeichner}-dev", \
            "user":${userJson}
        }"""

        dir("${stage}/provisioning") {
            if (env.BRANCH_NAME == 'release') {
                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(String bezeichner, String stage) {
    dir("${stage}/provisioning") {
        def ansibleVars = """{"k8s_context":"ozg-dev", \
            "kop_env":"dev", \
            "kop_bezeichner":${bezeichner}}"""

        if (env.BRANCH_NAME == 'release') {
            sh "ansible-playbook playbook/delete-commune.yml --extra-vars '${ansibleVars}'"
        }
        else {
            sh "ansible-playbook playbooks/delete-commune.yml --extra-vars '${ansibleVars}'"
        }
    }
}

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 stageName, String bezeichner, String reportFolder, Integer dbPort) {
    def configFile = generateCypressConfig(stageName, bezeichner, reportFolder, env.BUNDESLAND, 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) {
        sh "ls -l /root/.npm/_logs/*-debug.log"
        sh "cat /root/.npm/_logs/*-debug.log"
        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 stage, String bezeichner, String testFolder, Integer dbPort) {
    def namespace = "${env.BUNDESLAND}-${bezeichner}-dev"
    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.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(namespace, dbPort));

        writeJSON file: configName, json: config

        sh "cat ${configName}"
    }

    return configName
}

String getUserManagerEnv(namespace, dbPort){
	def secret = getSecrect(namespace, 'pluto-database-admin-user-manager-database-user');
	def secretPassword = decodeString(secret.password);
	return readJSON(text: """{
        "dbUrl":"mongodb://user-manager-database-user:${secretPassword}@localhost:${dbPort}/admin?ssl=false&directConnection=true", \
        "database":"user-manager-database"}""");
}

def getSecrect(namespace, secretName){
    script {
        return readJSON ( text: sh (script: "kubectl get secret ${secretName} -n ${namespace} -o jsonpath={.data}", returnStdout: true))
    }
}


String getKeycloakUuid(realm, userName) {
    def shScript = """curl -H 'Content-Type: application/json' \
                        -H 'Authorization: bearer ${getKeycloakAccessToken()}' \
                        'https://sso.${env.CLUSTER_BASE_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 ssoURL = "https://sso.${env.CLUSTER_BASE_URL}/realms/master/protocol/openid-connect/token"
		def token = readJSON text: sh (script: 'curl -d "client_id=admin-cli" -d "username=$USER" -d "password=$PASSWORD" -d "grant_type=password" $ssoUrl', returnStdout: true)

		return token.access_token
	}
}

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>"}"""

    if (env.BRANCH_NAME == 'master') {
        room = "!iQPAvQIiRwRpNOszjw:matrix.ozg-sh.de"
    }
    else if (env.BRANCH_NAME == 'release') {
        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
    }
}

Void initHelmRepo() {
    withCredentials([usernamePassword(credentialsId: 'jenkins-nexus-login', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
        sh 'helm repo add ozg-base-apps-snapshot https://nexus.ozg-sh.de/repository/ozg-base-apps-snapshot --username ${USER} --password ${PASSWORD}'
        sh 'helm repo add ozg-base-apps https://nexus.ozg-sh.de/repository/ozg-base-apps --username ${USER} --password ${PASSWORD}'
    }
}

def getElasticsearchSecret(namespace) {
    script {
        def elasticsearch = readJSON ( text: sh (script: "kubectl get secret elasticsearch-credentials -n ${namespace} -o jsonpath={.data}", returnStdout: true))

        return elasticsearch
    }
}

String getElasticsearchEnv(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)
}

String decodeString(encoded) {
    return sh (script: "echo -n ${encoded} | base64 --decode", returnStdout: true)
}

Void setNewGoofyProvisioningVersion(String environment) {
    dir("provisioning") {
        def envFile = "inventories/group_vars/${environment}/versions"

        def envVersions = readYaml file: envFile

        envVersions.versions.goofy.image.tag = IMAGE_TAG
        // envVersions.charts.goofy.version = HELM_CHART_VERSION

        writeYaml file: envFile, data: envVersions, overwrite: true
    }
}

Void pushNewProvisioningVersion(String environment) {
    dir('provisioning') {
        if (sh (script: "git status | grep 'inventories/group_vars/.*/versions'", returnStatus: true) == 1) {
            return
        }

        withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
            sh 'git add inventories/group_vars/*/versions'

            sh 'git config user.email "jenkins@ozg.de"'
            sh 'git config user.name "jenkins"'
            sh "git commit -m 'jenkins rollout ${environment} goofy version ${IMAGE_TAG}'"
            sh 'git push https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/provisioning.git'
        }
    }
}

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} &"
}