devK8s.Jenkinsfile 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. PROMO_SDK=[ service_name: 'info_promo-sdk', image: 'web-registration/promo-sdk', repo: 'git@github.com:icruportal/web-sdk-demo.git', dockerfile: 'Dockerfile-promo-sdk' ]
  2. PROMO_SEO=[ service_name: 'info_promo-seo', image: 'web-registration/promo-seo', repo: 'git@github.com:icruportal/web-registration.git', dockerfile: 'Dockerfile-promo-seo' ]
  3. @Library('jenkins-library-iru@master') _
  4. def Label = "web-registration-promo-build-${UUID.randomUUID().toString()}"
  5. pipeline {
  6. options {
  7. buildDiscarder logRotator(numToKeepStr: '10')
  8. disableConcurrentBuilds()
  9. timeout(time: 15, unit: 'MINUTES')
  10. }
  11. agent {
  12. kubernetes {
  13. label Label
  14. yaml libraryResource('promoPodDefinition.yaml')
  15. }
  16. }
  17. environment {
  18. CONTEXT='provision@sdsys.office'
  19. HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/k8s.git'
  20. HELM_REPO='https://images.sdsys.ru/artifactory/helm/'
  21. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  22. DOCKER_REGISTRY='images.sdsys.ru'
  23. VALUES_FILE = ''
  24. SOURCE_DIR=''
  25. HELM_DIR=''
  26. TAG=''
  27. }
  28. parameters {
  29. choice (
  30. choices: 'promo-seo\npromo-sdk',
  31. description: 'Which repo to use?',
  32. name: 'APP_NAME'
  33. )
  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 (APP_NAME) {
  50. case 'promo-sdk':
  51. SOURCE_GIT_URL = PROMO_SDK.repo
  52. DOCKER_IMAGE = PROMO_SDK.image
  53. SERVICE_NAME = PROMO_SDK.service_name
  54. DOCKER_FILE = PROMO_SDK.dockerfile
  55. BUILD_IMAGE_DIR = dirOps.calculateDir(SOURCE_GIT_URL)
  56. break
  57. case 'promo-seo':
  58. SOURCE_GIT_URL = PROMO_SEO.repo
  59. DOCKER_IMAGE = PROMO_SEO.image
  60. SERVICE_NAME = PROMO_SEO.service_name
  61. DOCKER_FILE = PROMO_SEO.dockerfile
  62. BUILD_IMAGE_DIR = dirOps.calculateDir(SOURCE_GIT_URL) + "/promo/build/libs"
  63. break
  64. }
  65. }
  66. }
  67. }
  68. stage("Prepare to build") {
  69. steps {
  70. script {
  71. echo "Pull ${SOURCE_GIT_URL}"
  72. gitOps.clone(SOURCE_GIT_URL)
  73. echo "CheckOut ${branch}"
  74. SOURCE_DIR = dirOps.calculateDir(SOURCE_GIT_URL)
  75. gitOps.checkout(SOURCE_DIR, branch)
  76. echo "Save Version"
  77. dir(SOURCE_DIR) {
  78. COMMIT = sh(script: 'echo $(git log -p -1 --pretty=format:"%h"|head -n1)', returnStdout: true).trim()
  79. }
  80. HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
  81. CHART_PATH = "helm/" + DOCKER_IMAGE.split("/")[0]
  82. VALUES_FILE = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[0] + ".yaml"
  83. }
  84. }
  85. }
  86. stage("Build APP") {
  87. steps {
  88. script {
  89. switch (APP_NAME) {
  90. case 'promo-sdk':
  91. echo "Build APP PROMO-SDK"
  92. dir("${SOURCE_DIR}") {
  93. buildOps.npmPromoSDK()
  94. }
  95. break
  96. case 'promo-seo':
  97. echo "Build APP PROMO-SEO"
  98. echo "Build customer-api"
  99. dir("${SOURCE_DIR}/customer-api") {
  100. buildOps.maven()
  101. }
  102. echo "Build public"
  103. dir("${SOURCE_DIR}/promo/src/public") {
  104. buildOps.npm()
  105. }
  106. echo "Build Promo"
  107. dir("${SOURCE_DIR}/promo") {
  108. buildOps.gradle()
  109. }
  110. }
  111. }
  112. }
  113. }
  114. stage("Build Image") {
  115. steps {
  116. script {
  117. IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${COMMIT}-${BUILD_NUMBER}"
  118. k8sBuildImage.kaniko(IMAGE_TO_PUSH,[CONTENT_DIR: BUILD_IMAGE_DIR],DOCKER_FILE)
  119. }
  120. }
  121. }
  122. stage("Update Helm Chart dev.yaml") {
  123. steps {
  124. script {
  125. TAG_PATH = "images." + DOCKER_IMAGE.split("/")[1].replace('-', '_')
  126. echo "Update ${TAG_PATH} to ${IMAGE_TO_PUSH} in ${VALUES_FILE}"
  127. gitOps.clone(HELM_GIT_URL)
  128. def data = readYaml file: "${VALUES_FILE}"
  129. yamlOps.updateBuilder(TAG_PATH, data, IMAGE_TO_PUSH)
  130. writeYaml file: "${VALUES_FILE}", data: data, overwrite: true
  131. }
  132. }
  133. }
  134. stage("Update APP") {
  135. steps {
  136. script {
  137. try {
  138. echo "Update APP"
  139. k8sHelmOps.upgrade(CONTEXT, DOCKER_IMAGE.split("/")[0], DOCKER_IMAGE.split("/")[0], CHART_PATH, VALUES_FILE, HELM_REPO)
  140. }
  141. catch(err) {
  142. echo "Recovering APP"
  143. k8sHelmOps.rollback(CONTEXT, DOCKER_IMAGE.split("/")[0], DOCKER_IMAGE.split("/")[0])
  144. throw err
  145. }
  146. }
  147. }
  148. }
  149. stage("Push changes to REPO") {
  150. steps {
  151. script {
  152. echo "Push changes to Repo"
  153. COMMIT = "Version update " + DOCKER_IMAGE.split("/")[1] + " to " + COMMIT
  154. gitOps.push(HELM_DIR, COMMIT)
  155. }
  156. }
  157. }
  158. }
  159. post {
  160. failure {
  161. mail charset: 'UTF-8',
  162. subject: "Jenkins build ERROR",
  163. mimeType: 'text/html',
  164. to: "${mailto}",
  165. 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}"
  166. }
  167. }
  168. }