我习惯使用经典的Devops " release ";用于将代码更改部署到Kubernetes集群的管道。最近,我一直在考虑改用Azure管道"部署"。工作和"环境"。它似乎工作得很好,我喜欢它的许多功能,比如能够检查与部署相关的Kubernetes实体,并跟踪部署历史。
如果发现bug已经发布(例如发布到生产环境),我习惯从经典的发布管道回滚到旧的部署。由于发布管道是基于构建工件的,您只需在发布UI中的旧工件上运行部署。
现在使用环境选项卡下的部署,我不确定如何运行回滚,除非实际更改代码以恢复到旧状态(并不必要地再次运行CI构建)。另一种选择是,由于部署是相对于代码(或提交)而不是工件完成的,因此可以手动运行一个新的管道并以给定的提交为目标——但这在Devops UI中实现起来相当麻烦,而且似乎容易出错。在我看来,回滚应该很容易实现,而且不容易出错。
有什么办法吗?下面是我的yaml文件的一个示例
trigger:
batch: true
branches:
include:
- master
pr:
branches:
include:
- master
variables:
azureContainerRegistry: <registryUrl>
azureContainerRegistryServiceConnection: <serviceConnection>
kubernetesConfigPath: kubernetes
kubernetesNamespace: <my-namespace>
major: 0
buildNumber: $(major).$(Build.BuildId)
imageName: "$(azureContainerRegistry)/<my-app>:$(buildNumber)"
stages:
- stage: Bake
displayName: "Build and Push image"
jobs:
- job: Validate
displayName: "Build image"
pool:
name: "Docker"
steps:
- script: docker build -t $(imageName) .
displayName: Build App
- job: Publish
displayName: "Push image"
dependsOn: Validate
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
pool:
name: "Docker"
steps:
- task: Docker@2
displayName: Login to Container Registry
inputs:
command: login
containerRegistry: $(azureContainerRegistryServiceConnection)
- script: docker push $(imageName)
displayName: PUSH $(imageName)
- stage: DeployTest
displayName: "Deploy TEST"
dependsOn: Bake
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
jobs:
- deployment: Deploy
environment: <my-test-env>.$(kubernetesNamespace)
pool:
name: "Docker"
strategy:
runOnce:
deploy:
steps:
- task: qetza.replacetokens.replacetokens-task.replacetokens@3
displayName: "Replace tokens"
inputs:
targetFiles: $(kubernetesConfigPath)/base/*.yaml
escapeType: none
tokenPrefix: "{"
tokenSuffix: "}"
- task: Kubernetes@1
displayName: "kubectl apply"
inputs:
namespace: $(kubernetesNamespace)
command: apply
arguments: -k $(kubernetesConfigPath)/test
versionSpec: 1.7.0
checkLatest: true
- task: Kubernetes@1
displayName: "kubectl rollout status"
inputs:
namespace: $(kubernetesNamespace)
command: rollout
arguments: "status deployments/<my-app>"
versionSpec: 1.7.0
checkLatest: true
- stage: DeployProd
displayName: "Deploy PROD"
dependsOn: DeployTest
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
jobs:
- deployment: Deploy
environment: <my-prod-env>.$(kubernetesNamespace)
pool:
name: "Docker"
strategy:
runOnce:
deploy:
steps:
- task: qetza.replacetokens.replacetokens-task.replacetokens@3
displayName: "Replace tokens"
inputs:
targetFiles: $(kubernetesConfigPath)/base/*.yaml
escapeType: none
tokenPrefix: "{"
tokenSuffix: "}"
- task: Kubernetes@1
displayName: "kubectl apply"
inputs:
namespace: $(kubernetesNamespace)
command: apply
arguments: -k $(kubernetesConfigPath)/prod
versionSpec: 1.7.0
checkLatest: true
- task: Kubernetes@1
displayName: "kubectl rollout status"
inputs:
namespace: $(kubernetesNamespace)
command: rollout
arguments: "status deployments/<my-app>"
versionSpec: 1.7.0
checkLatest: true
从旧版本的代码重新部署是实现它的方法。
可以手动运行一个新的管道并以给定的提交为目标但这在Devops UI中实现起来相当麻烦,而且看起来容易出错
这是你需要一个组织良好的源代码控制分支和标记策略。如果您以前从分支"release"/"release-20212710.01"部署,然后从分支"release"/"release-20212710.02"部署,则不需要进行任何代码更改。回滚仅仅意味着选择旧的分支——它仍然存在,代码与之前相同——并进行部署。