Jenkinsfile-dev 5.2 KB

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