Jenkinsfile 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. switch (cluster) {
  62. case 'prod':
  63. CLUSTER=CLUSTER_PROD
  64. break
  65. case 'dev':
  66. CLUSTER=CLUSTER_DEV
  67. break
  68. default:
  69. currentBuild.result = 'ABORTED'
  70. error('Unknown cluster')
  71. }
  72. for (String item : service_update.split()) {
  73. try{
  74. echo "Updating $item"
  75. withEnv(["service_name=${item}"]) {
  76. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  77. export DOCKER_HOST=tcp://${CLUSTER}:2376 DOCKER_TLS_VERIFY=1
  78. docker service update ${service_name} --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}
  79. '''
  80. }
  81. }
  82. catch(err){
  83. echo "Recovering service $item"
  84. withEnv(["service_name=${item}"]) {
  85. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  86. export DOCKER_HOST=tcp://${CLUSTER}:2376 DOCKER_TLS_VERIFY=1
  87. docker service rollback ${service_name}
  88. '''
  89. }
  90. throw err
  91. }
  92. }
  93. }
  94. }
  95. }
  96. stage("Tagging"){
  97. steps{
  98. echo "Setting latest tag"
  99. sh '''docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
  100. docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest'''
  101. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  102. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  103. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  104. git clone ${SWARM_GIT_URL}
  105. cd ${SWARM_GIT_NAME}
  106. echo -n ${NEWTAG} > tags/${DOCKER_IMAGE}.version
  107. git add -A
  108. git config --global user.email "${JENKINS_MAIL}"
  109. git config --global user.name "Jenkins"
  110. git commit -m 'Version update'
  111. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  112. git push origin master
  113. ls'''
  114. }
  115. }
  116. }
  117. }
  118. post {
  119. always {
  120. echo "CleaningUp work diretory"
  121. deleteDir()
  122. }
  123. failure {
  124. mail charset: 'UTF-8',
  125. subject: "Jenkins build ERROR",
  126. mimeType: 'text/html',
  127. to: "${mailto}",
  128. 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}"
  129. }
  130. success {
  131. mail charset: 'UTF-8',
  132. subject: "Jenkins build SUSCCESS",
  133. mimeType: 'text/html',
  134. to: "${mailto}",
  135. 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}"
  136. }
  137. }
  138. }