Jenkinsfile 5.3 KB

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