updateVersionJenkinsfile 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 label=com.docker.stack.namespace=$STACK_LABEL -f label=ru.infoclinica.service=$SERVICE_LABEL --format '{{.Name}}'" , returnStdout: true).split('\n')
  72. switch (TASK_ACTION) {
  73. case 'up':
  74. echo "These services will be updated to version $VERSION_TO_UPDATE: ${LIST_SERVICE.get(item)}"
  75. break
  76. case 'down':
  77. echo "These services will be downgraded to version $VERSION_TO_UPDATE ${LIST_SERVICE.get(item)}"
  78. break
  79. }
  80. }
  81. }
  82. }
  83. }
  84. stage("Update Services") {
  85. steps {
  86. script {
  87. ENAMES.each { item ->
  88. TO_ROLLBACK[item] = []
  89. try {
  90. for (name in LIST_SERVICE.get(item)) {
  91. TO_ROLLBACK[item].add(name)
  92. echo "Update service $name"
  93. sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service update $name --image ${REGISTRIES.get((item))}/lab/$DOCKER_IMAGE:$VERSION_TO_UPDATE"
  94. }
  95. }
  96. catch (err) {
  97. def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
  98. TO_ROLLBACK.each { target, value ->
  99. for (service_name in value) {
  100. echo "Recoverig $service_name running in $target to $STABLE_VERSION"
  101. sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service rollback $service_name"
  102. }
  103. }
  104. ERROR_JOB = TO_ROLLBACK[item][-1]
  105. currentBuild.result = 'FAILURE'
  106. error ("Failure on update $ERROR_JOB. The service(s) $ERROR_JOB was rollback!")
  107. }
  108. }
  109. }
  110. }
  111. }
  112. stage("Update Tags") {
  113. steps {
  114. script {
  115. def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
  116. def OLD_STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version" , returnStdout: true).trim()
  117. sh "set +x && echo $STABLE_VERSION > $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
  118. sh "set +x && echo $VERSION_TO_UPDATE > $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version"
  119. }
  120. }
  121. }
  122. stage("Push New Versions To Git") {
  123. steps {
  124. script {
  125. sh "cat commit.txt"
  126. sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version"
  127. sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
  128. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  129. sh """cd $SWARM_GIT_NAME
  130. git add -A
  131. git config --global user.email "${JENKINS_MAIL}"
  132. git config --global user.name "Jenkins"
  133. git commit -F ../commit.txt
  134. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  135. git push origin master
  136. """
  137. }
  138. }
  139. }
  140. }
  141. }
  142. post {
  143. always {
  144. echo "CleaningUp work directory"
  145. deleteDir()
  146. }
  147. failure {
  148. mail charset: 'UTF-8',
  149. subject: "Jenkins build ERROR",
  150. mimeType: 'text/html',
  151. to: "${mailto}",
  152. 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}"
  153. }
  154. }
  155. }