Skip to content

Latest commit

 

History

History
170 lines (121 loc) · 7.9 KB

Deploy_build_agent.md

File metadata and controls

170 lines (121 loc) · 7.9 KB

Deploy build agent

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:

  1. It creates new Azure Virtual Machine from image created by Build Agent Image. Additionally it removes other resources in case previous deployment failed.
  2. It removes existing build agent from Azure DevOps pool. Then it installs Azure DevOps agent on the new virtual machine.
  3. It deletes Azure Virtual Machine running previous build agent.

Setup release pipeline

  1. From menu select Pipelines then Releases.
  2. Click New release pipeline.
  3. Select Empty job (no template).
  4. 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.

Release variables

  1. Open Variable groups and link following variable groups: Azure resources, Build Agent properties, Azure DevOps Account

Release variable groups

  1. Open Pipeline tab.
  2. Click Add artifact.
  3. Set following properties:
    • Project: Current Azure DevOps project
    • Source (build pipeline): Build Agent Image
    • Default version: Latest
    • Source alias: AgentImage
  4. Click Add.

Artifact - AgentImage

  1. Click Add artifact.
  2. 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
  3. Click Add.

Artifact - BuildScripts

  1. Select stage in the release and change name to Azure DevOps Default Queue.

Release stage

  1. Open Tasks tab.
  2. Select first job and set following properties:
    • Display name: Create virtual machine
    • Agent pool: Default

Create Virtual Machine job

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

Clean up Azure virtual machines

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

Create virtual machine

  1. At Deployment Process click Add an agentless job.

Add an agentless job

  1. Set Display name to "Wait for virtual machine".

Wait for virtual machine

  1. Add task Delay and set properties:
    • Display name: Delay by 2 minutes
    • Delay time (minutes): 2

Delay by 2 minutes

  1. At Deployment Process click Add an agent job.

Add an agent job

  1. Set properties of the job:
    • Display name: Install build agent
    • Agent pool: Hosted VS2017

Install build agent job

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

Delete old build agent

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

Install build agent

  1. At Deployment Process click Add an agentless job.

Add an agentless job

  1. Set Display name to "Wait for build agent".

Wait for virtual machine

  1. Add task Delay and set properties:
    • Display name: Delay by 2 minutes
    • Delay time (minutes): 2

Delay by 2 minutes

  1. At Deployment Process click Add an agent job.

Add an agent job

  1. Set properties of the job:
    • Display name: Delete old virtual machine
    • Agent pool: Default

Delete old virtual machine job

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

Delete old virtual machine

  1. Save the release pipeline and name it Azure DevOps Build Agent.
  2. 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.