Jenkinsfile-prod 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. PROMO_SDK=[ service_name: 'info_promo-sdk', image: 'iru/promo-sdk', repo: 'git@github.com:icruportal/web-sdk-demo.git', dockerfile: 'Dockerfile-promo-sdk' ]
  2. PROMO_SEO=[ service_name: 'info_promo-seo', image: 'iru/promo-seo', repo: 'git@github.com:icruportal/web-registration.git', dockerfile: 'Dockerfile-promo-seo' ]
  3. pipeline {
  4. agent {
  5. label "swarm"
  6. }
  7. environment {
  8. DOCKER_IMAGE='iru/promo'
  9. DOCKER_CERT_PATH='/run/secrets/swarm'
  10. DHOST='tcp://iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
  11. DEV_REGISTRY='dev-registry.infoclinica.ru:5000'
  12. PROD_REGISTRY='registry.infoclinica.ru:5000'
  13. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  14. SWARM_GIT_NAME='stack-deploy'
  15. JENKINS_MAIL='jenkins@sdsys.ru'
  16. NEWTAG=''
  17. }
  18. parameters {
  19. choice (
  20. choices: 'promo-sdk\npromo-seo',
  21. description: 'Which repo to use?',
  22. name: 'GIT_REPO'
  23. )
  24. string(
  25. name: "service_update",
  26. defaultValue: "info_promo",
  27. description: "Services to update."
  28. )
  29. string(
  30. name: "mailto",
  31. defaultValue: "admin@sdsys.ru",
  32. description: "Email which has to be notified."
  33. )
  34. }
  35. stages {
  36. stage("Calculate Variables") {
  37. steps {
  38. script {
  39. switch (GIT_REPO) {
  40. case 'promo-sdk':
  41. REPO=PROMO_SDK.repo
  42. DOCKER_IMAGE=PROMO_SDK.image
  43. SERVICE_NAME=PROMO_SDK.service_name
  44. DOCKER_FILE=PROMO_SDK.dockerfile
  45. break
  46. case 'promo-seo':
  47. REPO=PROMO_SEO.repo
  48. DOCKER_IMAGE=PROMO_SEO.image
  49. SERVICE_NAME=PROMO_SEO.service_name
  50. DOCKER_FILE=PROMO_SEO.dockerfile
  51. break
  52. }
  53. echo REPO
  54. echo DOCKER_IMAGE
  55. echo SERVICE_NAME
  56. echo DOCKER_FILE
  57. }
  58. }
  59. }
  60. stage("Tagging in Git"){
  61. steps{
  62. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  63. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  64. git clone ${SWARM_GIT_URL}
  65. cd ${SWARM_GIT_NAME}
  66. cp -f tags/${DOCKER_IMAGE}.dev-version tags/${DOCKER_IMAGE}.version
  67. git add -A
  68. git config --global user.email "${JENKINS_MAIL}"
  69. git config --global user.name "Jenkins"
  70. git commit -m 'Version update'| grep '' # Shield ourself from non-zero exit code from 'git commit' command
  71. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  72. git push origin master
  73. """
  74. }
  75. }
  76. }
  77. stage("Tagging and pushing to prod registry"){
  78. steps{
  79. script {
  80. NEWTAG=sh (script: "cat ${SWARM_GIT_NAME}/tags/${DOCKER_IMAGE}.version", returnStdout: true).trim()
  81. sh """docker pull ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  82. docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  83. docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${PROD_REGISTRY}/${DOCKER_IMAGE}:latest
  84. docker push ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  85. docker push ${PROD_REGISTRY}/${DOCKER_IMAGE}:latest
  86. """
  87. }
  88. }
  89. }
  90. stage("Updating service"){
  91. steps{
  92. script {
  93. echo "Updating ${item}"
  94. try{
  95. sh "DOCKER_HOST=${DHOST} docker service update ${SERVICE_NAME} --image ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  96. }
  97. catch(err){
  98. echo "Recovering service $item"
  99. sh "DOCKER_HOST=${DHOST} docker service rollback ${SERVICE_NAME}"
  100. throw err
  101. }
  102. }
  103. }
  104. }
  105. }
  106. post {
  107. always {
  108. echo "CleaningUp work diretory"
  109. deleteDir()
  110. sh "docker image rm -f `docker image ls -q ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}`"
  111. }
  112. failure {
  113. mail charset: 'UTF-8',
  114. subject: "Jenkins build ERROR",
  115. mimeType: 'text/html',
  116. to: "${mailto}",
  117. 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}"
  118. }
  119. }
  120. }