updateToTargetVersionJenkinsfile 5.8 KB

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