k8s-produ-lib.groovy 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Harbor ???? + ACK ??????? Jenkins ????
  3. * ????produ/<prodDir>/Jenkinsfile
  4. * ??/???produ/whole/Jenkinsfile ? produ/promote-image/Jenkinsfile
  5. */
  6. /** ???? tag */
  7. def resolveTargetTag(def script, String targetTagParam) {
  8. def t = (targetTagParam ?: '').trim()
  9. return t ?: "produ-${script.env.BUILD_NUMBER}"
  10. }
  11. /**
  12. * ?????????? Harbor ???pull ? tag ? tag ?? tag ? push?
  13. * cfg: harborRegistry, harborProject, sourceTag, targetTag, harborCredentialsId, dryRun
  14. * services: List<Map> ? prodDir
  15. */
  16. def promoteHarborImages(def script, List services, Map cfg) {
  17. def regHost = cfg.harborRegistry.trim()
  18. def proj = cfg.harborProject.trim()
  19. def srcTag = (cfg.sourceTag ?: 'uat-latest').trim()
  20. def tgtTag = cfg.targetTag
  21. def dryRun = cfg.dryRun == true
  22. services.each { s ->
  23. def src = "${regHost}/${proj}/${s.prodDir}:${srcTag}"
  24. def tgt = "${regHost}/${proj}/${s.prodDir}:${tgtTag}"
  25. echo ">>> ???? ${s.prodDir}: ${src} ? ${tgt}"
  26. if (dryRun) {
  27. return
  28. }
  29. }
  30. if (dryRun) {
  31. return
  32. }
  33. script.withCredentials([script.usernamePassword(
  34. credentialsId: cfg.harborCredentialsId,
  35. usernameVariable: 'HARBOR_USER',
  36. passwordVariable: 'HARBOR_PASS',
  37. )]) {
  38. script.sh "echo \${HARBOR_PASS} | docker login ${regHost} -u \${HARBOR_USER} --password-stdin"
  39. services.each { s ->
  40. def src = "${regHost}/${proj}/${s.prodDir}:${srcTag}"
  41. def tgt = "${regHost}/${proj}/${s.prodDir}:${tgtTag}"
  42. script.sh """
  43. set -e
  44. docker pull ${src}
  45. docker tag ${src} ${tgt}
  46. docker push ${tgt}
  47. """
  48. }
  49. }
  50. }
  51. def deployToAck(def script, Map cfg) {
  52. def ns = cfg.k8sNamespace
  53. def imageRef = cfg.imageRef
  54. def strategy = cfg.deployStrategy
  55. def targetDeploy = strategy == 'canary' ? cfg.deploymentCanary : cfg.deploymentStable
  56. def canaryWeight = cfg.canaryWeight
  57. def ingressCanary = cfg.ingressCanary
  58. def kubeCreds = cfg.kubeCredentialsId
  59. script.withCredentials([script.file(credentialsId: kubeCreds, variable: 'KUBECONFIG')]) {
  60. script.sh """
  61. set -e
  62. kubectl config current-context
  63. kubectl -n ${ns} set image deployment/${targetDeploy} app=${imageRef} --record
  64. kubectl -n ${ns} rollout status deployment/${targetDeploy} --timeout=300s
  65. """
  66. if (strategy == 'canary') {
  67. script.sh """
  68. set -e
  69. kubectl -n ${ns} annotate ingress ${ingressCanary} \\
  70. nginx.ingress.kubernetes.io/canary=true \\
  71. nginx.ingress.kubernetes.io/canary-weight=${canaryWeight} \\
  72. --overwrite
  73. """
  74. }
  75. }
  76. }
  77. /** ?????? + ?? ACK */
  78. def promoteOneServiceToAck(def script, Map svc, Map params, Map env) {
  79. def regHost = params.HARBOR_REGISTRY.trim()
  80. def proj = params.HARBOR_PROJECT.trim()
  81. def srcTag = (params.SOURCE_TAG ?: 'uat-latest').trim()
  82. def tgtTag = resolveTargetTag(script, params.TARGET_TAG)
  83. def dryRun = params.DRY_RUN == true
  84. def strategy = params.DEPLOY_STRATEGY ?: 'rolling'
  85. env.TARGET_TAG_RESOLVED = tgtTag
  86. env.IMAGE_REF = "${regHost}/${proj}/${svc.prodDir}:${tgtTag}"
  87. promoteHarborImages(script, [svc], [
  88. harborRegistry: regHost,
  89. harborProject: proj,
  90. sourceTag: srcTag,
  91. targetTag: tgtTag,
  92. harborCredentialsId: env.HARBOR_CREDENTIALS,
  93. dryRun: dryRun,
  94. ])
  95. if (dryRun || strategy == 'skip') {
  96. return
  97. }
  98. deployToAck(script, [
  99. k8sNamespace: params.K8S_NAMESPACE,
  100. imageRef: env.IMAGE_REF,
  101. deployStrategy: strategy == 'canary' ? 'canary' : 'rolling',
  102. deploymentStable: svc.deployName,
  103. deploymentCanary: "${svc.deployName}-canary",
  104. ingressCanary: "${svc.deployName}-canary",
  105. canaryWeight: (params.CANARY_WEIGHT ?: '10').trim(),
  106. kubeCredentialsId: env.KUBECONFIG_CREDENTIALS,
  107. ])
  108. }
  109. return this