Are you looking for a way to run your Azure DevOps builds and deployments on your own infrastructure? If so, you’ll want to check out Azure DevOps self-hosted runners. Self-hosted runners are agents that allow you to run your build and deployment jobs on machines that you control, giving you more flexibility and control over your environment. In this blog post, we’ll take a closer look at what Azure DevOps self-hosted runners are, why you might want to use them, and how to set them up for your projects. Whether you’re looking to save costs, ensure security and isolation, or build on specialized hardware or software configurations, self-hosted runners can help you improve your Azure DevOps workflow.
This post is a continuation of our journey with self-hosted CI/CD agents. I encourage you to check part 1, part 2 and part 3 if you want to see a different approach to that topic.
Overview
We will create and configure the following resources:
AKS cluster with workload identity and kubelet identity.
Azure Container Registry.
Role assignment for kubelet identity and workload identity.
Kubernetes deployment object for self-hosted runner.
# After running above script, if there were no errors, variables should be available in terminal.chmod +x aks.sh
./aks.sh 'add user id, for me, it is my email of AAD user'
Get credentials to AKS, oidcUrl and test connection.
1
2
3
4
5
az aks get-credentials --resource-group$resourceGroup--name$aksNameexport oidcUrl="$(az aks show --name$aksName\--resource-group$resourceGroup\--query"oidcIssuerProfile.issuerUrl"-o tsv)"
kubectl get nodes
az identity federated-credential create \--name"aks-federated-credential"\--identity-name$workloadIdentity\--resource-group$resourceGroup\--issuer"${oidcUrl}"\--subject"system:serviceaccount:devops:workload-sa"
Create custom role for workload identity. Create acrbuild.json file with following definition. Replace {YOUR SUBSCRIPTION} with your subscription id.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{"Name":"AcrBuild","IsCustom":true,"Description":"Can read, push, pull and list builds.","Actions":["Microsoft.ContainerRegistry/registries/read","Microsoft.ContainerRegistry/registries/pull/read","Microsoft.ContainerRegistry/registries/push/write","Microsoft.ContainerRegistry/registries/scheduleRun/action","Microsoft.ContainerRegistry/registries/runs/*","Microsoft.ContainerRegistry/registries/listBuildSourceUploadUrl/action"],"AssignableScopes":["/subscriptions/{YOUR SUBSCRIPTION}"]}
1
az role definition create --role-definition acrbuild.json
Assign AcrBuild role to workload identity.
1
2
az role assignment create --assignee$workloadClientId\--role'AcrBuild'--scope$acrId
Assign Azure Kubernetes Service Cluster User Role and Azure Kubernetes Service RBAC Writer to workload identity.
1
2
3
4
az role assignment create \--role"Azure Kubernetes Service Cluster User Role"\--assignee$workloadPrincipalId\--scope$aksId
1
2
3
4
az role assignment create \--role"Azure Kubernetes Service RBAC Writer"\--assignee$workloadPrincipalId\--scope"$aksId/namespaces/devops"
Install Azure Devops self-hosted agent
Personal access token (PAT) and Agent Pool
As a first step, we must register the runner. We need to have permissions to administer the agent queue if we want to complete that step.
Sign in to Azure DevOps organization: https://dev.azure.com/{your_organization}.
Create personal access token.
Create an Agent pool
Create an image for Azure DevOps runner
We will build custom image useing new version of the runner, pre-release v3.217.1 . We will also add az cli, kubectl.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM ubuntu:22.04USER rootRUN apt-get -y update && apt-get install-y curl &&\
curl -sL https://aka.ms/InstallAzureCLIDeb | bash && az aks install-cli &&\
curl -fsSL https://get.docker.com -o get-docker.sh && sh ./get-docker.sh &&\
mkdir devops-runner &&cd devops-runner &&\
curl -o vsts-agent-linux-x64-3.217.1.tar.gz -L https://vstsagentpackage.azureedge.net/agent/3.217.1/vsts-agent-linux-x64-3.217.1.tar.gz &&\
tar xzf ./vsts-agent-linux-x64-3.217.1.tar.gz &&\
apt-get clean
RUN addgroup --gid 110 devops && adduser devops --uid 111 --system&& adduser devops devops &&\
chown-R devops:devops devops-runner
USER devops
1
az acr build -f Dockerfile.runner -t devops-runner:v1.0.0 -r$acrName-g$resourceGroup.