devK8s.Jenkinsfile 3.0 KB

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