Jenkinsfile-dev 5.7 KB

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