我有一个 ci/cd YAML 管道用于部署我的 Kubernetes 集群。我还有一个单独的 YAML 管道,用于扩大和缩小集群规模。
我的放大和缩小 YAML 检索每个环境的当前部署构建,因为我们的存储库只有一个主分支。这确保我们不会部署任何尚未部署到环境(开发、阶段和产品)的新 helm 值。
我应该继续保留单独的 YAML 文件还是将其压缩到单个管道中?有没有最佳实践或者这真的取决于团队的偏好?
我应该继续保留单独的 YAML 文件还是将其压缩到单个管道中?
简短回答:到处使用模板。
使用模板隐藏复杂的实现细节,并在管道内或跨管道重用逻辑和配置。
逻辑(又名功能)模板:
配置模板:
示例:
variables:
- template: /pipelines/variables/common-variables.yaml
pool:
name: $(agentPool)
stages:
- stage: Build
displayName: Build and publish Docker image
jobs:
- job: BuildAndPushDocker
displayName: Build and push Docker image
variables:
- template: /pipelines/variables/docker-variables.yaml
steps:
- checkout: self
- template: /pipelines/steps/build-push-docker-steps.yaml
parameters:
workingDirectory: $(workingDirectory)
# ...
- stage: DeployDev
displayName: Deploy to Dev
dependsOn: Build
jobs:
- job: DeployDev
displayName: Deploy to Dev
variables:
# reference environment-specific variables here
- template: /pipelines/variables/dev-variables.yaml
steps:
- checkout: self
- template: /pipelines/steps/deploy-helm-chart-steps.yaml
parameters:
# use variables from /pipelines/variables/dev-variables.yaml
chart: $(chart)
namespace: $(namespace)
valuesFile: $(valuesFile)
# ...
- stage: DeployStage
displayName: Deploy to Stage
dependsOn: DeployDev
jobs:
- job: DeployStage
displayName: Deploy to Stage
variables:
# reference environment-specific variables here
- template: /pipelines/variables/stage-variables.yaml
steps:
- checkout: self
- template: /pipelines/steps/deploy-helm-chart-steps.yaml
parameters:
# use variables from /pipelines/variables/stage-variables.yaml
chart: $(chart)
namespace: $(namespace)
valuesFile: $(valuesFile)
# ...
- stage: DeployProd
displayName: Deploy to Prod
dependsOn: DeployStage
jobs:
- job: DeployProd
displayName: Deploy to Prod
variables:
# reference environment-specific variables here
- template: /pipelines/variables/prod-variables.yaml
steps:
- checkout: self
- template: /pipelines/steps/deploy-helm-chart-steps.yaml
parameters:
# use variables from /pipelines/variables/prod-variables.yaml
chart: $(chart)
namespace: $(namespace)
valuesFile: $(valuesFile)
# ...
上述管道使用步骤和变量模板,使我们能够重用功能,例如部署 Helm 图表,为每个环境使用不同的配置。
也可以创建作业或阶段的模板。