Last active
October 5, 2023 10:46
-
-
Save focusthenflex/e55cebf71bbadd2a96aaf0b4e1465094 to your computer and use it in GitHub Desktop.
Adding Self-Hosted Build Agents to Azure DevOps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Asks user to provide information needed to create agent(s) | |
prompt_for_missing_variables() { | |
# Prompt for project URL (i.e. https://dev.azure.com/TeamNameHere/) | |
url_regex="((http|https):\/\/)?dev\.azure\.com\/[a-zA-Z0-9-]+(\/)?" | |
if [ -z "$url" ] ; | |
then | |
echo -e "\nPlease type in server url: " | |
read -r url | |
fi | |
if ! [[ "$url" =~ $url_regex ]] ; then | |
echo -e "URL Format Incorrect. Exiting...\n" >&2 | |
exit 1 | |
fi | |
# Prompt number of agents to create | |
if [ -z "$agents" ] ; | |
then | |
echo -e "\nPlease type in number of agents to create: " | |
read -r agents | |
fi | |
number_regex="[0-9]+" | |
if ! [[ "$agents" =~ $number_regex ]] ; | |
then | |
echo -e "\nAgents Format Incorrect. Exiting...\n" >&2 | |
exit 1 | |
fi | |
# Prompt for Personal Access Token | |
if [[ -z "$token" ]]; | |
then | |
echo -e "\nPlease type in Personal Access Token: " | |
read -r token | |
fi | |
# Prompt for pool name where you'd like agents to reside | |
if [[ -z "$pool" ]]; | |
then | |
echo -e "\nPlease type in Build Pool Name: " | |
read -r pool | |
fi | |
# Set variables | |
export url=$url | |
export agents=$agents | |
export pool=$pool | |
export token=$token | |
# Print variables to user | |
echo -e "\nURL set as ${url}.\n" | |
echo -e "\nNumber of agents set as ${agents}.\n" | |
echo -e "\nPool set as ${pool}.\n" | |
echo -e "\nToken set as ${token}.\n" | |
} | |
# Set directories for agents | |
add_agent_directories() { | |
echo -e "\nAdding 'Agent' Directory\n" | |
# Make directory 'builds' on root, and make current user owner of the new directory | |
sudo mkdir /builds/ | |
sudo chown $USER:$USER /builds | |
cd /builds/ | |
work_dir=$PWD | |
mkdir ${work_dir}/agents/ | |
export work_dir=$work_dir | |
export agents_dir=${work_dir}/agents/ | |
echo "\n'Agents' Directory Added!\n\n" | |
} | |
# Install agents | |
install_agents() { | |
echo -e "\nInstalling Agent(s)\n" | |
# Download Agent Set-Up File | |
wget https://vstsagentpackage.azureedge.net/agent/2.160.1/vsts-agent-linux-x64-2.160.1.tar.gz | |
# Create $agents number of agents | |
for (( i=1; i <= ${agents}; i++ )) | |
do | |
mkdir ${agents_dir}/agent${i} | |
cd ${agents_dir}/agent${i} | |
tar zxvf ${work_dir}/vsts-agent-linux-x64-2.160.1.tar.gz | |
./config.sh --acceptTeeEula --url ${url} --auth PAT --token ${token} --pool ${pool} --agent "Agent${i}" --work "_work" | |
# Install background agent service | |
sudo ./svc.sh install | |
# Start background agent service | |
sudo ./svc.sh start | |
done | |
echo -e "\nAgents Installed!" | |
} | |
echo -e "\nStarting script. \n\nBe sure you have Server URL (\$url), Number of Agents (\$agents), Personal Access Token (\$token), and Build Pool (\$pool) variables ready to be set \n\n" | |
read -p "\nPress the Enter/Return key to begin. " | |
prompt_for_missing_variables | |
read -p "\nPress the Enter/Return key if variables are as intended. Exit now to make changes." | |
add_agent_directories | |
install_agents | |
echo "All Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IS there an updated version of this that works now?