Thursday, March 3, 2022

How to Setup Self-Hosted Linux Docker Build Agent in Azure DevOps | How to configure Self-Hosted Linux Docker Agents in Azure Pipelines | Create Custom Build Agents in Azure DevOps

 Setting up a self-hosted Linux Docker build agent in Azure DevOps involves several steps. You’ll be configuring a Linux machine to run Docker containers that act as build agents for Azure Pipelines. Here’s a comprehensive guide to help you through the process:

1. Prepare Your Linux Machine

  1. Install Docker:

    • Update the package index:

      sudo apt-get update
    • Install Docker:

      sudo apt-get install -y docker.io
    • Start and enable Docker service:

      sudo systemctl start docker sudo systemctl enable docker
    • Verify Docker installation:

      docker --version
  2. Install Docker Compose (Optional):

    • Download Docker Compose:

      sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep tag_name | cut -d '"' -f 4)/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    • Apply for executable permissions:

      sudo chmod +x /usr/local/bin/docker-compose
    • Verify Docker Compose installation:

      docker-compose --version

2. Set Up Azure DevOps Self-Hosted Agent

  1. Create a Personal Access Token (PAT):

    • Go to your Azure DevOps organization in your browser.
    • Navigate to User Settings > Personal Access Tokens.
    • Click on New Token and create a token with the appropriate scopes, typically including "Agent Pools (read, manage)".
  2. Download and Configure the Agent:

    • Go to Project Settings in your Azure DevOps project.
    • Navigate to Agent Pools and create a new pool if needed.
    • Click on the pool, then click New Agent.
    • Select Linux as the agent type.
    • Download the agent package:

      mkdir myagent && cd myagent curl -O https://vstsagentpackage.azureedge.net/agent/2.206.0/vsts-agent-linux-x64-2.206.0.tar.gz

    • Extract the agent package:
      tar zxvf vsts-agent-linux-x64-2.206.0.tar.gz

    • Configure the agent:
      ./config.sh
      • Provide your Azure DevOps URL and PAT when prompted.
      • Choose the agent pool you created.
      • Set the agent name.
      • Confirm the agent configuration.
  3. Run the Agent:

    • Start the agent:
      ./run.sh
    • Optionally, configure the agent as a service to start automatically:
      sudo ./svc.sh install sudo ./svc.sh start

3. Set Up Docker-Based Builds

  1. Create a Dockerfile for the Build Agent:

    • Example Dockerfile:

      FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build # Install necessary tools (example: git, curl, etc.) RUN apt-get update && \ apt-get install -y git curl # Create a non-root user RUN useradd -m builduser USER builduser # Set up the working directory WORKDIR /build # Set up any necessary environment variables ENV PATH="/build:${PATH}"
  2. Build and Push the Docker Image:

    • Build the Docker image:

      docker build -t my-build-agent:latest .
    • Push the image to a Docker registry (e.g., Docker Hub or Azure Container Registry):

      docker tag my-build-agent:latest <your-registry>/my-build-agent:latest docker push <your-registry>/my-build-agent:latest
  3. Configure the Build Pipeline:

    • In Azure DevOps, create or edit a pipeline.
    • Use the Docker image in the pipeline configuration:

      pool: vmImage: 'ubuntu-latest' containers: - container: mybuild image: <your-registry>/my-build-agent:latest steps: - script: echo "Running in container" displayName: 'Run a one-line script'

4. Test Your Setup

  • Create a sample pipeline to test if your self-hosted agent is correctly picking up and running jobs.
  • Verify that builds are successfully executed and that your Docker-based agent is functioning as expected.

By following these steps, you'll have a self-hosted Linux Docker build agent running in Azure DevOps, which can help you manage and scale your build infrastructure effectively.

No comments:

Post a Comment