Jenkinsfile 5.1 KB

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