Azure Pipelines:在 Azure 中的 Kubernetes 集群部署失败后回滚到特定版本(发布)-Pipelines.yml

Azure Pipelines: Rollback to a certain version (release) after failed deployment to Kubernetes cluster in Azure-Pipelines.yml

我正在使用 Azure DevOps(管道 -> 管道)创建我的 CD 管道以发布到生产环境。我了解使用 Pipelines 版本(Pipelines -> Releases)我能够 select 并重新部署以前的版本。我想知道是否可以在我的 Azure 管道中使用 kubectl task rollout undo 命令来做到这一点,我该如何去做。如果您以前遇到过这种情况,如果您能分享这方面的知识,我将不胜感激。

以下是我的 Azure 管道中的 kubectl 任务代码:

           - task: Kubernetes@1
            inputs:
              connectionType: 'Azure Resource Manager'
              azureSubscriptionEndpoint: 'Azure subscription 1(xxxxxxx-xxx-xxxxx-xxxxxxx)'
              azureResourceGroup: 'rg'
              kubernetesCluster: 'kc'
              command: 'apply'
              useConfigurationFile: true
              configuration: '$(Pipeline.Workspace)/manifests/'
              secretType: 'dockerRegistry'
              containerRegistryType: 'Azure Container Registry'

          - task: Kubernetes@1
            name: rollout_status
            inputs:
              connectionType: 'Azure Resource Manager'
              azureSubscriptionEndpoint: 'Azure subscription 1(xxxxxxx-xxx-xxxxx-xxxxxxx)'
              azureResourceGroup: 'rg'
              kubernetesCluster: 'kc'
              command: 'rollout'
              arguments: 'status deployment/deployment-name'
              secretType: 'dockerRegistry'
              containerRegistryType: 'Azure Container Registry'
          
          - task: Kubernetes@1
            name: rollout_undo
            condition: failed()
            inputs:
              connectionType: 'Azure Resource Manager'
              azureSubscriptionEndpoint: 'Azure subscription 1(xxxxxxx-xxx-xxxxx-xxxxxxx)'
              azureResourceGroup: 'rg'
              kubernetesCluster: 'kc'
              command: 'rollout'
              arguments: 'undo deployment/deployment-name'
              secretType: 'dockerRegistry'
              containerRegistryType: 'Azure Container Registry'

根据 Yaml 示例,如果 rollout_status 步骤失败,它应该能够回滚。

要回滚到某个版本,您可以尝试使用以下命令:

kubectl rollout undo deployment/deployment-name --to-revision=2

请参考this Blog:

By default Kubernetes stores the last 10 ReplicaSets and lets you roll back to any of them.

因此您可以将参数 (--to-revision=x) 添加到参数中以指定版本。