title | description | ms.topic | ms.author | author | ms.date |
---|---|---|---|---|---|
Integrate Azure DevTest Labs into Azure Pipelines |
Learn how to integrate Azure DevTest Labs into Azure Pipelines continuous integration and delivery (CI/CD) pipelines. |
how-to |
rosemalcolm |
RoseHJM |
11/16/2021 |
You can use the Azure DevTest Labs Tasks extension to integrate Azure DevTest Labs into Azure Pipelines continuous integration and delivery (CI/CD) pipelines. The extension installs several tasks into Azure Pipelines, including:
- Create a virtual machine (VM)
- Create a custom image from a VM
- Delete a VM
These tasks make it easy to, for example, quickly deploy a golden image VM, run a specific test, and then delete the VM.
This article shows how to use Azure DevTest Labs Tasks to create and deploy a VM, create a custom image, and then delete the VM, all in one release pipeline. You'd ordinarily perform these tasks separately in your own build, test, and deployment pipelines.
[!INCLUDE devtest-lab-try-it-out]
-
In the Azure portal, create a DevTest Labs lab, or use an existing one.
-
Register or sign into your Azure DevOps Services organization, and create a project, or use an existing project.
-
Install the Azure DevTest Labs Tasks extension from Visual Studio Marketplace:
- Go to Azure DevTest Labs Tasks.
- Select Get it free.
- Select your Azure DevOps Services organization from the dropdown list, and then select Install.
First, construct an Azure Resource Manager (ARM) template that creates a lab VM on demand.
- In your lab in the Azure portal, select Add in the top menu bar.
- On the Choose a base screen, select a Windows base image for the VM.
- On the Create lab resource screen, under Artifacts, select Add or Remove Artifacts.
- On the Add artifacts screen, search for winrm, and then select the arrow next to Configure WinRM.
- On the Add artifact pane, enter a fully qualified domain name (FQDN) for the VM, such as
contosolab00000000000000.westus3.cloudapp.azure.com
. Select OK, and then select OK again. - Select the Advanced Settings tab, and for IP Address, select Public.
[!NOTE] If you use the WinRM artifact with a shared IP address, you must add a network address translation (NAT) rule to map an external port to the WinRM port. You don't need the NAT rule if you create the VM with a public IP address. For this walkthrough, create the VM with a public IP address.
- Select View ARM template.
- Copy the template code and save it as a file named CreateVMTemplate.json in your local source control branch.
- Check in the template to your project's source control system.
For more information and details, see Use a Resource Manager template.
Next, create a script to collect the values that task steps like Azure File Copy and PowerShell on Target Machines use to deploy apps to VMs. You'd ordinarily use these tasks to deploy your own apps to your Azure VMs. The tasks require values such as the VM resource group name, IP address, and FQDN.
[!INCLUDE updated-for-az]
Save the following script with a name like GetLabVMParams.ps1, and check it in to your project's source control system.
Param( [string] $labVmId)
$labVmComputeId = (Get-AzResource -Id $labVmId).Properties.ComputeId
# Get lab VM resource group name
$labVmRgName = (Get-AzResource -Id $labVmComputeId).ResourceGroupName
# Get the lab VM Name
$labVmName = (Get-AzResource -Id $labVmId).Name
# Get lab VM public IP address
$labVMIpAddress = (Get-AzPublicIpAddress -ResourceGroupName $labVmRgName
-Name $labVmName).IpAddress
# Get lab VM FQDN
$labVMFqdn = (Get-AzPublicIpAddress -ResourceGroupName $labVmRgName
-Name $labVmName).DnsSettings.Fqdn
# Set a variable labVmRgName to store the lab VM resource group name
Write-Host "##vso[task.setvariable variable=labVmRgName;]$labVmRgName"
# Set a variable labVMIpAddress to store the lab VM Ip address
Write-Host "##vso[task.setvariable variable=labVMIpAddress;]$labVMIpAddress"
# Set a variable labVMFqdn to store the lab VM FQDN name
Write-Host "##vso[task.setvariable variable=labVMFqdn;]$labVMFqdn"
Next, create the release pipeline in Azure Pipelines. The pipeline tasks use the values you assigned to the VM when you created the ARM template.
- From your Azure DevOps Services project page, select Pipelines > Releases from the left navigation.
- Select Create release.
- On the New release pipeline page, select the Variables tab.
- Select Add, and enter the following Name and Value pairs, selecting Add after adding each one.
- vmName: The VM name you assigned in the ARM template.
- userName: The username to access the VM.
- password: Password for the username. Select the lock icon to hide and secure the password.
The next step creates a golden image VM to use for future deployments. This step uses the Azure DevTest Labs Create VM task.
-
On the new release pipeline page, on the Pipeline tab, select the hyperlinked text in Stage 1.
-
In the left pane, select the plus sign + next to Agent job.
-
Under Add tasks in the right pane, search for and select Azure DevTest Labs Create VM, and select Add.
-
In the left pane, select the Create Azure DevTest Labs VM task.
-
In the right pane, fill out the form as follows:
-
Azure RM Subscription: Select your service connection or subscription from the dropdown list, and select Authorize if necessary.
[!NOTE] For information about creating a more restricted permissions connection to your Azure subscription, see Azure Resource Manager service endpoint.
-
Lab: Select your DevTest Labs lab name.
-
Template: Browse to and select the template file you checked in to your project repository.
-
Parameters File: Browse to and select the parameters file you checked in to your repository.
-
Parameter Overrides: Enter
-newVMName '$(vmName)' -userName '$(userName)' -password (ConvertTo-SecureString -String '$(password)' -AsPlainText -Force)
. -
Drop down Output Variables, and under Reference name, enter the variable for the created lab VM ID. If you use the default labVmId, you can refer to the variable in subsequent tasks as $(labVmId).
You can create a name other than the default, but remember to use the correct name in subsequent tasks. You can write the Lab VM ID in the following form:
/subscriptions/{subscription Id}/resourceGroups/{resource group Name}/providers/Microsoft.DevTestLab/labs/{lab name}/virtualMachines/{vmName}
.
-
Next, the pipeline runs the script you created to collect the details of the DevTest Labs VM.
- On the release pipeline Pipeline tab, select the hyperlinked text in Stage 1, and then select the plus sign + next to Agent job.
- Under Add tasks in the right pane, search for and select Azure PowerShell, and select Add.
- In the left pane, select the Azure PowerShell script: FilePath task.
- In the right pane, fill out the form as follows:
- Azure Subscription: Select your service connection or subscription.
- Script Type: Select Script File Path.
- Script Path: Browse to and select the PowerShell script that you checked in to your source code repository. You can use built-in properties to simplify the path, for example:
$(System.DefaultWorkingDirectory/Scripts/GetLabVMParams.ps1
. - Script Arguments: Enter the name of the labVmId variable you populated in the previous task, for example -labVmId '$(labVmId)'.
The script collects the required values and stores them in environment variables within the release pipeline, so you can refer to them in later steps.
The next task creates an image of the newly deployed VM in your lab. You can use the image to create copies of the VM on demand to do developer tasks or run tests.
- On the release pipeline Pipeline tab, select the hyperlinked text in Stage 1, and then select the plus sign + next to Agent job.
- Under Add tasks, select Azure DevTest Labs Create Custom Image, and select Add.
- In the left pane, select the Azure DevTest Labs Create Custom Image task.
- In the right pane, fill out the form as follows:
- Azure RM Subscription: Select your service connection or subscription.
- Lab: Select your lab.
- Custom Image Name: Enter a name for the custom image.
- Description: Enter an optional description to make it easy to select the correct image.
- Source Lab VM: The source labVmId. If you changed the default name of the labVmId variable, enter it here. The default value is $(labVmId).
- Output Variables: You can edit the name of the default Custom Image ID variable if necessary.
You can add tasks to deploy your app to the new DevTest Labs VM. If you only want to experiment with creating a DevTest Labs VM and a custom image, without deploying an app, you can skip this step.
The tasks you usually use to deploy apps are Azure File Copy and PowerShell on Target Machines. You can find the VM information you need for the task parameters in three configuration variables named labVmRgName, labVMIpAddress, and labVMFqdn within the release pipeline.
The final task is to delete the VM that you deployed in your lab. You'd ordinarily delete the VM after you do the developer tasks or run the tests that you need on the deployed VM.
- On the release pipeline Pipeline tab, select the hyperlinked text in Stage 1, and then select the plus sign + next to Agent job.
- Under Add tasks, select Azure DevTest Labs Delete VM, and select Add.
- Configure the task as follows:
- Azure RM Subscription: Select your service connection or subscription.
- Lab: Select your lab.
- Virtual Machine: Select the VM you want to delete.
- Output Variables: Under Reference name, if you changed the default name of the labVmId variable, enter it here. The default value is $(labVmId).
To save the new release pipeline:
- Select New release pipeline at the top of the release pipeline page, and enter a new name for the pipeline.
- Select Save at upper right.
To create and run a release using the new pipeline:
- On the release pipeline page, select Create release at upper right.
- Under Artifacts, select the latest build, and then select Create.
At each release stage, you can refresh the view of your lab in the Azure portal to see the VM creation, image creation, and VM deletion.
You can use the custom image to create VMs whenever you need them.
- Learn how to Create multi-VM environments with ARM templates.
- Explore more quickstart ARM templates for DevTest Labs automation from the public DevTest Labs GitHub repo.
- If necessary, see Azure Pipelines troubleshooting.