Jenkinsfile-prod 6.0 KB

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