k8s-produ-lib.groovy 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * ACK + Harbor 发版共享函数库(不是 Jenkins 入口文件)。
  3. *
  4. * - 单服务:produ/<prodDir>/Jenkinsfile 在顶层 pipeline 内 load 本文件后调用各 stage 函数。
  5. * - 整体/多选:produ/whole/Jenkinsfile load 本文件 + service-registry.groovy。
  6. *
  7. * Harbor:流水线内 docker login / build / push,日常发版无需打开 Harbor Web。
  8. */
  9. def checkoutBranch(def script, String branch, String gitUrl, String credentialsId) {
  10. script.git branch: branch, credentialsId: credentialsId, url: gitUrl
  11. script.sh """
  12. set -e
  13. git fetch origin
  14. git reset --hard origin/${branch}
  15. git log -1 --oneline
  16. """
  17. }
  18. def mavenPackageModule(def script, String module) {
  19. script.sh """
  20. set -e
  21. unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy || true
  22. export MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true"
  23. mvn -version
  24. mvn clean package -pl ${module} -am -DskipTests -e \\
  25. -Dmaven.repo.local=\${WORKSPACE}/.m2/repository
  26. """
  27. }
  28. /** 整体发版:根 POM 一次打包全部模块(比 7 次单模块 mvn 更快) */
  29. def mavenPackageAll(def script) {
  30. script.sh """
  31. set -e
  32. unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy || true
  33. export MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true"
  34. mvn -version
  35. mvn clean package -DskipTests -e \\
  36. -Dmaven.repo.local=\${WORKSPACE}/.m2/repository
  37. """
  38. }
  39. def dockerBuildAndPush(def script, Map cfg) {
  40. def registry = cfg.harborRegistry
  41. def project = cfg.harborProject
  42. def prodDir = cfg.prodDir
  43. def module = cfg.module
  44. def serverPort = cfg.serverPort
  45. def withLib = cfg.withLib ? 'true' : 'false'
  46. def baseImage = cfg.baseImage
  47. def imageRef = cfg.imageRef
  48. def harborCreds = cfg.harborCredentialsId
  49. def dockerfile = 'docs/jenkins/produ/docker/Dockerfile.java-service'
  50. def moduleDir = "${script.env.WORKSPACE}/${module}"
  51. def jarName = "${module}-1.0.0.jar"
  52. script.withCredentials([script.usernamePassword(
  53. credentialsId: harborCreds, usernameVariable: 'HARBOR_USER', passwordVariable: 'HARBOR_PASS')]) {
  54. script.sh """
  55. set -e
  56. mkdir -p ${moduleDir}/lib
  57. touch ${moduleDir}/lib/.keep
  58. echo "\${HARBOR_PASS}" | docker login ${registry} -u "\${HARBOR_USER}" --password-stdin
  59. docker build -f ${dockerfile} \\
  60. --build-arg BASE_IMAGE=${baseImage} \\
  61. --build-arg JAR_FILE=${jarName} \\
  62. --build-arg SERVER_PORT=${serverPort} \\
  63. --build-arg WITH_LIB=${withLib} \\
  64. -t ${imageRef} \\
  65. ${moduleDir}
  66. docker push ${imageRef}
  67. """
  68. }
  69. }
  70. def deployToAck(def script, Map cfg) {
  71. def ns = cfg.k8sNamespace
  72. def imageRef = cfg.imageRef
  73. def strategy = cfg.deployStrategy
  74. def targetDeploy = strategy == 'canary' ? cfg.deploymentCanary : cfg.deploymentStable
  75. def canaryWeight = cfg.canaryWeight
  76. def ingressCanary = cfg.ingressCanary
  77. def kubeCreds = cfg.kubeCredentialsId
  78. script.withCredentials([script.file(credentialsId: kubeCreds, variable: 'KUBECONFIG')]) {
  79. script.sh """
  80. set -e
  81. kubectl config current-context
  82. kubectl -n ${ns} set image deployment/${targetDeploy} app=${imageRef} --record
  83. kubectl -n ${ns} rollout status deployment/${targetDeploy} --timeout=300s
  84. """
  85. if (strategy == 'canary') {
  86. script.sh """
  87. set -e
  88. kubectl -n ${ns} annotate ingress ${ingressCanary} \\
  89. nginx.ingress.kubernetes.io/canary=true \\
  90. nginx.ingress.kubernetes.io/canary-weight=${canaryWeight} \\
  91. --overwrite
  92. """
  93. }
  94. }
  95. }
  96. return this