Skip to content
Snippets Groups Projects
Select Git revision
  • f74c1a10cfdbfe022f01461a254336ffefcf1adf
  • main default protected
  • release
  • OZG-8217-AFM-InboxReference
  • OZG-8280-fitconnect
  • OZG-8217-inbox-reference-identifier
  • develop
  • OZG-8075-NPE-wenn-der-Knoten-ORGID-leer-ist
  • OZG-7878-Resilienteres-Mapping-vom-AFM-Postfach-Address-Type
  • OZG-7144_extend_testfactory
  • OZG-7426-POC-DateitransferEingang
  • OZG-7144_orgid_lower_case
  • OZG-6922-reproduce-xdomea-bug
  • OZG-6710-Update-common-lib
  • OZG-6343-enable-dfoerdermittel-changes
  • OZG-6343-enable-dfoerdermittel
  • try-lasttests
  • formcycle-itcase-to-vorgangremoteservice
  • dfoerderfix
  • OZG-5156-Versammlungsanzeige-via-xta-adapter
  • OZG-5156-Anbindung-Versammlungsanzeige-2
  • 2.23.0
  • 2.22.0
  • 2.21.0
  • 2.20.0
  • 2.19.0
  • 2.18.1
  • 2.18.0
  • 2.17.0
  • 2.16.0
  • 2.15.0
  • 2.14.0
  • 2.13.0
  • 2.12.0
  • 2.11.0
  • 2.10.0
  • 2.9.1
  • 2.9.0
  • 2.8.0
  • 2.7.0
  • 2.6.0
41 results

sonar-project.properties

Blame
  • Jenkinsfile.admin 8.81 KiB
    pipeline {
      agent {
        node {
          label 'ozgcloud-jenkins-build-agent'
        }
      }
    
      environment {
            BLUE_OCEAN_URL = "https://jenkins.infra.ozg-cloud.systems/job/admin-client/job/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/"
            RELEASE_REGEX = /\d+.\d+.\d+/
            SNAPSHOT_REGEX = /\d+.\d+.\d+-SNAPSHOT/
            FAILED_STAGE = ""
            SH_SUCCESS_STATUS_CODE = 0
        }
    
        options {
            timeout(time: 1, unit: 'HOURS')
            disableConcurrentBuilds()
            buildDiscarder(logRotator(numToKeepStr: '5'))
        }
    
        stages {
            stage('Check Version') {
                steps {
                    script {
                        FAILED_STAGE = env.STAGE_NAME 
                         dir('alfa-client') { 
                        VERSION = getPackagejsonVersion()
                         }
                    }
                }
            }
        
            stage('build admin client and its docker image') {
                steps {
                    script {
    	                FAILED_STAGE=env.STAGE_NAME
    	
    	                dir('alfa-client') {
    	                    sh 'echo "registry=https://nexus.ozg-sh.de/repository/npm-proxy" >> ~/.npmrc'
    	                    sh 'echo "//nexus.ozg-sh.de/:_auth=amVua2luczprTSFnNVUhMVQzNDZxWQ==" >> ~/.npmrc'
    	
    	                    sh 'npm cache verify'
    	                    sh 'npm install'
    	
    	                    if (isReleaseBranch()) {
    	                    	sh 'npm run ci-prodBuild-admin'
    	                    } else {
    	                    	sh 'npm run ci-build-admin'
    	                    }
    	                    if (isMasterBranch()) {
                                withSonarQubeEnv('sonarqube-ozg-sh'){
                                    sh 'npm run ci-sonar'
                                }
                            } else {
                                sh 'npm run ci-test'
                            }
    	                }
                    }
                }
            }
    
            stage('Tag and Push Docker image') {
                steps {
                    script {
                        FAILED_STAGE=env.STAGE_NAME
                        IMAGE_TAG = generateImageTag()
    
                        tagAndPushDockerImage(IMAGE_TAG)
    
                        if (isMasterBranch()) {
                            tagAndPushDockerImage('snapshot-latest')
                        }
                        else if (isReleaseBranch()) {
                            tagAndPushDockerImage('latest')
                        }
                    }
                }
            }
    
            stage('Test, build and deploy Helm Chart') {
                steps {
                    script {
                        FAILED_STAGE=env.STAGE_NAME
                        HELM_CHART_VERSION = generateHelmChartVersion()
    
                       dir('alfa-client/apps/admin') {
                        sh "./run_helm_test.sh"
                       } 
    
                        dir('alfa-client/apps/admin/src/main/helm') {                    
    
                            sh "helm package --version=${HELM_CHART_VERSION} ."
    
                            deployHelmChart(HELM_CHART_VERSION)
                        }
                    }
                }
            }
    
            stage('Trigger Dev rollout') {
                when {
                    branch 'master'
                }
                steps {
                    script {
                        FAILED_STAGE = env.STAGE_NAME
    
                        cloneGitopsRepo()
    
                        setNewDevVersion()
                        pushGitopsRepo()
                    }
                }
            }
    
            stage('Trigger Test rollout') {
                when {
                    branch 'release-admin'
                }
                steps {
                    script {
                        FAILED_STAGE = env.STAGE_NAME
    
                        cloneGitopsRepo()
    
                        setNewTestVersion()
                        pushGitopsRepo()
                    }
                }
            }
        }
    
        post { 
            failure {
                script {    
                    if (isMasterBranch() || isReleaseBranch()) {
                        sendFailureMessage()
                    }
                }
            }
        }
    }
    
    
    Boolean isReleaseBranch() {
        return env.BRANCH_NAME == 'release-admin'
    }
    
    String generateImageTag() {
        def imageTag = "${env.BRANCH_NAME}-${VERSION}"
    
        if (isMasterBranch()) {
            imageTag += "-${env.GIT_COMMIT.take(7)}"
        }
    
        return imageTag
    }
    
    Void cloneGitopsRepo() {
        withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
            sh 'git clone https://${USER}:${TOKEN}@git.ozg-sh.de/ozgcloud-devops/gitops.git'
        }
        configureGit()
    }
    
    Void configureGit() {
        final email = "jenkins@ozg-sh.de"
        final name = "jenkins"
    
        dir("gitops") {
            sh "git config user.email '${email}'"
            sh "git config user.name '${name}'"
        }
    }
    
    Void setNewDevVersion() {
        setNewGitopsVersion("dev")
    }
    
    Void setNewTestVersion() {
        setNewGitopsVersion("test")
    }
    
    Void setNewGitopsVersion(String environment) {
        dir("gitops") {
            def envFile = "${environment}/application/values/admin-client-values.yaml"
    
            def envVersions = readYaml file: envFile
    
            envVersions.admin_client.image.tag = IMAGE_TAG
            envVersions.admin_client.helm.version = HELM_CHART_VERSION
    
            writeYaml file: envFile, data: envVersions, overwrite: true
    
            if (hasValuesFileChanged(environment)) {
                sh "git add ${envFile}"
                sh "git commit -m 'jenkins rollout ${environment} admin_client version ${IMAGE_TAG}'"
            }
        }
    }
    
    Boolean hasValuesFileChanged(String environment) {
        return sh (script: "git status | grep '${environment}/application/values/admin-client-values.yaml'", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer
    }
    
    
    Void pushGitopsRepo() {
        withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
            dir("gitops") {
                if (hasUnpushedCommits()) {
                    sh 'git push https://${USER}:${TOKEN}@git.ozg-sh.de/ozgcloud-devops/gitops.git'
                }
            }
        }
    }
    
    Boolean hasUnpushedCommits() {
        return sh (script: "git cherry -v | grep .", returnStatus: true) == env.SH_SUCCESS_STATUS_CODE as Integer
    }
    
    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/admin-client:build-latest docker.ozg-sh.de/admin-client:${newTag}"
            sh "docker push docker.ozg-sh.de/admin-client:${newTag}"
        }
    }
    String getPackagejsonVersion() {
        def packageJSON = readJSON file: 'package.json'
        def packageJSONVersion = packageJSON.version
        echo packageJSONVersion
        return packageJSONVersion
    }
    
    Void deployHelmChart(String helmChartVersion) {
        withCredentials([usernamePassword(credentialsId: 'jenkins-nexus-login', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]){
            if (isReleaseBranch()) {
                result = sh script: '''curl -u $USERNAME:$PASSWORD https://nexus.ozg-sh.de/service/rest/v1/components?repository=ozg-base-apps -F file=@admin-client-'''+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=@admin-client-'''+helmChartVersion+'''.tgz''', returnStdout: true
            }
    
            if (result != '') {
                error(result)
            }
        }
    }
    String generateHelmChartVersion() {
        def chartVersion = "${VERSION}"
    
        if (isMasterBranch()) {
            chartVersion += "-${env.GIT_COMMIT.take(7)}"
        }
        else if (!isReleaseBranch()) {
            chartVersion += "-${env.BRANCH_NAME}"
        }
    
        return chartVersion.replaceAll("_", "-")
    }
    
    Boolean isMasterBranch() {
        return env.BRANCH_NAME == 'master'
    }
    
    Void sendFailureMessage() {
        def room = ''
        def data = """{"msgtype":"m.text", \
                        "body":"Admin-Client: Build Failed. Stage: ${FAILED_STAGE} Build-ID: ${env.BUILD_NUMBER} Link: ${BLUE_OCEAN_URL}", \
                        "format": "org.matrix.custom.html", \
                        "formatted_body":"Admin-Client: Build Failed. Stage: ${FAILED_STAGE} Build-ID: <a href='${BLUE_OCEAN_URL}'>${env.BUILD_NUMBER}</a>"}"""
    
        if (isMasterBranch()) {
            room = "!iQPAvQIiRwRpNOszjw:matrix.ozg-sh.de"
        }
        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
        }
    }