1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- pipeline {
- agent {
- label "swarm"
- }
- environment {
- DOCKER_IMAGE='lab/admin-lab'
- DOCKER_CERT_PATH='/run/secrets/swarm'
- DHOST='tcp://iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
- DEV_REGISTRY='dev-registry.infoclinica.ru:5000'
- PROD_REGISTRY='registry.infoclinica.ru:5000'
- SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/stack-deploy.git'
- SWARM_GIT_NAME='stack-deploy'
- JENKINS_MAIL='jenkins@sdsys.ru'
- NEWTAG=''
- }
- parameters {
- string(
- name: "service_update",
- defaultValue: "lab_admin-lab",
- description: "Service name to update."
- )
- string(
- name: "mailto",
- defaultValue: "admin@sdsys.ru",
- description: "Email which has to be notified."
- )
- }
- stages {
- stage("Tagging in Git"){
- steps{
- 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}
- cp -f tags/${DOCKER_IMAGE}.dev-version 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'| grep '' # Shield ourself from non-zero exit code from 'git commit' command
- GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
- git push origin master
- ls"""
- }
- }
- }
- stage("Tagging and pushing to prod registry"){
- steps{
- script {
- NEWTAG=sh (script: "cat ${SWARM_GIT_NAME}/tags/${DOCKER_IMAGE}.version", returnStdout: true).trim()
- sh """docker pull ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
- docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
- docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${PROD_REGISTRY}/${DOCKER_IMAGE}:latest
- docker push ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
- docker push ${PROD_REGISTRY}/${DOCKER_IMAGE}:latest
- """
- }
- }
- }
- stage("Updating service"){
- steps{
- script {
- for (String item : service_update.split()) {
- echo "Updating ${item}"
- try{
- sh "DOCKER_HOST=${DHOST} docker service update ${item} --image ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
- }
- catch(err){
- echo "Recovering service $item"
- sh "DOCKER_HOST=${DHOST} docker service rollback ${item}"
- throw err
- }
- }
- }
- }
- }
- }
- post {
- always {
- echo "CleaningUp work diretory"
- deleteDir()
- sh "docker image rm -f `docker image ls -q ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}`"
- }
- 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}"
- }
- }
- }
|