Run Kubectl DevOps task with run-time specified resource details

1/18/2021

We're building out a release pipeline in Azure DevOps which pushes to a Kubernetes cluster. The first step in the pipeline is to run an Azure CLI script which sets up all the resources - this is an idempotent script so we can run it each time we run the release pipeline. Our intention is to have a standardised release pipeline which we can run against several clusters, existing and new.

The final step in the pipeline is to run the Kubectl task with the apply command.

However, this pipeline task requires specifying in advance (at the time of building the pipeline) the names of the resource group and cluster against which it should be executed. But the point of the idempotent script in the first step is to ensure that the resources and to create if not.

So there's the possibility that neither the resource group nor the cluster will exist before the pipeline is run.

How can I achieve this in a DevOps pipeline if the Kubectl task requires a resource group and a cluster to be specified at design time?

enter image description here

-- awj
azure-aks
azure-devops
azure-pipelines-release-pipeline
kubernetes

1 Answer

1/19/2021

This Kubectl task works with service connection type: Azure Resource Manager. And it requires to select Resource group field and Kubernetes cluster field after you select the Azure subscription, as below. enter image description here

After testing, we find that these 2 fields supports variable. Thus you could use variable in these 2 fields, and using PowerShell task to set variable value before this Kubectl task. See: Set variables in scripts for details.

-- Edward Han-MSFT
Source: StackOverflow