How to install, configure and add self-hosted agent in Azure DevOps?
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we are going to discuss how to install configure and add a self-hosted agent in Azure DevOps.
What is Azure Pipeline?
Azure Pipelines automatically builds and tests code projects to make them available to others. It works with just about any language or project type. Azure Pipelines combines continuous integration (CI) and continuous delivery (CD) to test and build your code and ship it to any target.
What is Azure Pipelines agents?
To build your code or deploy your software using Azure Pipelines, you need at least one agent. As you add more code and people, you’ll eventually need more. When your pipeline runs, the system begins one or more jobs. An agent is computing infrastructure with installed agent software that runs one job at a time.
- Microsoft-hosted agents: If your pipelines are in Azure Pipelines, then you’ve got a convenient option to run your jobs using a Microsoft-hosted agent. With Microsoft-hosted agents, maintenance and upgrades are taken care of for you. Each time you run a pipeline, you get a fresh virtual machine for each job in the pipeline. The virtual machine is discarded after one job. Microsoft-hosted agents can run jobs directly on the VM or in a container.
- Self-hosted agents: An agent that you set up and manage on your own to run jobs is a self-hosted agent. You can use self-hosted agents in Azure Pipelines or Team Foundation Server (TFS). Self-hosted agents give you more control to install dependent software needed for your builds and deployments. Also, machine-level caches and configuration persist from run to run, which can boost speed.
What is Azure Pipelines agent pools?
Instead of managing each agent individually, you organize agents into agent pools. In Azure Pipelines, pools are scoped to the entire organization; so you can share the agent machines across projects. In Azure DevOps Server, agent pools are scoped to the entire server; so you can share the agent machines across projects and collections. When you configure an agent, it is registered with a single pool, and when you create a pipeline, you specify which pool the pipeline uses. When you run the pipeline, it runs on an agent from that pool that meets the demands of the pipeline.
Next, we are going to create a new agent pool and then install, configure and add a new self-hosted agent into our agent pool.
How to install, configure and add self-hosted agent in Azure DevOps?
Prerequisites:
- One active Azure DevOps account
- Personal Access Token (PAT)
- One Linux instance where you will host the self-hosted agent. I am using an AWS EC2 instance with Amazon Linux 2 OS for this demo.
Step 1: Login to Azure DevOps portal and click on “Organization settings”
https://dev.azure.com/<Your_Organization_Name>/_settings/organizationOverview
Step 2: Navigate to Pipelines > Agent pools and click “Add pool”.
Step 3: From the “Pool type” drop-down select “Self-hosted”, provide a name and description and click “Create”.
Step 4: Click on the newly create pool and then click on “New agent”.
Step 5: Select the OS and architecture and copy the download link.
Note: For this demo I am using an AWS EC2 instance with Amazon Linux 2 OS to host the Azure self-hosted agent.
Step 6: Install self-hosted agent on your system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
## ------------------------------- ## Install Azure self-hosted agent ## ------------------------------- ## Create a new directory and get inside the directory mkdir myagent && cd myagent ## Get the latest Azure self-hosted agent package ## Use the download link copied in previous step ## x64 curl -o vsts-agent-linux-x64-2.194.0.tar.gz \ -L https://vstsagentpackage.azureedge.net/agent/2.194.0/vsts-agent-linux-x64-2.194.0.tar.gz ## ARM ## curl -o vsts-agent-linux-arm-2.194.0.tar.gz \ ## -L https://vstsagentpackage.azureedge.net/agent/2.194.0/vsts-agent-linux-arm-2.194.0.tar.gz ## ARM 64 ## curl -o vsts-agent-linux-arm64-2.194.0.tar.gz \ ## -L https://vstsagentpackage.azureedge.net/agent/2.194.0/vsts-agent-linux-arm64-2.194.0.tar.gz ## RHEL 6 ## curl -o vsts-agent-rhel.6-x64-2.194.0.tar.gz \ ## -L https://vstsagentpackage.azureedge.net/agent/2.194.0/vsts-agent-rhel.6-x64-2.194.0.tar.gz ## Extract the Azure self-hosted agent package tar zxvf ./vsts-agent-linux-x64-2.194.0.tar.gz |
Step 7: Configure and add self-hosted agent for Azure DevOps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
## --------------------------------- ## Configure Azure self-hosted agent ## --------------------------------- ## Configure the self-hosted agent ./config.sh ## Enter (Y/N) Accept the Team Explorer Everywhere license agreement now? (press enter for N) > Y ## Enter server URL > https://dev.azure.com/ ## Enter authentication type (press enter for PAT) > ## Enter personal access token > **************************************************** ## Enter agent pool (press enter for default) > mypool ## Enter agent name (press enter for ip-172-31-41-155) > myagent ## Enter work folder (press enter for _work) > ## 2021-11-17 05:20:25Z: Settings Saved. ## Run the self-hosted agent ## Run agent as service (recommended - Ubuntu 16 LTS or newer or Red Hat 7.1 or newer OS) ## Install the self-hosted agent as service sudo ./svc.sh install ec2-user ## replace ec2-user with your user name ## Start the self-hosted agent sudo ./svc.sh start ## Get the status of self-hosted agent sudo ./svc.sh status ## Run agent interactively ## ./run.sh ## Run agent once ## ./run.sh --once |
Now if you refresh the Azure DevOps agent page, this new self-hosted agent will be listed there.
Next, we are going to test this newly deployed self-hosted agent by running a Azure Pipeline on it.
Step 8: Create a new Azure repo in any of the project under your Azure DevOps organization and add below file in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
## ---------------------------- ## Test Azure self-hosted agent ## ---------------------------- ## Create a new Azure repo and below file in it. cat < pool: mypool trigger: branches: include: - '*' - main stages: - stage: Build jobs: - job: BuildJob steps: - script: echo Building! - stage: Test jobs: - job: TestOnWindows steps: - script: echo Testing on Windows! - job: TestOnLinux steps: - script: echo Testing on Linux! - stage: Deploy jobs: - job: Deploy steps: - script: echo Deploying the code! EOF |
Observe: We are using our self-hosted agent using “pool: mypool” parameter in the above yaml file.
Step 9: Add a new Azure pipeline inside the project where you have created the new Azure repo in the previous step.
Navigate to “Pipelines” and click “Create Pipeline”.
Select “Azure Repos Git”.
Select the repository created in step 8.
Select “Existing Azure Pipelines YAML file”.
From the drop-down select your Azure repo branch and the location of the file azure-pipelines.yml and click “Continue”.
Click on “Save” to save the pipeline configuration and then click on “Run” to run the Azure pipeline manually.
Our new Azure Pipeline gets executed successfully in our self-hosted runner.
You can also view details of the execution by clicking on any stages or jobs.
Hope you have enjoyed this article. To know more about Azure DevOps, please refer below official documentation
https://docs.microsoft.com/en-us/azure/devops/?view=azure-devops