Jenkinsfile-dev 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. options {
  8. disableConcurrentBuilds()
  9. timeout(time: 20, unit: 'MINUTES')
  10. }
  11. environment {
  12. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  13. SWARM_GIT_NAME='stack-deploy'
  14. JENKINS_MAIL='jenkins@sdsys.ru'
  15. PACKAGE_NAME='web-registry-promo'
  16. DOCKER_CERT_PATH='/run/secrets/swarm'
  17. DHOST='tcp://dev-iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
  18. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  19. NEWTAG=''
  20. }
  21. parameters {
  22. choice (
  23. choices: 'promo-seo\npromo-sdk',
  24. description: 'Which repo to use?',
  25. name: 'GIT_REPO')
  26. string(
  27. name: "branch",
  28. defaultValue: "release",
  29. description: "Which branch to use?"
  30. )
  31. string(
  32. name: "mailto",
  33. defaultValue: "admin@sdsys.ru",
  34. description: "Email which has to be notified."
  35. )
  36. }
  37. stages {
  38. stage("Calculate Variables") {
  39. steps {
  40. script {
  41. switch (GIT_REPO) {
  42. case 'promo-sdk':
  43. REPO=PROMO_SDK.repo
  44. DOCKER_IMAGE=PROMO_SDK.image
  45. SERVICE_NAME=PROMO_SDK.service_name
  46. DOCKER_FILE=PROMO_SDK.dockerfile
  47. break
  48. case 'promo-seo':
  49. REPO=PROMO_SEO.repo
  50. DOCKER_IMAGE=PROMO_SEO.image
  51. SERVICE_NAME=PROMO_SEO.service_name
  52. DOCKER_FILE=PROMO_SEO.dockerfile
  53. break
  54. }
  55. echo REPO
  56. echo DOCKER_IMAGE
  57. echo SERVICE_NAME
  58. echo DOCKER_FILE
  59. }
  60. }
  61. }
  62. stage("Build") {
  63. steps {
  64. script {
  65. echo "Building ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}."
  66. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  67. sh """set +x
  68. cat ${GIT_SSH_KEY} > ${WORKSPACE}/id_rsa
  69. chmod 600 ${WORKSPACE}/id_rsa
  70. """
  71. }
  72. sh "docker build --build-arg branch=${branch} --build-arg git_repo=${REPO} --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} -f ./${DOCKER_FILE} ."
  73. NEWTAG = sh(script: "docker run --rm ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} cat /tmp/version 2> /dev/null", returnStdout: true).trim()
  74. NEWTAG = branch + "-" + NEWTAG + "_" + BUILD_NUMBER
  75. echo "NEWTAG is $NEWTAG"
  76. sh "docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  77. }
  78. }
  79. }
  80. stage("Publish") {
  81. steps {
  82. echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  83. sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  84. }
  85. }
  86. stage("Update") {
  87. steps {
  88. script {
  89. try{
  90. sh "DOCKER_HOST=${DHOST} docker service update ${SERVICE_NAME} --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  91. }
  92. catch(err){
  93. echo "Recovering service $item"
  94. sh "DOCKER_HOST=${DHOST} docker service rollback ${SERVICE_NAME}"
  95. throw err
  96. }
  97. }
  98. }
  99. }
  100. stage("Tagging"){
  101. steps{
  102. echo "Setting latest tag"
  103. sh """docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest
  104. docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest"""
  105. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  106. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  107. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  108. git clone ${SWARM_GIT_URL}
  109. cd ${SWARM_GIT_NAME}
  110. echo -n ${NEWTAG} > tags/${DOCKER_IMAGE}.dev-version
  111. git add -A
  112. git config --global user.email "${JENKINS_MAIL}"
  113. git config --global user.name "Jenkins"
  114. git commit -m 'Version update'
  115. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  116. git push origin master
  117. """
  118. }
  119. }
  120. }
  121. }
  122. post {
  123. always {
  124. echo "CleaningUp work diretory"
  125. deleteDir()
  126. sh "docker image rm -f `docker image ls -q ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}`"
  127. }
  128. failure {
  129. mail charset: 'UTF-8',
  130. subject: "Jenkins build ERROR",
  131. mimeType: 'text/html',
  132. to: "${mailto}",
  133. 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}"
  134. }
  135. }
  136. }