Jenkinsfile-prod 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. def SERIAL
  2. def PROD = [ dir: 'stack-deploy', url: 'ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git', host: 'tcp://iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1', registry: 'registry.infoclinica.ru:5000' ]
  3. def VOLGA = [ dir: 'stack-deploy-volga', url: 'ssh://git@git.sdsys.ru:8022/iru/stack-deploy-volga.git', host: 'tcp://iru-volga.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1', registry: 'risz-registry.volganet.ru:5000', service: 'info_node-api' ]
  4. def TULA = [ dir: 'stack-deploy-tula', url: 'ssh://git@git.sdsys.ru:8022/iru/stack-deploy-tula.git', host: 'tcp://iru-tula.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1', registry: 'registry.doctor71.ru:5000', service: 'info_node-api' ]
  5. def GIT_DIR = ''
  6. def GIT_URL = ''
  7. def DHOST = ''
  8. def REGISTRY = ''
  9. def SERVICE = ''
  10. def GIT_DIR_PROD = ''
  11. def GIT_URL_PROD = ''
  12. pipeline {
  13. agent {
  14. label "swarm"
  15. }
  16. options {
  17. disableConcurrentBuilds()
  18. buildDiscarder logRotator(numToKeepStr: '20')
  19. timeout(time: 20, unit: 'MINUTES')
  20. }
  21. environment {
  22. DOCKER_IMAGE='iru/node'
  23. DOCKER_CERT_PATH='/run/secrets/swarm'
  24. DEV_REGISTRY='dev-registry.infoclinica.ru:5000'
  25. JENKINS_MAIL='jenkins@sdsys.ru'
  26. NEWTAG=''
  27. }
  28. parameters {
  29. choice (
  30. choices: 'PROD\nVOLGA\nTULA',
  31. description: 'There is run deploy?',
  32. name: 'CLUSTER')
  33. string(
  34. name: "service_update",
  35. defaultValue: "info_node info_node-api info_node-api2 info_node-api3",
  36. description: "Service name to update."
  37. )
  38. string(
  39. name: "mailto",
  40. defaultValue: "admin@sdsys.ru",
  41. description: "Email which has to be notified."
  42. )
  43. }
  44. stages {
  45. stage("Define Variables") {
  46. steps{
  47. script{
  48. switch (CLUSTER) {
  49. case 'PROD':
  50. GIT_DIR = PROD['dir']
  51. GIT_URL = PROD['url']
  52. DHOST = PROD['host']
  53. REGISTRY = PROD['registry']
  54. break
  55. case 'VOLGA':
  56. GIT_DIR = VOLGA['dir']
  57. GIT_URL = VOLGA['url']
  58. DHOST = VOLGA['host']
  59. REGISTRY = VOLGA['registry']
  60. SERVICE = VOLGA['service']
  61. break
  62. case 'TULA':
  63. GIT_DIR = TULA['dir']
  64. GIT_URL = TULA['url']
  65. DHOST = TULA['host']
  66. REGISTRY = TULA['registry']
  67. SERVICE = TULA['service']
  68. break
  69. }
  70. }
  71. }
  72. }
  73. stage("Tagging in Git") {
  74. steps {
  75. script {
  76. GIT_DIR_PROD = PROD['dir']
  77. GIT_URL_PROD = PROD['url']
  78. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  79. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  80. git clone ${GIT_URL}
  81. [ $CLUSTER != 'PROD' ] && GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  82. git clone ${GIT_URL_PROD}
  83. cp -f ${GIT_DIR_PROD}/tags/${DOCKER_IMAGE}.dev-version ${GIT_DIR}/tags/${DOCKER_IMAGE}.version
  84. cd ${GIT_DIR}
  85. git add -A
  86. git config --global user.email "${JENKINS_MAIL}"
  87. git config --global user.name "Jenkins"
  88. git commit -m 'Version update'| grep '' # Shield ourself from non-zero exit code from 'git commit' command
  89. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  90. git push origin master
  91. """
  92. }
  93. }
  94. }
  95. }
  96. stage("Tagging and pushing to prod registry"){
  97. steps{
  98. script {
  99. NEWTAG=sh (script: "cat ${GIT_DIR}/tags/${DOCKER_IMAGE}.version", returnStdout: true).trim()
  100. sh """docker pull ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  101. docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  102. docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${REGISTRY}/${DOCKER_IMAGE}:latest
  103. docker push ${REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  104. docker push ${REGISTRY}/${DOCKER_IMAGE}:latest
  105. """
  106. }
  107. }
  108. }
  109. stage("Updating service"){
  110. steps{
  111. script {
  112. if (CLUSTER == 'PROD') {
  113. for (String item : service_update.split()) {
  114. echo "Updating ${item}"
  115. try{
  116. sh "DOCKER_HOST=${DHOST} docker service update ${item} --image ${REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  117. }
  118. catch(err){
  119. echo "Recovering service $item"
  120. sh "DOCKER_HOST=${DHOST} docker service rollback ${item}"
  121. throw err
  122. }
  123. }
  124. } else {
  125. try{
  126. echo "Updating ${SERVICE}"
  127. sh "DOCKER_HOST=${DHOST} docker service update ${SERVICE} --image ${REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  128. }
  129. catch(err){
  130. echo "Recovering service ${SERVICE}"
  131. sh "DOCKER_HOST=${DHOST} docker service rollback ${SERVICE}"
  132. throw err
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. post {
  140. always {
  141. echo "CleaningUp work diretory"
  142. deleteDir()
  143. sh "docker image rm -f `docker image ls -q ${REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}`"
  144. }
  145. failure {
  146. mail charset: 'UTF-8',
  147. subject: "Jenkins build ERROR",
  148. mimeType: 'text/html',
  149. to: "${mailto}",
  150. 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}"
  151. }
  152. success {
  153. mail charset: 'UTF-8',
  154. subject: "Jenkins build SUSCCESS",
  155. mimeType: 'text/html',
  156. to: "${mailto}",
  157. 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}"
  158. }
  159. }
  160. }