buildImage.Jenkinsfile 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. @Library('jenkins-library@master') _
  2. def Label = "analis-lab-build-${UUID.randomUUID().toString()}"
  3. pipeline {
  4. options {
  5. buildDiscarder logRotator(numToKeepStr: '10')
  6. disableConcurrentBuilds()
  7. timeout(time: 15, unit: 'MINUTES')
  8. }
  9. agent {
  10. kubernetes {
  11. label Label
  12. yaml libraryResource('analisBuildPodDefinition.yaml')
  13. }
  14. }
  15. environment {
  16. CONTEXT='provision@sdsys.office'
  17. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  18. DOCKER_REGISTRY='images.sdsys.ru'
  19. DOCKER_IMAGE='web-laboratory/analis'
  20. REPO='svn://192.168.21.250/programs/trunk/Services/Analis/Win32'
  21. HELM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/k8s.git'
  22. TAG=''
  23. SVN_DIR='analis'
  24. REV_NUMBER=''
  25. CHART_PATH=''
  26. HELM_DIR=''
  27. IMAGE_TO_PUSH=''
  28. VALUES_FILE =''
  29. }
  30. parameters {
  31. string(
  32. name: "REVNUM",
  33. defaultValue: "HEAD",
  34. description: "Revision number"
  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("SVN Checkout") {
  44. steps {
  45. script {
  46. svnOps.checkout(REPO, REPO.split("/")[-2].toLowerCase(), REVNUM)
  47. REV_NUMBER = svnOps.revNumber(REPO.split("/")[-2].toLowerCase())
  48. echo "REV_NUMBER = ${REV_NUMBER}"
  49. gitOps.clone(HELM_GIT_URL)
  50. HELM_DIR = dirOps.calculateDir(HELM_GIT_URL)
  51. CHART_PATH = "helm/" + DOCKER_IMAGE.split("/")[0]
  52. VALUES_FILE = HELM_DIR + "/helm/" + "dev-" + DOCKER_IMAGE.split("/")[1] + ".yaml
  53. IMAGE_TO_PUSH = "${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${REV_NUMBER}-${BUILD_NUMBER}"
  54. }
  55. }
  56. }
  57. stage("Build Image") {
  58. steps {
  59. script {
  60. echo "Building ${IMAGE_TO_PUSH} with Kaniko"
  61. k8sBuildImage.kaniko(IMAGE_TO_PUSH)
  62. TAG = '${REV_NUMBER}-${BUILD_NUMBER}'
  63. echo "Taging ${IMAGE_TO_PUSH} to latest"
  64. k8sBuildImage.crane(IMAGE_TO_PUSH)
  65. }
  66. }
  67. }
  68. stage("Update Helm Chart values.yaml") {
  69. steps {
  70. script {
  71. echo "Update Helm Chart"
  72. def data = readYaml file: VALUES_FILE
  73. TAG = "${REV_NUMBER}-${BUILD_NUMBER}"
  74. TAG_PATH = "images." + DOCKER_IMAGE.split("/")[1] + ".build"
  75. echo "Update ${TAG_PATH} to ${TAG} in ${VALUES_FILE}"
  76. yamlOps.updateBuilder(TAG_PATH, data, TAG)
  77. writeYaml file: "${VALUES_FILE}", data: data, overwrite: true
  78. }
  79. }
  80. }
  81. stage("Push changes to REPO") {
  82. steps {
  83. script {
  84. echo "Push changes to Repo"
  85. COMMIT = "Build version ${TAG}"
  86. gitOps.push(HELM_DIR, COMMIT)
  87. }
  88. }
  89. }
  90. }
  91. post {
  92. failure {
  93. mail charset: 'UTF-8',
  94. subject: "Jenkins build ERROR",
  95. mimeType: 'text/html',
  96. to: "${mailto}",
  97. 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}"
  98. }
  99. }
  100. }