Jenkinsfile 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // 与 whole/Jenkinsfile 相同逻辑
  2. def sparseCheckoutProduShared() {
  3. checkout scm: [
  4. $class: 'GitSCM',
  5. branches: scm.branches,
  6. extensions: [
  7. [$class: 'CloneOption', depth: 1, shallow: true, noTags: true],
  8. [$class: 'SparseCheckoutPaths', sparseCheckoutPaths: [
  9. [path: 'docs/jenkins/produ/_shared/'],
  10. ]],
  11. ],
  12. userRemoteConfigs: scm.userRemoteConfigs,
  13. ]
  14. }
  15. def getProduLibs() {
  16. sparseCheckoutProduShared()
  17. def k8s = load 'docs/jenkins/produ/_shared/k8s-produ-lib.groovy'
  18. def reg = load 'docs/jenkins/produ/_shared/service-registry.groovy'
  19. return [k8s, reg]
  20. }
  21. pipeline {
  22. agent any
  23. options {
  24. buildDiscarder(logRotator(numToKeepStr: '20'))
  25. disableConcurrentBuilds()
  26. skipDefaultCheckout(true)
  27. timestamps()
  28. timeout(time: 120, unit: 'MINUTES')
  29. }
  30. parameters {
  31. choice(name: 'DEPLOY_MODE', choices: ['whole', 'single', 'multi'])
  32. choice(name: 'SINGLE_SERVICE', choices: [
  33. 'gateway', 'store', 'second', 'store-platform', 'lawyer', 'job', 'dining',
  34. ])
  35. booleanParam(name: 'MULTI_gateway', defaultValue: false)
  36. booleanParam(name: 'MULTI_store', defaultValue: false)
  37. booleanParam(name: 'MULTI_second', defaultValue: false)
  38. booleanParam(name: 'MULTI_store_platform', defaultValue: false)
  39. booleanParam(name: 'MULTI_lawyer', defaultValue: false)
  40. booleanParam(name: 'MULTI_job', defaultValue: false)
  41. booleanParam(name: 'MULTI_dining', defaultValue: false)
  42. string(name: 'SOURCE_TAG', defaultValue: 'uat-latest', trim: true,
  43. description: 'Harbor UAT tag,默认 uat-latest;回滚填 uat-build-<N>')
  44. string(name: 'TARGET_TAG', defaultValue: '', trim: true)
  45. choice(name: 'PROMOTE_METHOD', choices: ['harbor-api', 'docker-pull'],
  46. description: 'harbor-api=服务端打 tag(快)')
  47. booleanParam(name: 'PARALLEL_ACK_DEPLOY', defaultValue: true)
  48. string(name: 'HARBOR_REGISTRY', defaultValue: '39.105.153.68', trim: true)
  49. string(name: 'HARBOR_PROJECT', defaultValue: 'alien_cloud', trim: true)
  50. choice(name: 'DEPLOY_STRATEGY', choices: ['rolling', 'canary', 'skip'],
  51. description: '''rolling=更新 stable Deployment,kubectl 滚动替换 Pod(全量发版)
  52. canary=仅更新 *-canary Deployment,并按 CANARY_WEIGHT 设置 Ingress 灰度流量
  53. skip=只晋升 Harbor 镜像,不执行 kubectl''')
  54. string(name: 'CANARY_WEIGHT', defaultValue: '10', trim: true)
  55. string(name: 'K8S_NAMESPACE', defaultValue: 'alien-produ', trim: true)
  56. booleanParam(name: 'DRY_RUN', defaultValue: false)
  57. }
  58. environment {
  59. HARBOR_CREDENTIALS = 'harbor-robot-alien'
  60. KUBECONFIG_CREDENTIALS = 'ack-kubeconfig-alien'
  61. }
  62. stages {
  63. stage('Promote & Deploy') {
  64. steps {
  65. script {
  66. def (k8s, reg) = getProduLibs()
  67. def services = reg.filterServices(reg.getServiceRegistry(), params)
  68. env.SOURCE_TAG_RESOLVED = k8s.requireSourceTag(this, params.SOURCE_TAG)
  69. env.TARGET_TAG_RESOLVED = k8s.resolveTargetTag(this, params.TARGET_TAG)
  70. env.PROMOTE_LIST = services*.prodDir.join(',')
  71. echo ">>> 服务=${env.PROMOTE_LIST} ${env.SOURCE_TAG_RESOLVED} → ${env.TARGET_TAG_RESOLVED}"
  72. k8s.promoteHarborImages(this, services, [
  73. harborRegistry: params.HARBOR_REGISTRY,
  74. harborProject: params.HARBOR_PROJECT,
  75. sourceTag: env.SOURCE_TAG_RESOLVED,
  76. targetTag: env.TARGET_TAG_RESOLVED,
  77. harborCredentialsId: env.HARBOR_CREDENTIALS,
  78. promoteMethod: params.PROMOTE_METHOD,
  79. dryRun: params.DRY_RUN == true,
  80. ])
  81. if (params.DEPLOY_STRATEGY != 'skip' && !params.DRY_RUN) {
  82. k8s.deployServicesToAck(this, services, [
  83. harborRegistry: params.HARBOR_REGISTRY,
  84. harborProject: params.HARBOR_PROJECT,
  85. targetTag: env.TARGET_TAG_RESOLVED,
  86. deployStrategy: params.DEPLOY_STRATEGY,
  87. k8sNamespace: params.K8S_NAMESPACE,
  88. canaryWeight: params.CANARY_WEIGHT,
  89. kubeCredentialsId: env.KUBECONFIG_CREDENTIALS,
  90. parallelDeploy: params.PARALLEL_ACK_DEPLOY == true,
  91. ])
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }