Now the build agent VM image is built and stored in Azure Storage. The last step is to deploy the agent using Azure DevOps Release Pipeline. The release has 3 jobs:
- It creates new Azure Virtual Machine from image created by Build Agent Image. Additionally it removes other resources in case previous deployment failed.
- It removes existing build agent from Azure DevOps pool. Then it installs Azure DevOps agent on the new virtual machine.
- It deletes Azure Virtual Machine running previous build agent.
- From menu select Pipelines then Releases.
- Click New release pipeline.
- Select Empty job (no template).
- Select Variables tab and enter following variables:
Name | Value | Hidden | Scope |
---|---|---|---|
Azure.ServicePrincipalObjectId | Value of $spClientId returned by SetupPacker.ps1 script. | true | Release |
Azure.VirtualMachinePrefix | Prefix that will be added to VM name. For example, when prefix is VSBuild1V, then VM is named VSBuild1VXR (X is release number). | Release |
Note: Virtual Machine Prefix can contain only letters, numbers, and '-', and must have no more than 12 characters.
- Open Variable groups and link following variable groups: Azure resources, Build Agent properties, Azure DevOps Account
- Open Pipeline tab.
- Click Add artifact.
- Set following properties:
- Project: Current Azure DevOps project
- Source (build pipeline): Build Agent Image
- Default version: Latest
- Source alias: AgentImage
- Click Add.
- Click Add artifact.
- Set following properties:
- Project: Current Azure DevOps project
- Source (build pipeline): Build Scripts
- Default version: Latest from the build pipeline default branch with tags
- Tags: empty
- Source alias: BuildScripts
- Click Add.
- Select stage in the release and change name to Azure DevOps Default Queue.
- Open Tasks tab.
- Select first job and set following properties:
- Display name: Create virtual machine
- Agent pool: Default
- Add task Azure PowerShell and set properties:
- Display name: Clean up Azure virtual machines
- Azure Connection Type: Azure Resource Manager
- Azure Subscription: Azure Build
- Script Type: Script File Path
- Script Path:
$(System.DefaultWorkingDirectory)/BuildScripts/deploy/DeleteVMs.ps1
- Script Arguments:
-RGName "$(Azure.ResourceGroup)" -ExceptVMs @("$(Agent.MachineName)")
- ErrorActionPreference: Stop
- Azure PowerShell Version: Latest installed version
- Add task Azure PowerShell and set properties:
- Display name: Create virtual machine
- Azure Connection Type: Azure Resource Manager
- Azure Subscription: Azure Build
- Script Type: Script File Path
- Script Path:
$(System.DefaultWorkingDirectory)/BuildScripts/deploy/CreateVM.ps1
- Script Arguments:
-RGName "$(Azure.ResourceGroup)" -Location "$(Azure.Location)" -VHDUriFile "$(System.DefaultWorkingDirectory)\AgentImage\image\ImageOutput.txt" -VMName "$(Azure.VirtualMachinePrefix)$(Release.ReleaseId)R" -Username "$(BuildAgent.Username)" -Password "$(BuildAgent.Password)" -ServicePrincipalObjectId "$(Azure.ServicePrincipalObjectId)"
- ErrorActionPreference: Stop
- Azure PowerShell Version: Latest installed version
- At Deployment Process click Add an agentless job.
- Set Display name to "Wait for virtual machine".
- Add task Delay and set properties:
- Display name: Delay by 2 minutes
- Delay time (minutes): 2
- At Deployment Process click Add an agent job.
- Set properties of the job:
- Display name: Install build agent
- Agent pool: Hosted VS2017
- Add task PowerShell and set properties:
- Display name: Delete old build agent
- Type: File Path
- Script Path:
$(System.DefaultWorkingDirectory)/BuildScripts/deploy/DeleteAgent.ps1
- Script Arguments:
-VstsUrl "$(System.TeamFoundationCollectionUri)" -AuthToken "$(ADOAccount.PAT)" -AgentName "$(Azure.VirtualMachinePrefix)*"
- ErrorActionPreference: Stop
- Add task Azure PowerShell and set properties:
- Display name: Install build agent
- Azure Connection Type: Azure Resource Manager
- Azure Subscription: Azure Build
- Script Type: Script File Path
- Script Path:
$(System.DefaultWorkingDirectory)/BuildScripts/deploy/InstallAgent.ps1
- Script Arguments:
-RGName "$(Azure.ResourceGroup)" -VMName "$(Azure.VirtualMachinePrefix)$(Release.ReleaseId)R" -Username "$(BuildAgent.Username)" -Password "$(BuildAgent.Password)" -VstsUrl "$(System.TeamFoundationCollectionUri)" -PAT "$(ADOAccount.PAT)"
- ErrorActionPreference: Stop
- Azure PowerShell Version: Latest installed version
- At Deployment Process click Add an agentless job.
- Set Display name to "Wait for build agent".
- Add task Delay and set properties:
- Display name: Delay by 2 minutes
- Delay time (minutes): 2
- At Deployment Process click Add an agent job.
- Set properties of the job:
- Display name: Delete old virtual machine
- Agent pool: Default
- Add task Azure PowerShell and set properties:
- Display name: Delete old virtual machine
- Azure Connection Type: Azure Resource Manager
- Azure Subscription: Azure Build
- Script Type: Script File Path
- Script Path:
$(System.DefaultWorkingDirectory)/BuildScripts/deploy/DeleteVMs.ps1
- Script Arguments:
-RGName "$(Azure.ResourceGroup)" -ExceptVMs @("$(Agent.MachineName)") -RemovePublicAccess:$true
- ErrorActionPreference: Stop
- Azure PowerShell Version: Latest installed version
- Save the release pipeline and name it Azure DevOps Build Agent.
- Click Create a release. Confirm build versions to use and click Create.
After about 30 minutes the release should be finished and you should have your build agent ready.