updateToTargetVersionJenkinsfile 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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: "tomishinets.v@sdsys.ru",
  27. description: "Email which has to be notified."
  28. )
  29. string(
  30. name: "toVersion",
  31. defaultValue: "",
  32. description: "What version to update?"
  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. sh "set +x && echo \"Update version of Apps to $toVersion\" > commit.txt"
  44. }
  45. }
  46. }
  47. }
  48. stage("Get Names Of Running Services") {
  49. steps {
  50. script {
  51. ENAMES.each { item ->
  52. echo "Viewing: ${item} services, on ${CLUSTERS.get((item))}"
  53. 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')
  54. echo "These services will be updated $LIST_SERVICE to version $toVersion"
  55. }
  56. }
  57. }
  58. }
  59. stage("Update Services") {
  60. steps {
  61. script {
  62. ENAMES.each { item ->
  63. try {
  64. for (name in LIST_SERVICE) {
  65. TO_ROLLBACK.add(name)
  66. sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service update $name --image ${REGISTRIES.get((item))}/lab/$DOCKER_IMAGE:$toVersion"
  67. }
  68. }
  69. catch (err) {
  70. def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
  71. for (names_falure in TO_ROLLBACK) {
  72. echo "Recoverig $names_falure to $STABLE_VERSION"
  73. sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service rollback $names_falure"
  74. }
  75. ERROR_JOB = TO_ROLLBACK.last()
  76. currentBuild.result = 'FAILURE'
  77. error ("Failure on update $ERROR_JOB. The service $TO_ROLLBACK was rollback!")
  78. }
  79. }
  80. }
  81. }
  82. }
  83. stage("Update Tags") {
  84. steps {
  85. script {
  86. def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
  87. def OLD_STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version" , returnStdout: true).trim()
  88. sh "set +x && echo $STABLE_VERSION > $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
  89. sh "set +x && echo $VERSION_TO_UPDATE > $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version"
  90. }
  91. }
  92. }
  93. stage("Push New Versions To Git") {
  94. steps {
  95. script {
  96. sh "cat commit.txt"
  97. sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version"
  98. sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
  99. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  100. sh """cd $SWARM_GIT_NAME
  101. git add -A
  102. git config --global user.email "${JENKINS_MAIL}"
  103. git config --global user.name "Jenkins"
  104. git commit -F ../commit.txt
  105. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  106. git push origin master
  107. """
  108. }
  109. }
  110. }
  111. }
  112. }
  113. post {
  114. always {
  115. echo "CleaningUp work directory"
  116. deleteDir()
  117. }
  118. failure {
  119. mail charset: 'UTF-8',
  120. subject: "Jenkins build ERROR",
  121. mimeType: 'text/html',
  122. to: "${mailto}",
  123. 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}"
  124. }
  125. }
  126. }