Introduction
Azure DevOps offers a great platform for automating your build and deployment pipelines. To take full advantage of its capabilities, you can set up a Windows container build agent that seamlessly integrates with Azure DevOps.
In this step-by-step guide, we will walk you through the process of creating a Windows container build agent and connecting it to Azure DevOps, enabling you to automate your development workflows efficiently.
Prerequisites
Before we begin, ensure that you have the following prerequisites in place:
- Azure DevOps Account: You must have an Azure DevOps account. If you don’t have one, sign up at dev.azure.com.
- Azure DevOps Project: Create a new or use an existing Azure DevOps project.
- Docker Installed: You need to have Docker installed on your local machine. You can download and install Docker Desktop from Docker’s official website.
Step 1: Create a Dockerfile
- Open a text editor and create a
Dockerfile
for your Windows container build agent. - Save the
Dockerfile
in a directory of your choice.
# Use the official Microsoft Windows Server Core image as the base image
FROM mcr.microsoft.com/windows/servercore:ltsc2019
# Set environment variables
ENV AGENT_VERSION=2.195.1
ENV AGENT_FOLDER=C:\agent
# Create agent folder
RUN mkdir $env:AGENT_FOLDER
# Download and extract the Azure Pipelines Agent package
ADD https://vstsagentpackage.azureedge.net/agent/$env:AGENT_VERSION/vsts-agent-win-x64-$env:AGENT_VERSION.zip $env:AGENT_FOLDER/
# Extract the agent package
RUN Expand-Archive -Path "$env:AGENT_FOLDER/vsts-agent-win-x64-$env:AGENT_VERSION.zip" -DestinationPath $env:AGENT_FOLDER
# Cleanup
RUN Remove-Item -Path "$env:AGENT_FOLDER/vsts-agent-win-x64-$env:AGENT_VERSION.zip" -Force
# Set the work directory
WORKDIR $env:AGENT_FOLDER
# Configure the agent
RUN ./config.cmd
# Start the agent
CMD ./run.cmd
Step 2: Build the Docker Image
- Open a command prompt or PowerShell window.
- Navigate to the directory containing your
Dockerfile
. - Build the Docker image using the following command, replacing
<your-image-name>
with your desired image name. - This command will create a Docker image based on the
Dockerfile
and tag it with your specified image name.
docker build -t <your-image-name> .
Step 3: Push the Docker Image to a Container Registry
- Log in to a container registry of your choice, such as Docker Hub or Azure Container Registry:
docker login <registry-url>
Step 4: Set up an Azure DevOps Build Pipeline
- In your Azure DevOps project, navigate to “Project Settings” and select “Agent Pools.”
- Create a new agent pool if necessary or use an existing one.
- In the agent pool settings, select “New agent.”
- Provide a name for the agent, and under “Operating System,” select “Windows.”
- Copy the agent registration script from Azure DevOps.
- Run the copied registration script on your Windows container host.
.\config.cmd --url https://dev.azure.com/YourOrganization --auth pat --token <Your-Personal-Access-Token> --pool <Agent-Pool-Name> --agent <Agent-Name> --windows
Step 5: Verify Agent Connection
- Go back to your Azure DevOps project and navigate to “Agent Pools.”
- Select the agent pool you configured earlier.
- You should see your Windows container agent listed as online. This indicates a successful connection.
Step 6: Configure and Run Pipelines
With your Windows container build agent connected to Azure DevOps, you can now create and configure CI/CD pipelines to automate your build and release workflows.
- Create a new pipeline in your Azure DevOps project.
- Define the pipeline YAML or use the visual editor to specify build and release tasks.
- Assign the agent pool and agent specifications to your pipeline.
- Save and run your pipeline.
Conclusion
Creating a Windows container build agent and connecting it to Azure DevOps allows you to automate your CI/CD workflows efficiently in a containerized environment.
By following the steps outlined in this guide, you can streamline your development processes and leverage the power of Azure DevOps for building, testing, and deploying your applications.