@Library('jenkins-library-iru@master') _
def Label = "web-registration-node-build-${UUID.randomUUID().toString()}"
pipeline {
options {
buildDiscarder logRotator(numToKeepStr: '10')
disableConcurrentBuilds()
timeout(time: 10, unit: 'MINUTES')
}
agent {
kubernetes {
label Label
yaml libraryResource('standartPodDefinition.yaml')
}
}
environment {
CONTEXT='provision@sdsys.office'
DOCKER_IMAGE='web-registration/node'
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='images.sdsys.ru'
PROJECT=''
CHART_NAME=''
CHART_PATH=''
HELM_DIR=''
IMAGE_TO_PUSH=''
}
parameters {
string(
name: "repo",
defaultValue: "prod",
description: "Repository to build and/or deploy from."
)
string(
name: "mailto",
defaultValue: "admin@sdsys.ru",
description: "Email which has to be notified."
)
}
stages {
stage("Prepare to Build") {
steps {
script {
echo "GIT clone ${HELM_GIT_URL}"
gitOps.clone(HELM_GIT_URL)
echo "Defining variables"
HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
CHART_PATH = HELM_DIR + "/" + helm + "/" + DOCKER_IMAGE.split("/")[2]
PROJECT = DOCKER_IMAGE.split("/")[1]
VALUES_FILE = "${CHART_PATH}/dev.yaml"
IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
}
}
}
stage("Build Image") {
steps {
script {
echo "Building ${IMAGE_TO_PUSH} with Kaniko"
ARGS = [ "repo=${repo}" ]
k8sBuildImage.kaniko(IMAGE_TO_PUSH, ARGS)
}
}
}
stage("Update Helm Chart") {
steps {
script {
TAG_PATH = "images.${PROJECT}"
echo "Update ${TAG_PATH} to ${IMAGE_TO_PUSH} in ${VALUES_FILE}"
def data = readYaml file: "${VALUES_FILE}"
yamlOps.updateBuilder(TAG_PATH, data, IMAGE_TO_PUSH)
writeYaml file: "${VALUES_FILE}", data: data, overwrite: true
}
}
}
stage("Update APP") {
steps {
script {
RELEASENAME = DOCKER_IMAGE.split("/")[0]
NAMESPACE = DOCKER_IMAGE.split("/")[0]
try {
echo "Update APP to ${IMAGE_TO_PUSH}"
k8sHelmOps.upgrade(CONTEXT, NAMESPACE, RELEASENAME, CHART_PATH, VALUES_FILE)
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"
COMMIT = "Version update ${PROJECT}"
gitOps.push(HELM_DIR, COMMIT)
}
}
}
}
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}"
}
}
}