This post is about how to create containerized build agents with azure devops pipeline. Few days back I wrote a blog post on creating Azure Virtual Machine as Azure DevOps pipeline build agent. In this post we will be discussing about how to use Docker container as build agent. We can use this build agents for .NET Core and .NET Framework or any other languages / platforms. For this demo I am creating a build agent for a ASP.NET Core project.
Unlike other operating systems, Azure DevOps portal doesn’t contains configuration for container build agents. But we can get the details from docs page - Run a self-hosted agent in Docker. I am using the Docker file for Linux containers. Here is the Dockerfile from the page.
There is a start.sh file. Here is the code for the shell script. We can find about the script file code here
This docker file and script file for a build agent in Docker. We can build the docker image using the command - docker build -t anuraj/dotnetcorebuildagent:v1 ., since my docker hub username is anuraj, I am tagging the image with the name, if your name is different use it.
As I am planning to use this build agent for building .NET Core project, I need to install the .NET Core SDK as well. To install the .NET Core SDK we need to add the following code in the Dockerfile.
Now build the image again. And now we can run the docker container with few environment variables. Here are list of environment variables.
The URL of the Azure DevOps or Azure DevOps Server instance. Ex: https://dev.azure.com/dotnetthoughts
Personal Access Token (PAT) with Agent Pools (read, manage) scope, created by a user who has permission to configure agents, at AZP_URL
Agent name (default value: the container hostname).
Agent pool name (default value: Default).
Work directory (default value: _work).
Here is the way I am running my docker container with the environment variables like this.
Once it is executed successfully we will be able to see something like this.
And we can see the build agent listed under the Azure DevOps Agent Pool. Since we didn’t mentioned any specific pool, it will be under default pool.
Finally we need to configure the Agent pool inside the build definition. Like this.
Once it is done, we can save and queue the build which will compile the ASP.NET Core project and run unit tests. Since the project doesn’t have any unit tests that step is executed with a warning.
This way we will be able to configure the build agent in docker. Instead of running the build agent always we can execute this container and create build agents.