Jenkinsfile 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. timestamps()
  27. timeout(time: 120, unit: 'MINUTES')
  28. }
  29. parameters {
  30. choice(name: 'DEPLOY_MODE', choices: ['whole', 'single', 'multi'])
  31. choice(name: 'SINGLE_SERVICE', choices: [
  32. 'gateway', 'store', 'second', 'store-platform', 'lawyer', 'job', 'dining',
  33. ])
  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: '', trim: true)
  42. string(name: 'TARGET_TAG', defaultValue: '', trim: true)
  43. string(name: 'HARBOR_REGISTRY', defaultValue: '39.105.153.68', trim: true)
  44. string(name: 'HARBOR_PROJECT', defaultValue: 'alien_cloud', trim: true)
  45. choice(name: 'DEPLOY_STRATEGY', choices: ['rolling', 'canary', 'skip'])
  46. string(name: 'CANARY_WEIGHT', defaultValue: '10', trim: true)
  47. string(name: 'K8S_NAMESPACE', defaultValue: 'alien-produ', trim: true)
  48. booleanParam(name: 'DRY_RUN', defaultValue: false)
  49. }
  50. environment {
  51. HARBOR_CREDENTIALS = 'harbor-robot-alien'
  52. KUBECONFIG_CREDENTIALS = 'ack-kubeconfig-alien'
  53. }
  54. stages {
  55. stage('Plan') {
  56. steps {
  57. script {
  58. def (k8s, reg) = getProduLibs()
  59. def services = reg.filterServices(reg.getServiceRegistry(), params)
  60. env.PROMOTE_LIST = services*.prodDir.join(',')
  61. env.TARGET_TAG_RESOLVED = k8s.resolveTargetTag(this, params.TARGET_TAG)
  62. echo ">>> 服务=${env.PROMOTE_LIST} ${params.SOURCE_TAG} → ${env.TARGET_TAG_RESOLVED}"
  63. }
  64. }
  65. }
  66. stage('Promote images') {
  67. steps {
  68. script {
  69. def (k8s, reg) = getProduLibs()
  70. def services = reg.filterServices(reg.getServiceRegistry(), params)
  71. k8s.promoteHarborImages(this, services, [
  72. harborRegistry: params.HARBOR_REGISTRY,
  73. harborProject: params.HARBOR_PROJECT,
  74. sourceTag: params.SOURCE_TAG,
  75. targetTag: env.TARGET_TAG_RESOLVED,
  76. harborCredentialsId: env.HARBOR_CREDENTIALS,
  77. dryRun: params.DRY_RUN == true,
  78. ])
  79. }
  80. }
  81. }
  82. stage('Deploy to ACK') {
  83. when { expression { return params.DEPLOY_STRATEGY != 'skip' && !params.DRY_RUN } }
  84. steps {
  85. script {
  86. def (k8s, reg) = getProduLibs()
  87. def services = reg.filterServices(reg.getServiceRegistry(), params)
  88. def regHost = params.HARBOR_REGISTRY.trim()
  89. def proj = params.HARBOR_PROJECT.trim()
  90. def tgtTag = env.TARGET_TAG_RESOLVED
  91. def strategy = params.DEPLOY_STRATEGY
  92. services.each { s ->
  93. def imageRef = "${regHost}/${proj}/${s.prodDir}:${tgtTag}"
  94. k8s.deployToAck(this, [
  95. k8sNamespace: params.K8S_NAMESPACE,
  96. imageRef: imageRef,
  97. deployStrategy: strategy == 'canary' ? 'canary' : 'rolling',
  98. deploymentStable: s.deployName,
  99. deploymentCanary: "${s.deployName}-canary",
  100. ingressCanary: "${s.deployName}-canary",
  101. canaryWeight: (params.CANARY_WEIGHT ?: '10').trim(),
  102. kubeCredentialsId: env.KUBECONFIG_CREDENTIALS,
  103. ])
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }