updateK8s.Jenkinsfile 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. HELM_REPO='https://images.sdsys.ru/artifactory/helm/'
  22. CHART_NAME=''
  23. CHART_PATH=''
  24. PROJECT=''
  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. CHART_PATH = "helm/" + DOCKER_IMAGE.split("/")[1]
  43. VALUES_FILE_DEV = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[1] + ".yaml"
  44. VALUES_FILE_PROD = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[1] + ".yaml"
  45. }
  46. }
  47. }
  48. stage("Update Helm Chart values.yaml") {
  49. steps {
  50. script {
  51. echo "Update values.yaml"
  52. def data_build = readYaml file: VALUES_FILE_DEV
  53. TAG_PATH_BUILD = "images." + DOCKER_IMAGE.split("/")[1] + ".build"
  54. TAG = yamlOps.getBuilder(TAG_PATH_BUILD, data_build)
  55. def data_stable = readYaml file: VALUES_FILE_PROD
  56. TAG_PATH_STABLE = "images." + DOCKER_IMAGE.split("/")[1] + ".stable"
  57. yamlOps.updateBuilder(TAG_PATH_STABLE, data_stable, TAG)
  58. writeYaml file: VALUES_FILE_PROD, data: data_stable, overwrite: true
  59. }
  60. }
  61. }
  62. stage("Update APP") {
  63. steps {
  64. script {
  65. RELEASENAME = DOCKER_IMAGE.split("/")[1]
  66. NAMESPACE = DOCKER_IMAGE.split("/")[1]
  67. IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${TAG}"
  68. try {
  69. echo "Update APP"
  70. k8sHelmOps.upgrade(CONTEXT, NAMESPACE, RELEASENAME, CHART_PATH, VALUES_FILE_PROD, HELM_REPO)
  71. echo "Update success!!!"
  72. }
  73. catch(err) {
  74. echo "Recovering APP"
  75. k8sHelmOps.rollback(CONTEXT, NAMESPACE, RELEASENAME)
  76. throw err
  77. }
  78. }
  79. }
  80. }
  81. stage("Push changes to REPO") {
  82. steps {
  83. script {
  84. echo "Push changes to Repo"
  85. PROJECT = DOCKER_IMAGE.split("/")[1]
  86. COMMIT = "Version update ${PROJECT} to ${TAG}"
  87. gitOps.push(HELM_DIR, COMMIT)
  88. }
  89. }
  90. }
  91. // stage("Tagging Image") {
  92. // steps {
  93. // script {
  94. // echo "Tagging ${TAG} to latest"
  95. // IMAGE = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}"
  96. // k8sBuildImage.crane(IMAGE, TAG, 'latest')
  97. // }
  98. // }
  99. // }
  100. }
  101. post {
  102. failure {
  103. mail charset: 'UTF-8',
  104. subject: "Jenkins build ERROR",
  105. mimeType: 'text/html',
  106. to: "${mailto}",
  107. 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}"
  108. }
  109. }
  110. }