devK8s.Jenkinsfile 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. PROJECT=''
  23. CHART_NAME=''
  24. CHART_PATH=''
  25. HELM_DIR=''
  26. IMAGE_TO_PUSH=''
  27. }
  28. parameters {
  29. string(
  30. name: "repo",
  31. defaultValue: "prod",
  32. description: "Repository to build and/or deploy from."
  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. echo "GIT clone ${HELM_GIT_URL}"
  45. gitOps.clone(HELM_GIT_URL)
  46. echo "Defining variables"
  47. HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
  48. CHART_PATH = HELM_DIR + "/" + helm + "/" + DOCKER_IMAGE.split("/")[2]
  49. PROJECT = DOCKER_IMAGE.split("/")[1]
  50. VALUES_FILE = "${CHART_PATH}/dev.yaml"
  51. IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  52. }
  53. }
  54. }
  55. stage("Build Image") {
  56. steps {
  57. script {
  58. echo "Building ${IMAGE_TO_PUSH} with Kaniko"
  59. ARGS = [ "repo=${repo}" ]
  60. k8sBuildImage.kaniko(IMAGE_TO_PUSH, ARGS)
  61. }
  62. }
  63. }
  64. stage("Update Helm Chart") {
  65. steps {
  66. script {
  67. TAG_PATH = "images.${PROJECT}"
  68. echo "Update ${TAG_PATH} to ${IMAGE_TO_PUSH} in ${VALUES_FILE}"
  69. def data = readYaml file: "${VALUES_FILE}"
  70. yamlOps.updateBuilder(TAG_PATH, data, IMAGE_TO_PUSH)
  71. writeYaml file: "${VALUES_FILE}", data: data, overwrite: true
  72. }
  73. }
  74. }
  75. stage("Update APP") {
  76. steps {
  77. script {
  78. RELEASENAME = DOCKER_IMAGE.split("/")[0]
  79. NAMESPACE = DOCKER_IMAGE.split("/")[0]
  80. try {
  81. echo "Update APP to ${IMAGE_TO_PUSH}"
  82. k8sHelmOps.upgrade(CONTEXT, NAMESPACE, RELEASENAME, CHART_PATH, VALUES_FILE)
  83. echo "Update success!!!"
  84. }
  85. catch(err) {
  86. echo "Recovering APP"
  87. k8sHelmOps.rollback(CONTEXT, NAMESPACE, RELEASENAME)
  88. throw err
  89. }
  90. }
  91. }
  92. }
  93. stage("Push changes to REPO") {
  94. steps {
  95. script {
  96. echo "Push changes to Repo"
  97. COMMIT = "Version update ${PROJECT}"
  98. gitOps.push(HELM_DIR, COMMIT)
  99. }
  100. }
  101. }
  102. }
  103. post {
  104. failure {
  105. mail charset: 'UTF-8',
  106. subject: "Jenkins build ERROR",
  107. mimeType: 'text/html',
  108. to: "${mailto}",
  109. 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}"
  110. }
  111. }
  112. }