Jenkinsfile 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * 整体 / 单服务 / 多选:仅晋升 Harbor 镜像 + 可选 ACK。
  3. * Script Path:docs/jenkins/produ/whole/Jenkinsfile
  4. */
  5. def sparseCheckoutProduShared() {
  6. checkout scm: [
  7. $class: 'GitSCM',
  8. branches: scm.branches,
  9. extensions: [
  10. [$class: 'CloneOption', depth: 1, shallow: true, noTags: true],
  11. [$class: 'SparseCheckoutPaths', sparseCheckoutPaths: [
  12. [path: 'docs/jenkins/produ/_shared/'],
  13. ]],
  14. ],
  15. userRemoteConfigs: scm.userRemoteConfigs,
  16. ]
  17. }
  18. def k8s
  19. def reg
  20. pipeline {
  21. agent any
  22. options {
  23. buildDiscarder(logRotator(numToKeepStr: '15'))
  24. disableConcurrentBuilds()
  25. timestamps()
  26. timeout(time: 120, unit: 'MINUTES')
  27. }
  28. parameters {
  29. choice(name: 'DEPLOY_MODE', choices: ['whole', 'single', 'multi'],
  30. description: 'whole=7个服务;single=选一个;multi=勾选 MULTI_*')
  31. choice(name: 'SINGLE_SERVICE', choices: [
  32. 'gateway', 'store', 'second', 'store-platform', 'lawyer', 'job', 'dining',
  33. ], description: '仅 DEPLOY_MODE=single 时生效')
  34. booleanParam(name: 'MULTI_gateway', defaultValue: false)
  35. booleanParam(name: 'MULTI_store', defaultValue: false)
  36. booleanParam(name: 'MULTI_second', defaultValue: false)
  37. booleanParam(name: 'MULTI_store_platform', defaultValue: false)
  38. booleanParam(name: 'MULTI_lawyer', defaultValue: false)
  39. booleanParam(name: 'MULTI_job', defaultValue: false)
  40. booleanParam(name: 'MULTI_dining', defaultValue: false)
  41. string(name: 'SOURCE_TAG', defaultValue: 'uat-latest', trim: true,
  42. description: '七个服务共用的预生产 Harbor tag')
  43. string(name: 'TARGET_TAG', defaultValue: '', trim: true,
  44. description: '留空则 produ-${BUILD_NUMBER}')
  45. string(name: 'HARBOR_REGISTRY', defaultValue: '39.105.153.68', trim: true)
  46. string(name: 'HARBOR_PROJECT', defaultValue: 'alien_cloud', trim: true)
  47. choice(name: 'DEPLOY_STRATEGY', choices: ['rolling', 'canary', 'skip'],
  48. description: 'skip=只晋升 Harbor,不 kubectl')
  49. string(name: 'CANARY_WEIGHT', defaultValue: '10', trim: true)
  50. string(name: 'K8S_NAMESPACE', defaultValue: 'alien-produ', trim: true)
  51. booleanParam(name: 'DRY_RUN', defaultValue: false)
  52. }
  53. environment {
  54. HARBOR_CREDENTIALS = 'harbor-robot-alien'
  55. KUBECONFIG_CREDENTIALS = 'ack-kubeconfig-alien'
  56. }
  57. stages {
  58. stage('Plan') {
  59. steps {
  60. script {
  61. sparseCheckoutProduShared()
  62. k8s = load 'docs/jenkins/produ/_shared/k8s-produ-lib.groovy'
  63. reg = load 'docs/jenkins/produ/_shared/service-registry.groovy'
  64. def services = reg.filterServices(reg.getServiceRegistry(), params)
  65. env.PROMOTE_LIST = services*.prodDir.join(',')
  66. env.TARGET_TAG_RESOLVED = k8s.resolveTargetTag(this, params.TARGET_TAG)
  67. echo ">>> DEPLOY_MODE=${params.DEPLOY_MODE} 服务=${env.PROMOTE_LIST}"
  68. echo ">>> ${params.SOURCE_TAG} → ${env.TARGET_TAG_RESOLVED}"
  69. }
  70. }
  71. }
  72. stage('Promote images') {
  73. steps {
  74. script {
  75. def services = reg.filterServices(reg.getServiceRegistry(), params)
  76. k8s.promoteHarborImages(this, services, [
  77. harborRegistry: params.HARBOR_REGISTRY,
  78. harborProject: params.HARBOR_PROJECT,
  79. sourceTag: params.SOURCE_TAG,
  80. targetTag: env.TARGET_TAG_RESOLVED,
  81. harborCredentialsId: env.HARBOR_CREDENTIALS,
  82. dryRun: params.DRY_RUN == true,
  83. ])
  84. }
  85. }
  86. }
  87. stage('Deploy to ACK') {
  88. when { expression { return params.DEPLOY_STRATEGY != 'skip' && !params.DRY_RUN } }
  89. steps {
  90. script {
  91. def services = reg.filterServices(reg.getServiceRegistry(), params)
  92. def regHost = params.HARBOR_REGISTRY.trim()
  93. def proj = params.HARBOR_PROJECT.trim()
  94. def tgtTag = env.TARGET_TAG_RESOLVED
  95. def strategy = params.DEPLOY_STRATEGY
  96. services.each { s ->
  97. def imageRef = "${regHost}/${proj}/${s.prodDir}:${tgtTag}"
  98. k8s.deployToAck(this, [
  99. k8sNamespace: params.K8S_NAMESPACE,
  100. imageRef: imageRef,
  101. deployStrategy: strategy == 'canary' ? 'canary' : 'rolling',
  102. deploymentStable: s.deployName,
  103. deploymentCanary: "${s.deployName}-canary",
  104. ingressCanary: "${s.deployName}-canary",
  105. canaryWeight: (params.CANARY_WEIGHT ?: '10').trim(),
  106. kubeCredentialsId: env.KUBECONFIG_CREDENTIALS,
  107. ])
  108. }
  109. }
  110. }
  111. }
  112. }
  113. post {
  114. success { echo ">>> 完成: ${env.PROMOTE_LIST} tag=${env.TARGET_TAG_RESOLVED}" }
  115. }
  116. }