Run Azure Pipelines using Local Server

Introduction

Azure Pipelines is a cloud-based tool that automates the building and testing of code projects. It is compatible with a wide range of languages and project types, seamlessly integrating continuous integration (CI) and continuous delivery (CD) processes. This platform ensures thorough testing, efficient code building, and reliable code delivery to various destinations.

These pipelines run on Microsoft-hosted agents(which is a paid service) or we can create self-hosted agents in a VM or local machine. In this article, we will discuss Use Cases and how we can configure our system to work as an agent to run these pipelines followed by a demo using Power Platform Task.

Use Cases

  1. Cost Effective: Self-hosted Agent and that too on your own local machine is very cost-effective. Especially for students and learning professionals. We don't need to worry about trial completion or frequent update issues in Microsoft Hosted agents
  2. More Control/ Easy Troubleshooting: Microsoft-hosted agents act as a black box, a user does not have much control. In case of pipeline failure due to agent issues user has to wait sometimes days for a resolution of the ticket raised. With self-hosted agents troubleshooting becomes easy and the user has complete control over the agent.
  3. Availability/ Timeout issues: 24*7 availability is a great advantage that self-hosted agents can provide and users do not need to wait in case of service unavailability of Microsoft-hosted agents.

There might be several other use cases as well. Now let's discuss how we can achieve the above demands.

Steps to setup self-hosted agent

Step 1. Go to https://dev.azure.com/<organization name>/<project name>/ -> Click on Project Settings as shown in the image below.

Project settings

Step 2. In Project Settings, click on Agent Pools under Pipeline Section -> Select Default Pool from the list of pools as shown below.

Agent pools

Step 3. In the Default pool, click on Agents Tab -> Click on New Agent Button as shown below.

Agents

Step 4. A pop will appear. Select Windows OS -> Click on x64 architecture (Configuration can be selected based on user need)-> Click on Download as shown below.

Windows

Step 5. A zip file will be downloaded. Extract that zip file in path 'C:\Agents'. Then run the config.cmd file is shown in the image below.

config.cmd

Step 6. A new command line terminal will open. Enter the server URL in the format: https://dev.azure.com/<your-organization>.

Press Enter for Authentication type PAT as shown below.

Step 7. Now let's generate PAT. Go back to Azure DevOps. Click on User Settings -> Select Personal Access Token as shown in the image below:

Personal access token

Step 8. In Personal Access Token Page, Click on New Token -> Given token a Name -> Set Expiration Duration -> Set access as Custom -> Give only Read & Manage permission on Agent Pools -> Click on Create

New token

Step 9. PAT will be generated. Keep a copy of this PAT as it will not be visible later. Paste this PAT in the command line terminal as shown below:

Azure pipeline

Step 10. Press enter to select the Default agent pool. Enter Agent Name -> Press enter to select  _work as work folder -> Press enter to run agent in interactive mode -> Press Enter again to skip autologin configuration as shown in the image below.

Agent- SAK

Step 11. After completion of Step 10 activities, go to the path 'C:\Agents' in File Explorer and run run.cmd file. A command line terminal will open as shown below:

 Connecting to server

In the above screenshot, we can see the agent is listenimg for jobs. We can verify the same by going to the Default agent pool's agent view as shown below.

Default Agents

Here we can see agent is online.

Step 12. Now let's use this agent in our Azure pipeline.

Prerequisites

The Power Platform Build tools extension should be installed in your organization, Power Platform Service connection should be present in the current project.

Below is a sample YAML code that is used to test the connection for the Power Apps Environment.

trigger:
- none

pool:
 name: default #agent pool
 demands:
 - agent.name -equals AgentSAK #self-hosted agent

steps:
- task: PowerPlatformToolInstaller@2
  inputs:
    DefaultVersion: true
- task: PowerPlatformWhoAmi@2
  inputs:
    authenticationType: 'PowerPlatformEnvironment'
    PowerPlatformEnvironment: 'DEV' #Service Connection

Sample Output

Initialize job

For each pipeline run command line terminal will give the output as given below.

cmd.exe

Conclusion

We discussed how we can create a self-hosted agent in our local machine. This method is both cost-effective and time-efficient. These agents can be run both in service and interactive mode. One major advantage of self-hosted agents is they can be used in pipelines that are required to run longer. Please feel free to reach out in case any doubts or clarifications are required.

Note: PAT generated should be kept secure and only necessary permissions should be given while generating PAT.


Similar Articles