prodK8s.Jenkinsfile 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. @Library('jenkins-library@master') _
  2. def Label = "admin-build-${UUID.randomUUID().toString()}"
  3. pipeline {
  4. options {
  5. buildDiscarder logRotator(numToKeepStr: '10')
  6. disableConcurrentBuilds()
  7. }
  8. agent {
  9. kubernetes {
  10. label Label
  11. yaml libraryResource('adminPodDefinition.yaml')
  12. }
  13. }
  14. environment {
  15. CLOUD='cloud-it-k8s'
  16. DOCKER_IMAGE='iru/node'
  17. CHART_PATH='helm/info'
  18. NAMESPACE='info'
  19. HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/k8s.git'
  20. APP_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/node.git'
  21. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  22. DOCKER_REGISTRY='jcr.infoclinica.ru'
  23. CHART_NAME=''
  24. APP_DIR=''
  25. HELM_DIR=''
  26. TAG=''
  27. }
  28. parameters {
  29. string(
  30. name: "mailto",
  31. defaultValue: "admin@sdsys.ru",
  32. description: "Email which has to be notified."
  33. )
  34. }
  35. stages {
  36. stage("Prepare to Update") {
  37. steps {
  38. script {
  39. echo "Pull Repo"
  40. gitOps.clone(HELM_GIT_URL)
  41. HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
  42. }
  43. }
  44. }
  45. stage("Update Helm Chart values.yaml") {
  46. steps {
  47. script {
  48. echo "Update values.yaml"
  49. APP_DIR = dirOps.calculateDir(APP_GIT_URL)
  50. def data = readYaml file: "${HELM_DIR}/${CHART_PATH}/values.yaml"
  51. TAG_PATH = "images.${APP_DIR}.dev"
  52. TAG = yamlOps.getBuilder(TAG_PATH, data)
  53. TAG_PATH = "images.${APP_DIR}.prod"
  54. yamlOps.updateBuilder(TAG_PATH, data, TAG)
  55. writeYaml file: "${HELM_DIR}/${CHART_PATH}/values.yaml", data: data, overwrite: true
  56. }
  57. }
  58. }
  59. stage("Update APP") {
  60. steps {
  61. script {
  62. CHART = "${HELM_DIR}/${CHART_PATH}"
  63. CHART_NAME = CHART_PATH.split("/")[1]
  64. try {
  65. echo "Update APP"
  66. k8sHelmOps.upgrade(CLOUD, NAMESPACE, CHART_NAME, CHART, 'prod')
  67. }
  68. catch(err) {
  69. echo "Recovering APP"
  70. k8sHelmOps.rollback(CLOUD, NAMESPACE, CHART_NAME)
  71. throw err
  72. }
  73. }
  74. }
  75. }
  76. stage("Push changes to REPO") {
  77. steps {
  78. script {
  79. echo "Push changes to Repo"
  80. COMMIT = "Version update ${APP_DIR}"
  81. gitOps.push(HELM_DIR, COMMIT)
  82. }
  83. }
  84. }
  85. stage("Tagging Image") {
  86. steps {
  87. script {
  88. echo "Tagging ${TAG} to latest"
  89. IMAGE = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
  90. k8sBuildImage.crane(IMAGE, TAG, 'latest')
  91. }
  92. }
  93. }
  94. }
  95. post {
  96. failure {
  97. mail charset: 'UTF-8',
  98. subject: "Jenkins build ERROR",
  99. mimeType: 'text/html',
  100. to: "${mailto}",
  101. 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}"
  102. }
  103. }
  104. }