updateK8s.Jenkinsfile 2.8 KB

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