PROMO_SDK=[ service_name: 'info_promo-sdk', image: 'iru/promo-sdk', repo: 'git@github.com:icruportal/web-sdk-demo.git', dockerfile: 'Dockerfile-promo-sdk' ]
PROMO_SEO=[ service_name: 'info_promo-seo', image: 'iru/promo-seo', repo: 'git@github.com:icruportal/web-registration.git', dockerfile: 'Dockerfile-promo-seo' ]
pipeline {
agent {
label "swarm"
}
options {
disableConcurrentBuilds()
timeout(time: 20, unit: 'MINUTES')
}
environment {
SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
SWARM_GIT_NAME='stack-deploy'
JENKINS_MAIL='jenkins@sdsys.ru'
PACKAGE_NAME='web-registry-promo'
DOCKER_CERT_PATH='/run/secrets/swarm'
DHOST='tcp://dev-iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
NEWTAG=''
}
parameters {
choice (
choices: 'promo-sdk\npromo-seo',
description: 'Which repo to use?',
name: 'GIT_REPO')
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 (GIT_REPO) {
case 'promo-sdk':
REPO=PROMO_SDK.repo
DOCKER_IMAGE=PROMO_SDK.image
SERVICE_NAME=PROMO_SDK.service_name
DOCKER_FILE=PROMO_SDK.dockerfile
break
case 'promo-seo':
REPO=PROMO_SEO.repo
DOCKER_IMAGE=PROMO_SEO.image
SERVICE_NAME=PROMO_SEO.service_name
DOCKER_FILE=PROMO_SEO.dockerfile
break
}
echo REPO
echo DOCKER_IMAGE
echo SERVICE_NAME
echo DOCKER_FILE
}
}
}
stage("Build") {
steps {
script {
echo "Building ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}."
withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
sh """set +x
cat ${GIT_SSH_KEY} > ${WORKSPACE}/id_rsa
chmod 600 ${WORKSPACE}/id_rsa
"""
}
sh "docker build --build-arg branch=${branch} --build-arg git_repo=${REPO} --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} -f ./${DOCKER_FILE} ."
NEWTAG = sh(script: "docker run --rm ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} cat /tmp/version 2> /dev/null", returnStdout: true).trim()
NEWTAG = branch + "-" + NEWTAG + "_" + BUILD_NUMBER
echo "NEWTAG is $NEWTAG"
sh "docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
}
}
}
stage("Publish") {
steps {
echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
}
}
stage("Update") {
steps {
script {
try{
sh "DOCKER_HOST=${DHOST} docker service update ${SERVICE_NAME} --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
}
catch(err){
echo "Recovering service $item"
sh "DOCKER_HOST=${DHOST} docker service rollback ${SERVICE_NAME}"
throw err
}
}
}
}
stage("Tagging"){
steps{
echo "Setting latest tag"
sh """docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest
docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest"""
echo "Updating tag info in ${SWARM_GIT_NAME} repository"
withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
git clone ${SWARM_GIT_URL}
cd ${SWARM_GIT_NAME}
echo -n ${NEWTAG} > tags/${DOCKER_IMAGE}.dev-version
git add -A
git config --global user.email "${JENKINS_MAIL}"
git config --global user.name "Jenkins"
git commit -m 'Version update'
GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
git push origin master
"""
}
}
}
}
post {
always {
echo "CleaningUp work diretory"
deleteDir()
sh "docker image rm -f `docker image ls -q ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}`"
}
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}"
}
}
}