updateVersionJenkinsfile 7.2 KB

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