Setting up an Azure VM DevOps Build agent

Microsoft provides several build agents for your Azure DevOps builds and these are packed with features. Unfortunately, they have one disadvantage – you always get a completely clean agent for each build. This is, of course, necessary for security, but it also means there can be no caching of dependencies and packages which results in longer build times. Luckily, you can quite easily set up your own Azure VM build agent which will work around this issue.

Creating the VM

In the Azure Portal click the green plus button in the menu and search for “Visual Studio” in the search box:

Visual Studio 2019 VM image
Visual Studio 2019 VM image

The Visual Studio 2019 and Visual Studio 2019 Latest images are great starting points for a build agent as they come with a pre-installed Visual Studio 2019 with all workloads enabled.

Selecting an edition of Visual Studio
Selecting an edition of Visual Studio

In the image intro page, you can select an edition of Visual Studio as well and then click Create.

In the wizard, we need to select the resource group to which we want to assign this new VM, set the VM name and its size. I recommend one of the burstable VMs (starting with B). These are generally cheaper as they are optimized for workloads where they are idle most of the time but need to burst to high performance for short periods of time – which is exactly the case for our build agent.

Burstable VMs
Burstable VMs

Next, set up the administrator credentials and then go to the Inbound port rules section and allow RDP connections in the dropdown:

Inbound port rules
Inbound port rules

This way you will be able to connect to your newly created VM via a Remote Desktop Connection to set up the build agent further. You can always disable this port later.

These are all required settings, so you can just click to the last Review tab or customize further if you want. Finally, click Create on the review page and let your VM to deploy.

Preparing Azure DevOps

Next up, go to your Azure DevOps dashboard, click your profile picture in the upper-right corner and select Security.

Security
Security

Select Personal Access Tokens in the left-hand menu and then click New Token. In the dialog pane, set the name, and expiration. The expiration does not have to be too long, it is used just for the initial set up of the build agent and then should no longer be needed. In the Scopes section, set Custom defined and click Show all scopes link which is at the bottom of the dialog. Then find Agent Pools scope and check the Read & Manage permission. For deployment group agents also check the same permission in the Deployment Groups scope.

Click Create and you will be shown the newly generated token. Make sure to copy it somewhere (like notepad), we will need it later and you cannot display it again. Once done, Close the pane.

Personal Access Token
Personal Access Token

Now that we have our access token, let’s get the agent installer. Go to the Azure DevOps organization dashboard and click Organization settings in the lower left corner. In the menu, find Agent pools in the Pipelines section.

Click the New agent pool… button and enter the desired name of your new agent pool.

New agent pool dialog
New agent pool dialog

Select your newly created pool in the list and then click the Download agent button. As we are creating a Windows x64 agent, just click Download which will start the download of a zip archive with the agent setup.

Connecting to VM

By now the VM should be running, so let’s go to Azure Portal and find our VM. We can do so by its name or just go to Virtual Machines or our Resource Group and find it there.

Click the Connect button on the VM dashboard and then in the newly opened blade click Download RDP File:

Download RDP File
Download RDP File

Open the .rdp file you just downloaded. It will ask if you trust the remote computer, which you can because it is yours 😉 , so just check the confirmation checkbox and click Connect.

You will get a Windows Security popup. Click More choices and then select Use a different account. Then fill out the credentials you have chosen during VM creation and click OK.

You will get another security popup, which you just confirm with Yes and in a few moments, you will be on your VM’s desktop.

Configuring the agent

Once the VM desktop loads, find the agent zip archive we previously downloaded on your PC and copy it into clipboard (Ctrl+C or right-click and Copy) and then go back to the VM  and paste it there (Ctrl+V or right-click and Paste). You will see a copy dialog:

Copy dialog
Copy dialog

This will take a while according to the speed of your internet connection. When done, extract the archive (right-click, Extract All…). Go to the extracted folder, right-click config.cmd and choose Run as administrator.

Run config as Admin
Run config as Admin

This will launch the configuration wizard which will walk you through the rest of the setup process. First, enter your Azure DevOps URL (in the format “https://dev.azure.com/ORGANIZATION/“or “https://ORGANIZATION.visualstudio.com/“).

Next, we need to choose authentication type. As we are using PAT, just press enter. Now enter the Personal Access Token we created earlier.

This should successfully connect to the server.

Ready to register agent
Ready to register agent

It should now be ready to register the agent. First, enter the Agent Pool – enter the same name as when you created it earlier in Azure DevOps – in my case it was My Agents. Next enter the agent name and confirm. You will also be prompted to enter the work folder, but you can just leave the default.

Finally, decide if you want to run the agent interactively or as a service. The service-based agent is less obtrusive and runs automatically in the background, but the interactive option gives you a console where you can see the status and can run UI tests as well. In case you select the interactive agent, you will also be able to set it to run automatically after startup. This is useful, as it will run automatically after the VM restarts.

Whichever option you choose, I recommend you to choose the user account under which the service will run. The default is a built-in network user, but this one does not have access to the work folder by default and in such case, you will have to grant it the required permissions. If you use your admin account instead, it will run without hiccups.

Using the agent in Azure DevOps

Now that the agent has been added to Azure DevOps, go to your build pipeline Edit it and select your new agent pool in the Agent drop down. Save and queue your build and you are done!

Selecting your agent pool
Selecting your agent pool

Summary

The first time your build will probably not run much faster than with the default agents, but any subsequent builds will just reuse the cached packages so the NuGet or NPM restore phase will be as quick as a few seconds instead of minutes. And that’s a significant improvement! A custom agent can also help you in case you are using some specific tools which are not available in the default agents.

Buy me a coffeeBuy me a coffee

9 thoughts on “Setting up an Azure VM DevOps Build agent”

  1. I do trust all of the concepts you’ve introduced on your
    post. They are very convincing and will definitely work.
    Nonetheless, the posts are too brief for beginners.
    May just you please prolong them a bit from next time? Thanks for the post.

  2. Hi nice article,

    But when i create the new azure agent it is not pushing all DLL files. Due to my .net core API application is throwing 502.5 error.

    Can you help me on this ?

    1. Hi, I know it is delayed response but marking it as it may help other.

      I got fixes for the issue. The actual issue was with the OS, we were deploying the new Agent on the Windows server 2019. That time the azure agent may not compatible with latest version. I tried configuring azure agent on windows 10 and that worked well.

      So some time the OS version or the Service pack may also issue.

      Thanks !

  3. Neat blog! Is your heme custom made or did you dwnload it from somewhere?
    A theme like yours with a few simple adjustements would really make
    my bkog shine. Please let me know where you ggot your design. Many thanks

    Takke a look at my site architectural consultancy services (Janie)

  4. Hi Martin
    I am very happy about your post, its gives me so much value to read about this setup.
    One question though:
    Can you tell me a bit more about VM cost control, I believe the amount of money baid monthly for e.g. BSms VM is for the pure raw machine power and data consumptions is added on top? and what payment solution do you recommend for a Selfhosted build machine: PAYG og reserved(monthly or yearly) and how can you use automation on VM’s, so it dont consume data while in idle?
    Hope to hear from you, your blog is gold mine of good knowledge for me.
    Thanks
    Christian

  5. May I just say what a comfort to uhcover somebody who really understands what they’re talking about on the net.
    You certainly understand how to bring a problem to light andd
    make it important. More people must look at this and understand this side off the story.
    It’s surprising you aren’t more popular given that you certainly
    have the gift.

    My page Residential Bathroom Design

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.