Jenkinsfile-prod 5.3 KB

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