123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- def NEWTAG
- pipeline {
- agent {
- label "swarm"
- }
- environment {
- DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
- DOCKER_IMAGE='admin'
- SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
- SWARM_GIT_NAME='stack-deploy'
- JENKINS_MAIL='jenkins@sdsys.ru'
- CLUSTER_DEV='dev-iru-swarm.infoclinica.lan'
- CLUSTER_PROD='iru-swarm.infoclinica.ru'
- PACKAGE_NAME='web-registry-admin'
- }
- parameters {
- string(
- name: "repo",
- defaultValue: "prod",
- description: "Repository to build and/or deploy from. Values - dev, prod."
- )
- string(
- name: "cluster",
- defaultValue: "dev",
- description: "Cluster to deploy. Values - dev, prod."
- )
- string(
- name: "service_update",
- defaultValue: "info_admin",
- description: "Service name to update."
- )
- string(
- name: "mailto",
- defaultValue: "admin@sdsys.ru",
- description: "Email which has to be notified."
- )
- }
- stages {
- stage("Build") {
- steps {
- echo "Building ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
- sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
- script {
- NEWTAG = sh script: "docker run --rm ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} rpm -q --qf %{VERSION}-%{RELEASE} ${PACKAGE_NAME} 2> /dev/null", returnStdout: true
- NEWTAG = repo + "-" + NEWTAG + "_" + BUILD_NUMBER
- echo "NEWTAG is $NEWTAG"
- }
- sh "docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}"
- }
- }
- stage("Publish") {
- steps {
- echo "Publishing ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}"
- sh "docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}"
- }
- }
- stage("Update") {
- when { expression { service_update != "" } }
- steps {
- script {
- def CLUSTER_TGT
- echo "Cluster is $cluster"
- switch (cluster) {
- case 'prod':
- CLUSTER_TGT=CLUSTER_PROD
- echo "prod - $CLUSTER_TGT"
- break
- case 'dev':
- CLUSTER_TGT=CLUSTER_DEV
- echo "dev - $CLUSTER_TGT"
- break
- default:
- currentBuild.result = 'ABORTED'
- error('Unknown cluster')
- }
- for (String item : service_update.split()) {
- try{
- echo "Updating $item, cluster $CLUSTER_TGT"
- withEnv(["service_name=${item}"]) {
- sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
- export DOCKER_HOST=tcp://${CLUSTER_TGT}:2376 DOCKER_TLS_VERIFY=1
- docker service update ${service_name} --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}
- '''
- }
- }
- catch(err){
- echo "Recovering service $item"
- withEnv(["service_name=${item}"]) {
- sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
- export DOCKER_HOST=tcp://${CLUSTER_TGT}:2376 DOCKER_TLS_VERIFY=1
- docker service rollback ${service_name}
- '''
- }
- throw err
- }
- }
- }
- }
- }
- stage("Tagging"){
- steps{
- echo "Setting latest tag"
- sh '''docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
- docker push ${DOCKER_REGISTRY}/iru/${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}.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
- ls'''
- }
- }
- }
- }
- post {
- always {
- echo "CleaningUp work diretory"
- deleteDir()
- }
- 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}"
- }
- success {
- mail charset: 'UTF-8',
- subject: "Jenkins build SUSCCESS",
- mimeType: 'text/html',
- to: "${mailto}",
- body: "<b>Congradulations!!!</b> <b><br> Jenkins job succefully finished.\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}"
- }
- }
- }
|