DEV Community

Abhinav Dadhich
Abhinav Dadhich

Posted on

How to Configure & Setup Azure DevOps Self Hosted Linux Runner as a Systemd service

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.

Azure Pipelines provides several different types of agents.

  1. Microsoft-hosted agents (Agents hosted and managed by Microsoft).
  2. Self-hosted agents (Agents that you configure and manage, hosted on your VMs).
  3. Azure Virtual Machine Scale Set agents (A form of self-hosted agents, using Azure Virtual Machine Scale Sets, that can be auto-scaled to meet demands).

In this document, we will cover how to set up a self-hosted runner on an Ubuntu machine, configuring it to run as a systemd service.

Prerequisites:

  1. Microsoft Account: Ensure you have a Microsoft account set up.
  2. Azure Account and Subscription: Set up your Azure account and ensure you have an active subscription.
  3. Azure Virtual Machine: Create a VM running Ubuntu 22.04 in the Azure Cloud.
  4. Personal Access Token: Generate a Personal Access Token (PAT) in Azure DevOps.

Steps to Configure Self Hosted in Ubuntu Machine

In your web browser, navigate to Agent pools:

Image description

  • Choose Agent pools.

Image description

  • ADD a new Agent pool (Click on ADD Pool and then select Self Hosted)

Image description

  • Setup a New Agent

Image description

  • Click on Linux (As we are using Linux Machine), and then copy the URL to download the agent in your Ubuntu Machine.

Image description

Login to your Azure VM and then execute these following commands to download and setup your agent

  1. mkdir myagent && cd myagent
  2. wget https://vstsagentpackage.azureedge.net/agent/3.242.1/vsts-agent-linux-x64-3.242.1.tar.gz
  3. tar zxvf vsts-agent-linux-x64-3.242.1.tar.gz
  4. ./config.sh

Image description

Note : Configure your Agent by Answering these following questions
"Enter server URL" > https://dev.azure.com/{yourorganization}/
"Enter authentication type (press enter for PAT)" >
"Enter personal access token" >
"Enter agent pool (press enter for default)" >
"Enter agent name (press enter for AZDevops-test-runner)" >

Configure the Agent to run as a Service

  1. sudo ./svc.sh install &
  2. ./runsvc.sh &
  • Now Follow these steps to run this service as a systemd service
  1. Create a file called /etc/systemd/system/MyTestAgent.service
[Unit]
Description=Runner_AD
After=network.target
StartLimitIntervalSec=0
[Service]
#User=root
User=azureuser
#Restart=on-failure
ExecStart=/home/azureuser/myagent/runsvc.sh
StandardOutput=syslog
StandardError=syslog
#Change this to find app logs in /var/log/syslog
SyslogIdentifier=adrunner
[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode
  1. Your runsvc.sh should look like this (Make the changes accordingly)
#!/bin/bash

# convert SIGTERM signal to SIGINT
# for more info on how to propagate SIGTERM to a child process see: http://veithen.github.io/2014/11/16/sigterm-propagation.html
trap 'kill -INT $PID' TERM INT

if [ -f ".path" ]; then
    # configure
    export PATH=`cat .path`
    echo ".path=${PATH}"
fi

# insert anything to setup env when running as a service

# fallback on Node16 if Node20 is not supported by the host
./externals/node20_1/bin/node --version
if [ $? == 0 ]; then
    NODE_VER="node20_1"
else
    NODE_VER="node16"
fi

# run the host process which keep the listener alive
./externals/"$NODE_VER"/bin/node ./bin/AgentService.js &
PID=$!
wait $PID
trap - TERM INT
wait $PID
Enter fullscreen mode Exit fullscreen mode
  1. sudo systemctl daemon-reload
  2. sudo systemctl enable MyTestAgent.service
  3. sudo systemctl start MyTestAgent.service
  4. sudo systemctl status MyTestAgent.service

Image description

Image description

Voila! We have successfully Configured & Setup Azure DevOps Self Hosted Linux Runner as a Systemd service.

Top comments (0)