prodK8s.Jenkinsfile 2.9 KB

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