updateVersionJenkinsfile 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // def ENAMES = [ 'prod', 'dev' ]
  2. def ENAMES = [ 'prod' ]
  3. def CLUSTERS = ['prod': 'iru-swarm.infoclinica.lan', 'dev': 'dev-iru-swarm.infoclinica.lan']
  4. def REGISTRIES = ['prod': 'registry.infoclinica.ru:5000', 'dev': 'dev-registry.infoclinica.ru:5000']
  5. def DHOST = ['prod': 'tcp://iru-swarm.infoclinica.lan:2376', 'dev': 'tcp://dev-iru-swarm.infoclinica.lan:2376']
  6. def LIST_SERVICE = []
  7. def TO_ROLLBACK = []
  8. def ERROR_JOB
  9. def VERSION_TO_UPDATE
  10. pipeline {
  11. agent {
  12. label "swarm"
  13. }
  14. environment {
  15. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/stack-deploy.git'
  16. SWARM_GIT_NAME='stack-deploy'
  17. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  18. DOCKER_CERT_PATH='/run/secrets/swarm'
  19. DOCKER_IMAGE='analis-wineservice'
  20. SERVICE_LABEL='analis-std'
  21. STACK_LABEL='analis-wineservice'
  22. }
  23. parameters {
  24. string(
  25. name: "mailto",
  26. defaultValue: "admin@sdsys.ru",
  27. description: "Email which has to be notified."
  28. )
  29. choice (
  30. choices: 'up\ndown',
  31. description: 'What you want? Up version or down?',
  32. name: 'TASK_ACTION'
  33. )
  34. }
  35. stages {
  36. stage("Git Pull. Define Version to Update") {
  37. steps {
  38. script {
  39. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  40. sh """set +x && GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  41. git clone ${SWARM_GIT_URL}
  42. """
  43. }
  44. switch (TASK_ACTION) {
  45. case 'up':
  46. VERSION_TO_UPDATE = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.build-version" , returnStdout: true).trim()
  47. sh "set +x && echo \"Update version of Apps to $VERSION_TO_UPDATE\" > commit.txt"
  48. break
  49. case 'down':
  50. VERSION_TO_UPDATE = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version" , returnStdout: true).trim()
  51. sh "set +x && echo \"Downgrade version of Apps to $VERSION_TO_UPDATE\" > commit.txt"
  52. break
  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 = 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. switch (TASK_ACTION) {
  64. case 'up':
  65. echo "These services will be updated $LIST_SERVICE to version $VERSION_TO_UPDATE"
  66. break
  67. case 'down':
  68. echo "These services will be downgraded $LIST_SERVICE to version $VERSION_TO_UPDATE"
  69. break
  70. }
  71. }
  72. }
  73. }
  74. }
  75. stage("Update Services") {
  76. steps {
  77. script {
  78. ENAMES.each { item ->
  79. try {
  80. for (name in LIST_SERVICE) {
  81. TO_ROLLBACK.add(name)
  82. 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"
  83. }
  84. }
  85. catch (err) {
  86. def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
  87. for (names_falure in TO_ROLLBACK) {
  88. echo "Recoverig $names_falure to $STABLE_VERSION"
  89. sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service rollback $names_falure"
  90. }
  91. ERROR_JOB = TO_ROLLBACK.last()
  92. currentBuild.result = 'FAILURE'
  93. error ("Failure on update $ERROR_JOB. The service $TO_ROLLBACK 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. }