CodeBlog.xyz

SetUp Azure DevOpsSetUp pipeline with selfhosted agent

September 16, 2023 | by Meir Achildiev

SetUp Azure DevOpsSetUp pipeline with selfhosted agent

Running a pipeline using Azure DevOps involves several steps, including creating a YAML file to define your pipeline and setting up a self-hosted agent for execution. Let’s dive deep into each of these aspects with detailed explanations and examples.

Creating a YAML Pipeline:

Azure DevOps provides a powerful feature called YAML Pipelines, which allows you to define your build and release pipelines as code. Here’s a step-by-step guide to creating a YAML pipeline:

  1. Sign in to Azure DevOps:
    Make sure you’re logged into your Azure DevOps account.
  2. Navigate to Pipelines:
    Go to the “Pipelines” section in your Azure DevOps project.
  3. Create New Pipeline:
    Click on the “New Pipeline” button. This will initiate the pipeline creation process.
  4. Select Repository:
    Choose the repository where your application code is stored.
  5. Configure Pipeline:
    Select “YAML” as the pipeline configuration type.
  6. Create YAML File:
    You’ll need to create a YAML file named azure-pipeline.yml (or any name you prefer) in the root of your repository. This file will define your pipeline’s stages, jobs, and tasks.
  7. Define Pipeline:
    In your YAML file, define your pipeline using YAML syntax. Here’s an example:
trigger:
- main  # Trigger pipeline on changes to the main branch

pool:
  vmImage: 'ubuntu-latest'  # Specify the VM image for the pipeline agent

jobs:
- job: Build
  steps:
  - script: echo 'Building the application...'
    displayName: 'Build Application'

- job: Test
  steps:
  - script: echo 'Running tests...'
    displayName: 'Run Tests'
  1. Commit and Push:
    Once you’ve defined your YAML file, commit and push it to your repository.
  2. Trigger Pipeline:
    After the commit, Azure DevOps will automatically detect the new YAML pipeline definition and trigger the pipeline according to the specified conditions.

Creating a Self-Hosted Agent:

A self-hosted agent is a machine within your environment that runs pipeline jobs. This allows you to execute pipelines on your own infrastructure. Here’s how to set up a self-hosted agent:

  1. Install Dependencies:
    On the machine you intend to use as a self-hosted agent, ensure you have the necessary software installed, such as Git, Node.js (for Angular), .NET Core (for C# .NET), and SQL Server.
  2. Download Agent Package:
    In Azure DevOps, go to your project settings, navigate to “Agent Pools,” and click “New agent.” Follow the instructions to download the agent package for your operating system.
  3. Configure Agent:
    Extract the downloaded package and run the configuration script. During configuration, provide your Azure DevOps account details and choose an agent pool.
  4. Run Agent:
    Start the agent service using the provided script. This will register the agent with your Azure DevOps project.
  5. Assign Agent to Jobs:
    In your YAML pipeline, you can specify that certain jobs should run on your self-hosted agent using the pool keyword.
jobs:
- job: Build
  pool:
    name: MySelfHostedAgent  # Name of your self-hosted agent pool
  steps:
  - script: echo 'Building the application...'
    displayName: 'Build Application'

By following these steps, you’ll be able to create a YAML pipeline and set up a self-hosted agent for executing your pipeline jobs. This approach empowers you to manage your entire CI/CD process as code and leverage your own infrastructure for execution.

Remember, the examples provided are simplified for illustration purposes. Depending on your project’s complexity, you may need to include more detailed steps, environment configurations, and error handling in your YAML pipeline definition.

RELATED POSTS

View all

view all