Ver código fonte

add k8s jenkins files

Tomishinets Vladimir 4 anos atrás
pai
commit
fbd50bb6fe
2 arquivos alterados com 215 adições e 0 exclusões
  1. 111 0
      devK8s.Jenkinsfile
  2. 104 0
      prodK8s.Jenkinsfile

+ 111 - 0
devK8s.Jenkinsfile

@@ -0,0 +1,111 @@
+@Library('jenkins-library@master') _
+def Label = "node-build-${UUID.randomUUID().toString()}"
+pipeline {
+  options {
+    buildDiscarder logRotator(numToKeepStr: '10')
+    disableConcurrentBuilds()
+  }
+  agent {
+    kubernetes {
+      label Label
+      yaml libraryResource('adminPodDefinition.yaml')
+    }
+  }
+  environment {
+    CLOUD='cloud-it-k8s'
+    DOCKER_IMAGE='iru/node'
+    CHART_PATH='helm/info'
+    NAMESPACE='info'
+    HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/k8s.git'
+    APP_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/node.git'
+    JENKINS_MAIL='jenkins.dev@sdsys.ru'
+    DOCKER_REGISTRY='jcr.infoclinica.ru'
+    CHART_NAME=''
+    APP_DIR=''
+    HELM_DIR=''
+    TAG=''
+  }
+  parameters {
+    string(
+      name: "repo",
+      defaultValue: "prod",
+      description: "Repository to build and/or deploy from. Values - dev, prod."
+    )
+    string(
+      name: "mailto",
+      defaultValue: "admin@sdsys.ru",
+      description: "Email which has to be notified."
+    )
+  }
+  stages {
+    stage("Prepare to Build") {
+      steps {
+        script {
+          gitOps.clone(APP_GIT_URL)
+          APP_DIR = dirOps.calculateDir(APP_GIT_URL)
+        }
+      }
+    }
+    stage("Build Image") {
+      steps {
+        script {
+          dir (APP_DIR) {
+            echo "Build Image with Kaniko"
+            REGISTRY = DOCKER_REGISTRY + "/" + DOCKER_IMAGE
+            ARGS = [ "repo=${repo}" ]
+            TAG = "${repo}-${BUILD_NUMBER}"
+            k8sBuildImage.kaniko(REGISTRY, TAG, ARGS)
+          }
+        }
+      }
+    }
+    stage("Update Helm Chart values.yaml") {
+      steps {
+        script {
+          echo "Update Helm Chart"
+          gitOps.clone(HELM_GIT_URL)
+          HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
+          def data = readYaml file: "${HELM_DIR}/${CHART_PATH}/values.yaml"
+          TAG_PATH = "images.${APP_DIR}.dev"
+          yamlOps.updateBuilder(TAG_PATH, data, TAG)
+          writeYaml file: "${HELM_DIR}/${CHART_PATH}/values.yaml", data: data, overwrite: true
+        }
+      }
+    }
+    stage("Update APP") {
+      steps {
+        script {
+          CHART = "${HELM_DIR}/${CHART_PATH}"
+          CHART_NAME = CHART_PATH.split("/")[1]
+          try {
+            echo "Update APP"
+            k8sHelmOps.upgrade(CLOUD, NAMESPACE, CHART_NAME, CHART)
+          }
+          catch(err) {
+            echo "Recovering APP"
+            k8sHelmOps.rollback(CLOUD, NAMESPACE, CHART_NAME)
+            throw err
+          }
+        }
+      }
+    }
+    stage("Push changes to REPO") {
+      steps {
+        script {
+          echo "Push changes to Repo"
+          COMMIT = "Version update ${APP_DIR}"
+          gitOps.push(HELM_DIR, COMMIT)
+        }
+      }
+    }
+  }
+  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}"
+    }
+  }
+}

+ 104 - 0
prodK8s.Jenkinsfile

@@ -0,0 +1,104 @@
+@Library('jenkins-library@master') _
+def Label = "admin-build-${UUID.randomUUID().toString()}"
+pipeline {
+  options {
+    buildDiscarder logRotator(numToKeepStr: '10')
+    disableConcurrentBuilds()
+  }
+  agent {
+    kubernetes {
+      label Label
+      yaml libraryResource('adminPodDefinition.yaml')
+    }
+  }
+  environment {
+    CLOUD='cloud-it-k8s'
+    DOCKER_IMAGE='iru/node'
+    CHART_PATH='helm/info'
+    NAMESPACE='info'
+    HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/k8s.git'
+    APP_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/node.git'
+    JENKINS_MAIL='jenkins.dev@sdsys.ru'
+    DOCKER_REGISTRY='jcr.infoclinica.ru'
+    CHART_NAME=''
+    APP_DIR=''
+    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)
+        }
+      }
+    }
+    stage("Update Helm Chart values.yaml") {
+      steps {
+        script {
+          echo "Update values.yaml"
+          APP_DIR = dirOps.calculateDir(APP_GIT_URL)
+          def data = readYaml file: "${HELM_DIR}/${CHART_PATH}/values.yaml"
+          TAG_PATH = "images.${APP_DIR}.dev"
+          TAG = yamlOps.getBuilder(TAG_PATH, data)
+          TAG_PATH = "images.${APP_DIR}.prod"
+          yamlOps.updateBuilder(TAG_PATH, data, TAG)
+          writeYaml file: "${HELM_DIR}/${CHART_PATH}/values.yaml", data: data, overwrite: true
+        }
+      }
+    }
+    stage("Update APP") {
+      steps {
+        script {
+          CHART = "${HELM_DIR}/${CHART_PATH}"
+          CHART_NAME = CHART_PATH.split("/")[1]
+          try {
+            echo "Update APP"
+            k8sHelmOps.upgrade(CLOUD, NAMESPACE, CHART_NAME, CHART, 'prod')
+          }
+          catch(err) {
+            echo "Recovering APP"
+            k8sHelmOps.rollback(CLOUD, NAMESPACE, CHART_NAME)
+            throw err
+          }
+        }
+      }
+    }
+    stage("Push changes to REPO") {
+      steps {
+        script {
+          echo "Push changes to Repo"
+          COMMIT = "Version update ${APP_DIR}"
+          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}"
+    }
+  }
+}