updateK8s.Jenkinsfile 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. @Library('jenkins-library-iru@master') _
  2. def Label = "analis-lab-update-${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. CONTEXT='provision@sdsys.office'
  17. DOCKER_IMAGE='web-laboratory/analis'
  18. HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/k8s.git'
  19. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  20. DOCKER_REGISTRY='images.sdsys.ru'
  21. CHART_NAME=''
  22. CHART_PATH=''
  23. PROJECT=''
  24. HELM_DIR=''
  25. TAG=''
  26. }
  27. parameters {
  28. string(
  29. name: "mailto",
  30. defaultValue: "admin@sdsys.ru",
  31. description: "Email which has to be notified."
  32. )
  33. }
  34. stages {
  35. stage("Prepare to Update") {
  36. steps {
  37. script {
  38. echo "Pull Repo"
  39. gitOps.clone(HELM_GIT_URL)
  40. HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
  41. CHART_PATH = "helm/" + DOCKER_IMAGE.split("/")[0]
  42. VALUES_FILE_DEV = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[1] + ".yaml"
  43. VALUES_FILE_PROD = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[1] + ".yaml"
  44. echo "VALUES_FILE_DEV = ${VALUES_FILE_DEV}"
  45. echo "VALUES_FILE_PROD = ${VALUES_FILE_PROD}"
  46. }
  47. }
  48. }
  49. stage("Update Helm Chart values.yaml") {
  50. steps {
  51. script {
  52. echo "Update values.yaml"
  53. def data_build = readYaml file: VALUES_FILE_DEV
  54. TAG_PATH_BUILD = "images." + DOCKER_IMAGE.split("/")[1] + ".build"
  55. TAG = yamlOps.getBuilder(TAG_PATH_BUILD, data_build)
  56. def data_stable = readYaml file: VALUES_FILE_PROD
  57. TAG_PATH_STABLE = "images." + DOCKER_IMAGE.split("/")[1] + ".stable"
  58. yamlOps.updateBuilder(TAG_PATH_STABLE, data_stable, TAG)
  59. writeYaml file: VALUES_FILE_PROD, data: data_stable, overwrite: true
  60. }
  61. }
  62. }
  63. stage("Update APP") {
  64. steps {
  65. script {
  66. RELEASENAME = DOCKER_IMAGE.split("/")[0]
  67. NAMESPACE = DOCKER_IMAGE.split("/")[0]
  68. IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${TAG}"
  69. try {
  70. echo "Update APP"
  71. k8sHelmOps.upgrade(CONTEXT, NAMESPACE, RELEASENAME, CHART_PATH, VALUES_FILE_PROD, HELM_REPO)
  72. echo "Update success!!!"
  73. }
  74. catch(err) {
  75. echo "Recovering APP"
  76. k8sHelmOps.rollback(CONTEXT, NAMESPACE, RELEASENAME)
  77. throw err
  78. }
  79. }
  80. }
  81. }
  82. stage("Push changes to REPO") {
  83. steps {
  84. script {
  85. echo "Push changes to Repo"
  86. PROJECT = DOCKER_IMAGE.split("/")[1]
  87. COMMIT = "Version update ${PROJECT} to ${TAG}"
  88. gitOps.push(HELM_DIR, COMMIT)
  89. }
  90. }
  91. }
  92. // stage("Tagging Image") {
  93. // steps {
  94. // script {
  95. // echo "Tagging ${TAG} to latest"
  96. // IMAGE = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
  97. // k8sBuildImage.crane(IMAGE, TAG, 'latest')
  98. // }
  99. // }
  100. // }
  101. }
  102. post {
  103. failure {
  104. mail charset: 'UTF-8',
  105. subject: "Jenkins build ERROR",
  106. mimeType: 'text/html',
  107. to: "${mailto}",
  108. 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}"
  109. }
  110. }
  111. }