Jenkinsfile-prod 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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: "mailto",
  26. defaultValue: "admin@sdsys.ru",
  27. description: "Email which has to be notified."
  28. )
  29. }
  30. stages {
  31. stage("Calculate Variables") {
  32. steps {
  33. script {
  34. switch (GIT_REPO) {
  35. case 'promo-sdk':
  36. REPO=PROMO_SDK.repo
  37. DOCKER_IMAGE=PROMO_SDK.image
  38. SERVICE_NAME=PROMO_SDK.service_name
  39. DOCKER_FILE=PROMO_SDK.dockerfile
  40. break
  41. case 'promo-seo':
  42. REPO=PROMO_SEO.repo
  43. DOCKER_IMAGE=PROMO_SEO.image
  44. SERVICE_NAME=PROMO_SEO.service_name
  45. DOCKER_FILE=PROMO_SEO.dockerfile
  46. break
  47. }
  48. echo REPO
  49. echo DOCKER_IMAGE
  50. echo SERVICE_NAME
  51. echo DOCKER_FILE
  52. }
  53. }
  54. }
  55. stage("Tagging in Git"){
  56. steps{
  57. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  58. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  59. git clone ${SWARM_GIT_URL}
  60. cd ${SWARM_GIT_NAME}
  61. cp -f tags/${DOCKER_IMAGE}.dev-version tags/${DOCKER_IMAGE}.version
  62. git add -A
  63. git config --global user.email "${JENKINS_MAIL}"
  64. git config --global user.name "Jenkins"
  65. git commit -m 'Version update'| grep '' # Shield ourself from non-zero exit code from 'git commit' command
  66. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  67. git push origin master
  68. """
  69. }
  70. }
  71. }
  72. stage("Tagging and pushing to prod registry"){
  73. steps{
  74. script {
  75. NEWTAG=sh (script: "cat ${SWARM_GIT_NAME}/tags/${DOCKER_IMAGE}.version", returnStdout: true).trim()
  76. sh """docker pull ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  77. docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  78. docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${PROD_REGISTRY}/${DOCKER_IMAGE}:latest
  79. docker push ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  80. docker push ${PROD_REGISTRY}/${DOCKER_IMAGE}:latest
  81. """
  82. }
  83. }
  84. }
  85. stage("Updating service"){
  86. steps{
  87. script {
  88. echo "Updating ${SERVICE_NAME}"
  89. try{
  90. sh "DOCKER_HOST=${DHOST} docker service update ${SERVICE_NAME} --image ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  91. }
  92. catch(err){
  93. echo "Recovering service ${SERVICE_NAME}"
  94. sh "DOCKER_HOST=${DHOST} docker service rollback ${SERVICE_NAME}"
  95. throw err
  96. }
  97. }
  98. }
  99. }
  100. }
  101. post {
  102. always {
  103. echo "CleaningUp work diretory"
  104. deleteDir()
  105. sh "docker image rm -f `docker image ls -q ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}`"
  106. }
  107. failure {
  108. mail charset: 'UTF-8',
  109. subject: "Jenkins build ERROR",
  110. mimeType: 'text/html',
  111. to: "${mailto}",
  112. 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}"
  113. }
  114. }
  115. }