updateToTargetVersionJenkinsfile 5.9 KB

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