PROMO_SDK=[ service_name: 'info_promo-sdk', image: 'web-registration/promo-sdk', repo: 'git@github.com:icruportal/web-sdk-demo.git', dockerfile: 'Dockerfile-promo-sdk' ]
PROMO_SEO=[ service_name: 'info_promo-seo', image: 'web-registration/promo-seo', repo: 'git@github.com:icruportal/web-registration.git', dockerfile: 'Dockerfile-promo-seo' ]
@Library('jenkins-library-iru@master') _
def Label = "web-registration-promo-build-${UUID.randomUUID().toString()}"
pipeline {
options {
buildDiscarder logRotator(numToKeepStr: '10')
disableConcurrentBuilds()
timeout(time: 15, unit: 'MINUTES')
}
agent {
kubernetes {
label Label
yaml libraryResource('promoPodDefinition.yaml')
}
}
environment {
CONTEXT='provision@sdsys.office'
HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/k8s.git'
HELM_REPO='https://images.sdsys.ru/artifactory/helm/'
JENKINS_MAIL='jenkins.dev@sdsys.ru'
DOCKER_REGISTRY='images.sdsys.ru'
VALUES_FILE = ''
SOURCE_DIR=''
HELM_DIR=''
TAG=''
}
parameters {
choice (
choices: 'promo-seo\npromo-sdk',
description: 'Which repo to use?',
name: 'APP_NAME'
)
string(
name: "branch",
defaultValue: "release",
description: "Which branch to use"
)
string(
name: "mailto",
defaultValue: "admin@sdsys.ru",
description: "Email which has to be notified."
)
}
stages {
stage("Calculate Variables") {
steps {
script {
switch (APP_NAME) {
case 'promo-sdk':
SOURCE_GIT_URL = PROMO_SDK.repo
DOCKER_IMAGE = PROMO_SDK.image
SERVICE_NAME = PROMO_SDK.service_name
DOCKER_FILE = PROMO_SDK.dockerfile
BUILD_IMAGE_DIR = dirOps.calculateDir(SOURCE_GIT_URL)
break
case 'promo-seo':
SOURCE_GIT_URL = PROMO_SEO.repo
DOCKER_IMAGE = PROMO_SEO.image
SERVICE_NAME = PROMO_SEO.service_name
DOCKER_FILE = PROMO_SEO.dockerfile
BUILD_IMAGE_DIR = dirOps.calculateDir(SOURCE_GIT_URL) + "/promo/build/libs"
break
}
}
}
}
stage("Prepare to build") {
steps {
script {
echo "Pull ${SOURCE_GIT_URL}"
gitOps.clone(SOURCE_GIT_URL)
echo "CheckOut ${branch}"
SOURCE_DIR = dirOps.calculateDir(SOURCE_GIT_URL)
gitOps.checkout(SOURCE_DIR, branch)
echo "Save Version"
dir(SOURCE_DIR) {
COMMIT = sh(script: 'echo $(git log -p -1 --pretty=format:"%h"|head -n1)', returnStdout: true).trim()
}
HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
CHART_PATH = "helm/" + DOCKER_IMAGE.split("/")[0]
VALUES_FILE = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[0] + ".yaml"
}
}
}
stage("Build APP") {
steps {
script {
switch (APP_NAME) {
case 'promo-sdk':
echo "Build APP PROMO-SDK"
dir("${SOURCE_DIR}") {
buildOps.npmPromoSDK()
}
break
case 'promo-seo':
echo "Build APP PROMO-SEO"
echo "Build customer-api"
dir("${SOURCE_DIR}/customer-api") {
buildOps.maven()
}
echo "Build public"
dir("${SOURCE_DIR}/promo/src/public") {
buildOps.npm()
}
echo "Build Promo"
dir("${SOURCE_DIR}/promo") {
buildOps.gradle()
}
}
}
}
}
stage("Build Image") {
steps {
script {
IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${COMMIT}-${BUILD_NUMBER}"
k8sBuildImage.kaniko(IMAGE_TO_PUSH,[CONTENT_DIR: BUILD_IMAGE_DIR],DOCKER_FILE)
}
}
}
stage("Update Helm Chart dev.yaml") {
steps {
script {
TAG_PATH = "images." + DOCKER_IMAGE.split("/")[1].replace('-', '_')
echo "Update ${TAG_PATH} to ${IMAGE_TO_PUSH} in ${VALUES_FILE}"
gitOps.clone(HELM_GIT_URL)
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 {
try {
echo "Update APP"
k8sHelmOps.upgrade(CONTEXT, DOCKER_IMAGE.split("/")[0], DOCKER_IMAGE.split("/")[0], CHART_PATH, VALUES_FILE, HELM_REPO)
}
catch(err) {
echo "Recovering APP"
k8sHelmOps.rollback(CONTEXT, DOCKER_IMAGE.split("/")[0], DOCKER_IMAGE.split("/")[0])
throw err
}
}
}
}
stage("Push changes to REPO") {
steps {
script {
echo "Push changes to Repo"
COMMIT = "Version update " + DOCKER_IMAGE.split("/")[1] + " to " + COMMIT
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}"
}
}
}