devK8s.Jenkinsfile 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. @Library('jenkins-library-iru@master') _
  2. def Label = "web-laboratory-admin-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-laboratory/admin'
  18. HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/k8s.git'
  19. HELM_REPO='https://images.sdsys.ru/artifactory/helm/'
  20. APP_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/admin.git'
  21. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  22. DOCKER_REGISTRY='images.sdsys.ru'
  23. CHART_PATH=''
  24. HELM_DIR=''
  25. IMAGE_TO_PUSH=''
  26. VALUES_FILE =''
  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: "version",
  36. defaultValue: "16.1",
  37. description: "Witch version to build and/or deploy."
  38. )
  39. string(
  40. name: "mailto",
  41. defaultValue: "admin@sdsys.ru",
  42. description: "Email which has to be notified."
  43. )
  44. }
  45. stages {
  46. stage("Prepare to Build") {
  47. steps {
  48. script {
  49. echo "GIT clone ${HELM_GIT_URL}"
  50. gitOps.clone(HELM_GIT_URL)
  51. echo "Defining variables"
  52. HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
  53. CHART_PATH = "helm/" + DOCKER_IMAGE.split("/")[0]
  54. VALUES_FILE = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[0] + ".yaml"
  55. IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  56. }
  57. }
  58. }
  59. stage("Build Image") {
  60. steps {
  61. script {
  62. echo "Building ${IMAGE_TO_PUSH} with Kaniko"
  63. ARGS = [ "repo=${repo}" ]
  64. k8sBuildImage.kaniko(IMAGE_TO_PUSH, ARGS)
  65. }
  66. }
  67. }
  68. stage("Update Helm Chart values.yaml") {
  69. steps {
  70. script {
  71. TAG_PATH = "images." + DOCKER_IMAGE.split("/")[1]
  72. echo "Update ${TAG_PATH} to ${IMAGE_TO_PUSH} in ${VALUES_FILE}"
  73. def data = readYaml file: "${VALUES_FILE}"
  74. yamlOps.updateBuilder(TAG_PATH, data, IMAGE_TO_PUSH)
  75. writeYaml file: "${VALUES_FILE}", data: data, overwrite: true
  76. }
  77. }
  78. }
  79. stage("Update APP") {
  80. steps {
  81. script {
  82. try {
  83. echo "Update APP to ${IMAGE_TO_PUSH}"
  84. k8sHelmOps.upgrade(CONTEXT, DOCKER_IMAGE.split("/")[0], DOCKER_IMAGE.split("/")[0], CHART_PATH, VALUES_FILE, HELM_REPO)
  85. echo "Update success!!!"
  86. }
  87. catch(err) {
  88. echo "Recovering APP"
  89. k8sHelmOps.rollback(CONTEXT, DOCKER_IMAGE.split("/")[0], DOCKER_IMAGE.split("/")[0])
  90. throw err
  91. }
  92. }
  93. }
  94. }
  95. stage("Push changes to REPO") {
  96. steps {
  97. script {
  98. echo "Push changes to Repo"
  99. PROJECT = DOCKER_IMAGE.split("/")[1]
  100. COMMIT = "Version update ${PROJECT}"
  101. gitOps.push(HELM_DIR, COMMIT)
  102. }
  103. }
  104. }
  105. }
  106. post {
  107. failure {
  108. mail charset: 'UTF-8',
  109. subject: "Jenkins build ERROR",
  110. mimeType: 'text/html',
  111. to: "${mailto}",
  112. 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}"
  113. }
  114. }
  115. }