Jenkinsfile 5.4 KB

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