Jenkinsfile 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. def NEWTAG
  2. pipeline {
  3. agent {
  4. label "swarm"
  5. }
  6. environment {
  7. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  8. DOCKER_IMAGE='admin'
  9. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  10. SWARM_GIT_NAME='stack-deploy'
  11. JENKINS_MAIL='jenkins@sdsys.ru'
  12. CLUSTER_DEV='dev-iru-swarm.infoclinica.lan'
  13. CLUSTER_PROD='iru-swarm.infoclinica.ru'
  14. }
  15. parameters {
  16. string(
  17. name: "repo",
  18. defaultValue: "prod",
  19. description: "Repository to build and/or deploy from. Values - dev, prod."
  20. )
  21. string(
  22. name: "cluster",
  23. defaultValue: "dev",
  24. description: "Cluster to deploy. Values - dev, prod."
  25. )
  26. string(
  27. name: "service_update",
  28. defaultValue: "info_admin",
  29. description: "Service name to update."
  30. )
  31. string(
  32. name: "mailto",
  33. defaultValue: "admin@sdsys.ru",
  34. description: "Email which has to be notified."
  35. )
  36. }
  37. stages {
  38. stage("Build") {
  39. steps {
  40. echo "Building ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
  41. sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
  42. script {
  43. NEWTAG = sh script: "docker run --rm ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} rpm -q --qf %{VERSION}-%{RELEASE} web-registry-admin 2> /dev/null", returnStdout: true
  44. NEWTAG = NEWTAG + BUILD_NUMBER
  45. echo "NEWTAG is $NEWTAG"
  46. }
  47. }
  48. }
  49. stage("Publish") {
  50. steps {
  51. echo "Tag is $NEWTAG"
  52. error "Stop pipeline"
  53. echo "Publishing ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  54. sh "docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  55. }
  56. }
  57. stage("Update") {
  58. when { expression { service_update != "" } }
  59. steps {
  60. script {
  61. for (String item : service_update.split()) {
  62. try{
  63. echo "Updating $item"
  64. if (cluster == 'prod') {
  65. withEnv(["service_name=${item}"]) {
  66. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  67. export DOCKER_HOST=tcp://${CLUSTER_PROD}:2376 DOCKER_TLS_VERIFY=1
  68. docker service update ${service_name} --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}
  69. '''
  70. }
  71. }
  72. if (cluster == 'dev') {
  73. withEnv(["service_name=${item}"]) {
  74. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  75. export DOCKER_HOST=tcp://${CLUSTER_DEV}:2376 DOCKER_TLS_VERIFY=1
  76. docker service update ${service_name} --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}
  77. '''
  78. }
  79. }
  80. }
  81. catch(err){
  82. echo "Recovering service $item"
  83. if (cluster == 'prod') {
  84. withEnv(["service_name=${item}"]) {
  85. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  86. export DOCKER_HOST=tcp://${CLUSTER_PROD}:2376 DOCKER_TLS_VERIFY=1
  87. docker service rollback ${service_name}
  88. '''
  89. }
  90. }
  91. if (cluster == 'dev') {
  92. withEnv(["service_name=${item}"]) {
  93. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  94. export DOCKER_HOST=tcp://${CLUSTER_DEV}:2376 DOCKER_TLS_VERIFY=1
  95. docker service rollback ${service_name}
  96. '''
  97. }
  98. }
  99. throw err
  100. }
  101. }
  102. }
  103. }
  104. }
  105. stage("Tagging"){
  106. steps{
  107. echo "Setting latest tag"
  108. sh '''docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
  109. docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest'''
  110. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  111. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  112. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  113. git clone ${SWARM_GIT_URL}
  114. cd ${SWARM_GIT_NAME}
  115. echo -n ${repo}-${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.version
  116. git add -A
  117. git config --global user.email "${JENKINS_MAIL}"
  118. git config --global user.name "Jenkins"
  119. git commit -m 'Version update'
  120. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  121. git push origin master
  122. ls'''
  123. }
  124. }
  125. }
  126. }
  127. post {
  128. always {
  129. echo "CleaningUp work diretory"
  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> <b><br> Jenkins job failed.\n\n <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>\nBuild Number:</b> ${env.BUILD_NUMBER} <b><br>\nURL Build:</b> ${RUN_DISPLAY_URL}"
  138. }
  139. success {
  140. mail charset: 'UTF-8',
  141. subject: "Jenkins build SUSCCESS",
  142. mimeType: 'text/html',
  143. to: "${mailto}",
  144. body: "<b>Congradulations!!!</b> <b><br> Jenkins job succefully finished.\n\n <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>\nBuild Number:</b> ${env.BUILD_NUMBER} <b><br>\nURL Build:</b> ${RUN_DISPLAY_URL}"
  145. }
  146. }
  147. }