updateToTargetVersionJenkinsfile 6.2 KB

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