devK8s.Jenkinsfile 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. @Library('jenkins-library@master') _
  2. def Label = "node-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: "repo",
  31. defaultValue: "prod",
  32. description: "Repository to build and/or deploy from. Values - dev, prod."
  33. )
  34. string(
  35. name: "mailto",
  36. defaultValue: "admin@sdsys.ru",
  37. description: "Email which has to be notified."
  38. )
  39. }
  40. stages {
  41. stage("Prepare to Build") {
  42. steps {
  43. script {
  44. gitOps.clone(APP_GIT_URL)
  45. APP_DIR = dirOps.calculateDir(APP_GIT_URL)
  46. }
  47. }
  48. }
  49. stage("Build Image") {
  50. steps {
  51. script {
  52. dir (APP_DIR) {
  53. echo "Build Image with Kaniko"
  54. REGISTRY = DOCKER_REGISTRY + "/" + DOCKER_IMAGE
  55. ARGS = [ "repo=${repo}" ]
  56. TAG = "${repo}-${BUILD_NUMBER}"
  57. k8sBuildImage.kaniko(REGISTRY, TAG, ARGS)
  58. }
  59. }
  60. }
  61. }
  62. stage("Update Helm Chart values.yaml") {
  63. steps {
  64. script {
  65. echo "Update Helm Chart"
  66. gitOps.clone(HELM_GIT_URL)
  67. HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
  68. def data = readYaml file: "${HELM_DIR}/${CHART_PATH}/values.yaml"
  69. TAG_PATH = "images.${APP_DIR}.dev"
  70. yamlOps.updateBuilder(TAG_PATH, data, TAG)
  71. writeYaml file: "${HELM_DIR}/${CHART_PATH}/values.yaml", data: data, overwrite: true
  72. }
  73. }
  74. }
  75. stage("Update APP") {
  76. steps {
  77. script {
  78. CHART = "${HELM_DIR}/${CHART_PATH}"
  79. CHART_NAME = CHART_PATH.split("/")[1]
  80. try {
  81. echo "Update APP"
  82. k8sHelmOps.upgrade(CLOUD, NAMESPACE, CHART_NAME, CHART)
  83. }
  84. catch(err) {
  85. echo "Recovering APP"
  86. k8sHelmOps.rollback(CLOUD, NAMESPACE, CHART_NAME)
  87. throw err
  88. }
  89. }
  90. }
  91. }
  92. stage("Push changes to REPO") {
  93. steps {
  94. script {
  95. echo "Push changes to Repo"
  96. COMMIT = "Version update ${APP_DIR}"
  97. gitOps.push(HELM_DIR, COMMIT)
  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. }