Browse Source

add new Jenkinsfile

Tomishinets Vladimir 4 years ago
parent
commit
1f049882b3
2 changed files with 106 additions and 1 deletions
  1. 1 1
      buildImage.Jenkinsfile
  2. 105 0
      updateK8s.Jenkinsfile

+ 1 - 1
buildImage.Jenkinsfile

@@ -68,7 +68,7 @@ pipeline {
         script {
           echo "Update Helm Chart"
           def data = readYaml file: "${CHART_PATH}/values.yaml"
-          TAG_PATH = "images.${PROJECT}.build"
+          TAG_PATH = "images.${PROJECT}.dev"
           yamlOps.updateBuilder(TAG_PATH, data, TAG)
           writeYaml file: "${CHART_PATH}/values.yaml", data: data, overwrite: true
         }

+ 105 - 0
updateK8s.Jenkinsfile

@@ -0,0 +1,105 @@
+@Library('jenkins-library@master') _
+def Label = "analis-lab-build-${UUID.randomUUID().toString()}"
+pipeline {
+  options {
+    buildDiscarder logRotator(numToKeepStr: '10')
+    disableConcurrentBuilds()
+    timeout(time: 60, unit: 'MINUTES')
+  }
+  agent {
+    kubernetes {
+      label Label
+      yaml libraryResource('deployPodDefinition.yaml')
+    }
+  }
+  environment {
+    CLOUD='cloud-it-k8s'
+    DOCKER_IMAGE='lab/analis'
+    CHART_SUBPATH='helm/analis'
+    NAMESPACE='analis'
+    HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/k8s.git'
+    JENKINS_MAIL='jenkins.dev@sdsys.ru'
+    DOCKER_REGISTRY='jcr.infoclinica.ru'
+    CHART_NAME=''
+    CHART_PATH=''
+    PROJECT=''
+    HELM_DIR=''
+    TAG=''
+  }
+  parameters {
+    string(
+      name: "mailto",
+      defaultValue: "admin@sdsys.ru",
+      description: "Email which has to be notified."
+    )
+  }
+  stages {
+    stage("Prepare to Update") {
+      steps {
+        script {
+          echo "Pull Repo"
+          gitOps.clone(HELM_GIT_URL)
+          HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
+          CHART_PATH = "${HELM_DIR}/${CHART_SUBPATH}"
+          PROJECT = DOCKER_IMAGE.split("/")[1]
+        }
+      }
+    }
+    stage("Update Helm Chart values.yaml") {
+      steps {
+        script {
+          echo "Update values.yaml"
+          def data = readYaml file: "${CHART_PATH}/values.yaml"
+          TAG_PATH = "images.${PROJECT}.dev"
+          TAG = yamlOps.getBuilder(TAG_PATH, data)
+          TAG_PATH = "images.${PROJECT}.prod"
+          yamlOps.updateBuilder(TAG_PATH, data, TAG)
+          writeYaml file: "${CHART_PATH}/values.yaml", data: data, overwrite: true
+        }
+      }
+    }
+    stage("Update APP") {
+      steps {
+        script {
+          RELEASENAME = CHART_SUBPATH.split("/")[1]
+          try {
+            echo "Update APP"
+            k8sHelmOps.upgrade(CLOUD, NAMESPACE, RELEASENAME, CHART_PATH, 'dev')
+          }
+          catch(err) {
+            echo "Recovering APP"
+            k8sHelmOps.rollback(CLOUD, NAMESPACE, RELEASENAME)
+            throw err
+          }
+        }
+      }
+    }
+    stage("Push changes to REPO") {
+      steps {
+        script {
+          echo "Push changes to Repo"
+          COMMIT = "Version update ${PROJECT} to ${TAG}"
+          gitOps.push(HELM_DIR, COMMIT)
+        }
+      }
+    }
+    stage("Tagging Image") {
+      steps {
+        script {
+          echo "Tagging ${TAG} to latest"
+          IMAGE = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
+          k8sBuildImage.crane(IMAGE, TAG, 'latest')
+        }
+      }
+    }
+  }
+  post {
+    failure {
+      mail charset: 'UTF-8',
+           subject: "Jenkins build ERROR",
+           mimeType: 'text/html',
+           to: "${mailto}",
+           body: "<b>ATTENTION!!!</b> <b><br> Jenkins job failed.\n\n <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>\nBuild Number:</b> ${env.BUILD_NUMBER} <b><br>\nURL Build:</b> ${RUN_DISPLAY_URL}"
+    }
+  }
+}