Jenkinsfile 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. def NEWTAG
  2. pipeline {
  3. agent {
  4. label "swarm"
  5. }
  6. environment {
  7. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  8. DOCKER_IMAGE='admin'
  9. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  10. SWARM_GIT_NAME='stack-deploy'
  11. JENKINS_MAIL='jenkins@sdsys.ru'
  12. CLUSTER_DEV='dev-iru-swarm.infoclinica.lan'
  13. CLUSTER_PROD='iru-swarm.infoclinica.ru'
  14. }
  15. parameters {
  16. string(
  17. name: "repo",
  18. defaultValue: "prod",
  19. description: "Repository to build and/or deploy from. Values - dev, prod."
  20. )
  21. string(
  22. name: "cluster",
  23. defaultValue: "dev",
  24. description: "Cluster to deploy. Values - dev, prod."
  25. )
  26. string(
  27. name: "service_update",
  28. defaultValue: "info_admin",
  29. description: "Service name to update."
  30. )
  31. string(
  32. name: "mailto",
  33. defaultValue: "admin@sdsys.ru",
  34. description: "Email which has to be notified."
  35. )
  36. }
  37. stages {
  38. stage("Build") {
  39. steps {
  40. echo "Building ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
  41. sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
  42. script {
  43. NEWTAG = sh script: "docker run --rm ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} rpm -q --qf %{VERSION}-%{RELEASE} web-registry-admin 2> /dev/null", returnStdout: true
  44. echo "NEWTAG is $NEWTAG"
  45. }
  46. }
  47. }
  48. stage("Publish") {
  49. steps {
  50. echo "Tag is $NEWTAG"
  51. error "Stop pipeline"
  52. echo "Publishing ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  53. sh "docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  54. }
  55. }
  56. stage("Update") {
  57. when { expression { service_update != "" } }
  58. steps {
  59. script {
  60. for (String item : service_update.split()) {
  61. try{
  62. echo "Updating $item"
  63. if (cluster == 'prod') {
  64. withEnv(["service_name=${item}"]) {
  65. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  66. export DOCKER_HOST=tcp://${CLUSTER_PROD}:2376 DOCKER_TLS_VERIFY=1
  67. docker service update ${service_name} --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}
  68. '''
  69. }
  70. }
  71. if (cluster == 'dev') {
  72. withEnv(["service_name=${item}"]) {
  73. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  74. export DOCKER_HOST=tcp://${CLUSTER_DEV}:2376 DOCKER_TLS_VERIFY=1
  75. docker service update ${service_name} --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}
  76. '''
  77. }
  78. }
  79. }
  80. catch(err){
  81. echo "Recovering service $item"
  82. if (cluster == 'prod') {
  83. withEnv(["service_name=${item}"]) {
  84. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  85. export DOCKER_HOST=tcp://${CLUSTER_PROD}:2376 DOCKER_TLS_VERIFY=1
  86. docker service rollback ${service_name}
  87. '''
  88. }
  89. }
  90. if (cluster == 'dev') {
  91. withEnv(["service_name=${item}"]) {
  92. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  93. export DOCKER_HOST=tcp://${CLUSTER_DEV}:2376 DOCKER_TLS_VERIFY=1
  94. docker service rollback ${service_name}
  95. '''
  96. }
  97. }
  98. throw err
  99. }
  100. }
  101. }
  102. }
  103. }
  104. stage("Tagging"){
  105. steps{
  106. echo "Setting latest tag"
  107. sh '''docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
  108. docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest'''
  109. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  110. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  111. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  112. git clone ${SWARM_GIT_URL}
  113. cd ${SWARM_GIT_NAME}
  114. echo -n ${repo}-${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.version
  115. git add -A
  116. git config --global user.email "${JENKINS_MAIL}"
  117. git config --global user.name "Jenkins"
  118. git commit -m 'Version update'
  119. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  120. git push origin master
  121. ls'''
  122. }
  123. }
  124. }
  125. }
  126. post {
  127. always {
  128. echo "CleaningUp work diretory"
  129. deleteDir()
  130. }
  131. failure {
  132. mail charset: 'UTF-8',
  133. subject: "Jenkins build ERROR",
  134. mimeType: 'text/html',
  135. to: "${mailto}",
  136. 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}"
  137. }
  138. success {
  139. mail charset: 'UTF-8',
  140. subject: "Jenkins build SUSCCESS",
  141. mimeType: 'text/html',
  142. to: "${mailto}",
  143. 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}"
  144. }
  145. }
  146. }