Jenkinsfile-dev 5.1 KB

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