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

1.3k views Asked by At

I am using Azure DevOps (Pipelines -> Pipelines) to create my CD pipeline to release to production. I understand that using Pipelines releases (Pipelines -> Releases) I am able to select and redeploy a previous release version. I would like to know if it is possible to do so using kubectl task rollout undo command in my Azure pipelines and how can I go about doing it. I appreciate if you could share your knowledge on this if you have previously encountered this.

The following is my kubectl task code in my Azure pipelines:

           - 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'
1

There are 1 answers

2
Kevin Lu-MSFT On BEST ANSWER

From the Yaml sample, it should be able to roll back if the rollout_status step fails.

To rollback to a certain version, you could try to use the following command:

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

Please refer to this Blog:

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

So you could add the parameter (--to-revision=x) to arguments to specify the revision.