devK8s.Jenkinsfile 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. @Library('jenkins-library-iru@master') _
  2. def Label = "web-registration-node-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('standartPodDefinition.yaml')
  13. }
  14. }
  15. environment {
  16. CONTEXT='provision@sdsys.office'
  17. DOCKER_IMAGE='web-registration/node'
  18. HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/k8s.git'
  19. APP_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/node.git'
  20. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  21. DOCKER_REGISTRY='images.sdsys.ru'
  22. CHART_PATH=''
  23. HELM_DIR=''
  24. IMAGE_TO_PUSH=''
  25. VALUES_FILE = ''
  26. }
  27. parameters {
  28. string(
  29. name: "repo",
  30. defaultValue: "prod",
  31. description: "Repository to build and/or deploy from."
  32. )
  33. string(
  34. name: "mailto",
  35. defaultValue: "admin@sdsys.ru",
  36. description: "Email which has to be notified."
  37. )
  38. }
  39. stages {
  40. stage("Prepare to Build") {
  41. steps {
  42. script {
  43. echo "GIT clone ${HELM_GIT_URL}"
  44. gitOps.clone(HELM_GIT_URL)
  45. echo "Defining variables"
  46. HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
  47. CHART_PATH = HELM_DIR + "/helm/" + DOCKER_IMAGE.split("/")[0]
  48. VALUES_FILE = "${CHART_PATH}/dev.yaml"
  49. IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  50. }
  51. }
  52. }
  53. stage("Build Image") {
  54. steps {
  55. script {
  56. echo "Building ${IMAGE_TO_PUSH} with Kaniko"
  57. ARGS = [ "repo=${repo}" ]
  58. k8sBuildImage.kaniko(IMAGE_TO_PUSH, ARGS)
  59. }
  60. }
  61. }
  62. stage("Update Helm Chart") {
  63. steps {
  64. script {
  65. TAG_PATH = "images." + DOCKER_IMAGE.split("/")[1]
  66. echo "Update ${TAG_PATH} to ${IMAGE_TO_PUSH} in ${VALUES_FILE}"
  67. def data = readYaml file: "${VALUES_FILE}"
  68. yamlOps.updateBuilder(TAG_PATH, data, IMAGE_TO_PUSH)
  69. writeYaml file: "${VALUES_FILE}", data: data, overwrite: true
  70. }
  71. }
  72. }
  73. stage("Update APP") {
  74. steps {
  75. script {
  76. RELEASENAME = DOCKER_IMAGE.split("/")[0]
  77. NAMESPACE = DOCKER_IMAGE.split("/")[0]
  78. try {
  79. echo "Update APP to ${IMAGE_TO_PUSH}"
  80. k8sHelmOps.upgrade(CONTEXT, NAMESPACE, RELEASENAME, CHART_PATH, VALUES_FILE)
  81. echo "Update success!!!"
  82. }
  83. catch(err) {
  84. echo "Recovering APP"
  85. k8sHelmOps.rollback(CONTEXT, 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. PROJECT = DOCKER_IMAGE.split("/")[1]
  96. COMMIT = "Version update ${PROJECT}"
  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. }