In this article I will demonstrate a couple of ways to deploy an Azure Resource Group using Azure DevOps.
Setup
To get started you will need to create an Azure DevOps project and a repository.
I created a repository in GitHub, called SampleAzureDevOpsResourceGroupDeployment
, which can be found at https://github.com/mattruma/SampleAzureDevOpsResourceGroupDeployment.
You can also create the GIT repository in Azure DevOps, does not really matter where it is located, Azure DevOps just needs access to it.
Now create an Azure DevOps project, I called mine SampleAzureDevOpsResourceGroupDeployment
, this is where we will create our Release Pipeline to deploy our Azure Resource Group.
We will now look at how to deploy our Azure Resource Group with the Azure CLI and Azure Powershell.
Azure CLI
The Azure command-line interface (CLI) is Microsoft’s cross-platform command-line experience for managing resources in Azure.
Navigate to the Azure DevOps portal.
Navigate to Pipelines, then to Releases, and click New pipeline.
Select Empty job for the template.
Rename the Release Pipeline to Azure CLI Release
.
Select Add Artifact.
Select where your repository is originating from, in my case, I will select GitHub.
Note, I had to create a GitHub Service Connection to connect Azure DevOps to GitHub, you can read more how to do this at https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#sep-github.
Select the SampleAzureDevOpsResourceGroupDeployment repository, select the master branch and then click Add.
Select the lightning bolt icon next to the artifact and Enable the Continuous deployment trigger.
Then select the 1 job, 0 task link, this is where we will add the step to deploy the Azure Resource Group to Azure.
Click the Add a task to Agent job button.
Search for cli
, select the Azure CLI task and click Add.
Click on the newly added Azure CLI task and select the Azure Subscription where the Azure Resource Group will be created, you may have to Authorize the Azure subscription prior to completing this step.
For Script Location, select Inline script.
Next, add the Inline script.
1 |
az group create --name YOUR_AZURE_RESOURCE_GROUP_NAME --location eastus2 |
Replace the text YOUR_AZURE_RESOURCE_GROUP_NAME
with whatever you want to call your Azure Resource Group.
Click Save.
Click Create Release to kick-off the release pipeline.
Once the release pipeline completes, navigate to the Azure Portal to verify the Azure Resource Group was created.
Azure Powershell
Delete the Azure Resource Group you just created, we will recreate it in this step using Azure Powershell.
To save some time, we will Clone the Azure CLI Release pipeline.
Navigate to the Azure CLI Release pipeline, select the ellipsis and select Clone.
Rename the release pipeline, Azure Powershell Release
.
Click the 1 job, 1 task link.
Select the Azure CLI task and click Remove.
Click the Add a task to Agent job button.
Search for powershell
, select the Azure PowerShell task and click Add.
Click on the newly added Azure CLI task and select the Azure Subscription where the Azure Resource Group will be created.
For Script Location, select Inline script.
Next, add the Inline script.
1 2 3 4 |
$Name = "YOUR_AZURE_RESOURCE_GROUP_NAME" $Location = "eastus2" New-AzureRmResourceGroup -Name $Name -Location $Location |
Replace the text YOUR_AZURE_RESOURCE_GROUP_NAME
with whatever you want to call your Azure Resource Group.
Set Azure PowerShell Version to Latest installed version.
Click Save, and then click Create Release to kick-off the release pipeline.
Once the release pipeline completes, navigate to the Azure Portal to verify the Azure Resource Group was created.
Wrap Up
What if the Azure Resource Group already exists?
Great question!
The Azure Resource Group will NOT be deleted, it will just be updated. Any existing resources in the Azure Resource Group will be unaffected.
One thing to note, when I reran the Azure PowerShell Release, the following error would be generated, and the task would fail:
Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.
This was caused by a prompt to confirm updating of the existing Azure Resource Group.
To disable this prompt, you just need to add -Force
to the end of the PowerShell command.
A Better Way
Are there other ways to do this? Are there better ways to do this? Probably! If you know of one, or have some feedback on how this article might be more relevant, please share with me!
Well…it’s a problem that will be solved by wiser heads than mine. I’m along for the fun.
Tasslehoff Burrfoot
Discover more from Matt Ruma
Subscribe to get the latest posts sent to your email.
The -Force trick did trick for me! Thank you for sharing