@Library('jenkins-library-iru@master') _ def Label = "analis-lab-update-${UUID.randomUUID().toString()}" pipeline { options { buildDiscarder logRotator(numToKeepStr: '10') disableConcurrentBuilds() timeout(time: 60, unit: 'MINUTES') } agent { kubernetes { label Label yaml libraryResource('deployPodDefinition.yaml') } } environment { CONTEXT='provision@sdsys.office' DOCKER_IMAGE='web-laboratory/analis' HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/k8s.git' JENKINS_MAIL='jenkins.dev@sdsys.ru' DOCKER_REGISTRY='images.sdsys.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/" + DOCKER_IMAGE.split("/")[0] VALUES_FILE_DEV = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[1] + ".yaml" VALUES_FILE_PROD = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[1] + ".yaml" } } } stage("Update Helm Chart values.yaml") { steps { script { echo "Update values.yaml" def data_build = readYaml file: VALUES_FILE_DEV TAG_PATH_BUILD = "images." + DOCKER_IMAGE.split("/")[1] + ".build" TAG = yamlOps.getBuilder(TAG_PATH_BUILD, data_build) def data_stable = readYaml file: VALUES_FILE_PROD TAG_PATH_STABLE = "images." + DOCKER_IMAGE.split("/")[1] + ".stable" yamlOps.updateBuilder(TAG_PATH_STABLE, data_stable, TAG) writeYaml file: VALUES_FILE_PROD, data: data_stable, overwrite: true } } } stage("Update APP") { steps { script { RELEASENAME = DOCKER_IMAGE.split("/")[0] NAMESPACE = DOCKER_IMAGE.split("/")[0] IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${TAG}" try { echo "Update APP" k8sHelmOps.upgrade(CONTEXT, NAMESPACE, RELEASENAME, CHART_PATH, VALUES_FILE_PROD, HELM_REPO) echo "Update success!!!" } catch(err) { echo "Recovering APP" // k8sHelmOps.rollback(CONTEXT, NAMESPACE, RELEASENAME) throw err } } } } stage("Push changes to REPO") { steps { script { echo "Push changes to Repo" PROJECT = DOCKER_IMAGE.split("/")[1] 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: "ATTENTION!!!
Jenkins job failed.\n\n
Project Name:
${env.JOB_NAME}
\nBuild Number:
${env.BUILD_NUMBER}
\nURL Build:
${RUN_DISPLAY_URL}" } } }