updateVersionJenkinsfile 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. }
  21. parameters {
  22. string(
  23. name: "mailto",
  24. defaultValue: "tomishinets.v@sdsys.ru",
  25. description: "Email which has to be notified."
  26. )
  27. choice (
  28. choices: 'up\ndown',
  29. description: 'What you want? Up version or down?',
  30. name: 'TASK_ACTION'
  31. )
  32. }
  33. stages {
  34. stage("Check Stable Version") {
  35. steps {
  36. script {
  37. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  38. sh """set +x && GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  39. git clone ${SWARM_GIT_URL}
  40. """
  41. }
  42. switch (TASK_ACTION) {
  43. case 'up':
  44. VERSION_TO_UPDATE = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.build-version" , returnStdout: true).trim()
  45. sh "set +x && echo \"Update version of Apps to $VERSION_TO_UPDATE\" > commit.txt"
  46. break
  47. case 'down':
  48. VERSION_TO_UPDATE = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version" , returnStdout: true).trim()
  49. sh "set +x && echo \"Downgrade version of Apps to $VERSION_TO_UPDATE\" > commit.txt"
  50. break
  51. }
  52. }
  53. }
  54. }
  55. stage("Get Names Of Running Services") {
  56. steps {
  57. script {
  58. ENAMES.each { item ->
  59. echo "Viewing: ${item} services, on ${CLUSTERS.get((item))}"
  60. LIST_SERVICE = sh (script: "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service ls -f label=com.docker.stack.namespace=analis-wineservice --format '{{.Name}}'" , returnStdout: true).split('\n')
  61. switch (TASK_ACTION) {
  62. case 'up':
  63. echo "These services will be updated $LIST_SERVICE to version $VERSION_TO_UPDATE"
  64. break
  65. case 'down':
  66. echo "These services will be downgraded $LIST_SERVICE to version $VERSION_TO_UPDATE"
  67. break
  68. }
  69. }
  70. }
  71. }
  72. }
  73. stage("Update Services") {
  74. steps {
  75. script {
  76. ENAMES.each { item ->
  77. try {
  78. for (name in LIST_SERVICE) {
  79. TO_ROLLBACK.add(name)
  80. 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"
  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. for (names_falure in TO_ROLLBACK) {
  86. echo "Recoverig $names_falure to $STABLE_VERSION"
  87. sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service rollback $names_falure"
  88. }
  89. ERROR_JOB = TO_ROLLBACK.last()
  90. currentBuild.result = 'FAILURE'
  91. error ("Failure on update $ERROR_JOB. The service $TO_ROLLBACK was rollback!")
  92. }
  93. }
  94. }
  95. }
  96. }
  97. stage("Update Tags") {
  98. steps {
  99. script {
  100. def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
  101. def OLD_STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version" , returnStdout: true).trim()
  102. sh "set +x && echo $STABLE_VERSION > $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
  103. sh "set +x && echo $VERSION_TO_UPDATE > $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version"
  104. }
  105. }
  106. }
  107. stage("Push New Versions To Git") {
  108. steps {
  109. script {
  110. sh "cat commit.txt"
  111. sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version"
  112. sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
  113. /* withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  114. sh """cd $SWARM_GIT_NAME
  115. git add -A
  116. git config --global user.email "${JENKINS_MAIL}"
  117. git config --global user.name "Jenkins"
  118. git commit -F ../commit.txt
  119. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  120. git push origin master
  121. """
  122. }
  123. */ }
  124. }
  125. }
  126. }
  127. post {
  128. always {
  129. echo "CleaningUp work directory"
  130. deleteDir()
  131. }
  132. failure {
  133. mail charset: 'UTF-8',
  134. subject: "Jenkins build ERROR",
  135. mimeType: 'text/html',
  136. to: "${mailto}",
  137. 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}"
  138. }
  139. }
  140. }