updateVersionJenkinsfile 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. def ENAMES = [ 'prod-open', 'prod' ]
  2. //def def ENAMES = [ 'dev-open', 'dev' ]
  3. def CLUSTERS = ['prod': 'iru-swarm.infoclinica.lan',
  4. 'prod-open': 'iru-swarm1-open.infoclinica.ru',
  5. 'dev': 'dev-iru-swarm.infoclinica.lan',
  6. 'dev-open': 'dev-iru-swarm1-open.infoclinica.lan']
  7. def REGISTRIES = ['prod': 'registry.infoclinica.ru:5000',
  8. 'prod-open': 'registry.infoclinica.ru:5000',
  9. 'dev': 'dev-registry.infoclinica.ru:5000',
  10. 'dev-open': 'dev-registry.infoclinica.ru:5000']
  11. def DHOST = ['prod': 'tcp://iru-swarm.infoclinica.lan:2376',
  12. 'prod-open': 'tcp://iru-swarm1-open.infoclinica.ru:2376',
  13. 'dev': 'tcp://dev-iru-swarm.infoclinica.lan:2376',
  14. 'dev-open': 'tcp://dev-iru-swarm1-open.infoclinica.lan:2376']
  15. def LIST_SERVICE = [:]
  16. def TO_ROLLBACK = [:]
  17. def ERROR_JOB
  18. def VERSION_TO_UPDATE
  19. pipeline {
  20. agent {
  21. label "swarm"
  22. }
  23. environment {
  24. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/stack-deploy.git'
  25. SWARM_GIT_NAME='stack-deploy'
  26. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  27. DOCKER_CERT_PATH='/run/secrets/swarm'
  28. DOCKER_IMAGE='analis-wineservice'
  29. SERVICE_LABEL='analis-std'
  30. STACK_LABEL='analis-wineservice'
  31. }
  32. parameters {
  33. string(
  34. name: "mailto",
  35. defaultValue: "admin@sdsys.ru",
  36. description: "Email which has to be notified."
  37. )
  38. choice (
  39. choices: 'up\ndown',
  40. description: 'What you want? Up version or down?',
  41. name: 'TASK_ACTION'
  42. )
  43. }
  44. stages {
  45. stage("Git Pull. Define Version to Update") {
  46. steps {
  47. script {
  48. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  49. sh """set +x && GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  50. git clone ${SWARM_GIT_URL}
  51. """
  52. }
  53. switch (TASK_ACTION) {
  54. case 'up':
  55. VERSION_TO_UPDATE = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.build-version" , returnStdout: true).trim()
  56. sh "set +x && echo \"Update version of Apps to $VERSION_TO_UPDATE\" > commit.txt"
  57. break
  58. case 'down':
  59. VERSION_TO_UPDATE = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version" , returnStdout: true).trim()
  60. sh "set +x && echo \"Downgrade version of Apps to $VERSION_TO_UPDATE\" > commit.txt"
  61. break
  62. }
  63. }
  64. }
  65. }
  66. stage("Get Names Of Running Services") {
  67. steps {
  68. script {
  69. ENAMES.each { item ->
  70. echo "Viewing: ${item} services, on ${CLUSTERS.get((item))}"
  71. LIST_SERVICE[item] = sh (script: "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service ls -f \
  72. label=com.docker.stack.namespace=$STACK_LABEL -f \
  73. label=ru.infoclinica.service=$SERVICE_LABEL --format '{{.Name}}'" , returnStdout: true).split('\n')
  74. switch (TASK_ACTION) {
  75. case 'up':
  76. echo "These services will be updated to version $VERSION_TO_UPDATE: ${LIST_SERVICE.get(item)}"
  77. break
  78. case 'down':
  79. echo "These services will be downgraded to version $VERSION_TO_UPDATE ${LIST_SERVICE.get(item)}"
  80. break
  81. }
  82. }
  83. }
  84. }
  85. }
  86. stage("Update Services") {
  87. steps {
  88. script {
  89. ENAMES.each { item ->
  90. TO_ROLLBACK[item] = []
  91. try {
  92. for (name in LIST_SERVICE.get(item)) {
  93. TO_ROLLBACK[item].add(name)
  94. echo "Update service $name"
  95. sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service update \
  96. $name --image ${REGISTRIES.get((item))}/lab/$DOCKER_IMAGE:$VERSION_TO_UPDATE"
  97. }
  98. }
  99. catch (err) {
  100. def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
  101. TO_ROLLBACK.each { target, value ->
  102. for (service_name in value) {
  103. echo "Recoverig $service_name running in $target to $STABLE_VERSION"
  104. sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service rollback $service_name"
  105. }
  106. }
  107. ERROR_JOB = TO_ROLLBACK[item][-1]
  108. currentBuild.result = 'FAILURE'
  109. error ("Failure on update $ERROR_JOB. The service(s) $ERROR_JOB was rollback!")
  110. }
  111. }
  112. }
  113. }
  114. }
  115. stage("Update Tags") {
  116. steps {
  117. script {
  118. def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
  119. def OLD_STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version" , returnStdout: true).trim()
  120. sh "set +x && echo $STABLE_VERSION > $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
  121. sh "set +x && echo $VERSION_TO_UPDATE > $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version"
  122. }
  123. }
  124. }
  125. stage("Push New Versions To Git") {
  126. steps {
  127. script {
  128. sh "cat commit.txt"
  129. sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version"
  130. sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
  131. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  132. sh """cd $SWARM_GIT_NAME
  133. git add -A
  134. git config --global user.email "${JENKINS_MAIL}"
  135. git config --global user.name "Jenkins"
  136. git commit -F ../commit.txt
  137. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  138. git push origin master
  139. """
  140. }
  141. }
  142. }
  143. }
  144. }
  145. post {
  146. always {
  147. echo "CleaningUp work directory"
  148. deleteDir()
  149. }
  150. failure {
  151. mail charset: 'UTF-8',
  152. subject: "Jenkins build ERROR",
  153. mimeType: 'text/html',
  154. to: "${mailto}",
  155. body: "<b>ATTENTION!!!</b> <br> <b>Failure update:</b> $ERROR_JOB <br><b>Was rollback:</b> $TO_ROLLBACK <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>Build Number:</b> ${env.BUILD_NUMBER} <b><br>URL Build:</b> ${RUN_DISPLAY_URL}"
  156. }
  157. }
  158. }