DEV Community

Cover image for Azure pipeline
Mo
Mo

Posted on

Azure pipeline

Azure pipeline is a cloud-based service that allows you to automate the build, test and deployment of your applications. In this blog post, I will show you how to write a pipeline to compile and run an ASP.NET Core project, and how to choose between Microsoft hosted agent and self-hosted agent for your pipeline.

Introduction to Azure Pipelines:

A pipeline is a sequence of steps that define how to build, test and deploy your application. You can write a pipeline using YAML or a graphical user interface. YAML is a human-readable data format that allows you to specify the tasks and settings of your pipeline in a text file. The graphical user interface is a web-based tool that lets you drag and drop tasks and configure them using forms.

To write a pipeline for an ASP.NET Core project, you need to have the following components:

  • A source code repository that contains your project files. You can use any Git-based repository, such as GitHub, Azure Repos or Bitbucket.
  • A pipeline definition file that specifies the steps of your pipeline. You can create this file using YAML or the graphical user interface.
  • An agent that runs the tasks of your pipeline. An agent is a machine that has the software and tools required to run your pipeline tasks. You can use either a Microsoft hosted agent or a self-hosted agent for your pipeline.

Microsoft Hosted Agent

A Microsoft hosted agent is a pre-configured virtual machine that is managed by Microsoft and runs in Azure. You can choose from different types of hosted agents, such as Windows, Linux or macOS, depending on your application platform. A Microsoft hosted agent is convenient because you don't have to maintain or update it, but it has some limitations, such as:

  • You can only run one job at a time on a hosted agent.
  • You have a maximum of 180 minutes per job on a hosted agent.
  • You have limited disk space and memory on a hosted agent.
  • You cannot install custom software or tools on a hosted agent.

Self-Hosted Agent

A self-hosted agent is a machine that you own and manage, and that you register with Azure DevOps. You can use any machine that meets the minimum requirements, such as Windows, Linux or macOS. A self-hosted agent gives you more control and flexibility over your pipeline, but it also requires more maintenance and security. Some benefits of using a self-hosted agent are:

  • You can run multiple jobs concurrently on a self-hosted agent.
  • You have no time limit per job on a self-hosted agent.
  • You have more disk space and memory on a self-hosted agent.
  • You can install custom software or tools on a self-hosted agent.

Microsoft Hosted Agent vs. Self-Hosted Agent

To decide whether to use a Microsoft hosted agent or a self-hosted agent for your pipeline, you need to consider the following factors:

  • The complexity and requirements of your application.
  • The frequency and duration of your pipeline runs.
  • The cost and availability of resources.

Generally speaking, if your application is simple and does not require any special software or tools, and if you run your pipeline occasionally and for short periods of time, you can use a Microsoft hosted agent. If your application is complex and requires custom software or tools, and if you run your pipeline frequently and for long periods of time, you may want to use a self-hosted agent.

Here is an example of a YAML pipeline definition file that compiles and runs an ASP.NET Core project using a Microsoft hosted agent:

This pipeline defines the following steps:

trigger:
- main

pool:
  vmImage: 'windows-latest'

steps:
- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'select'
    vstsFeed: 'MyFeed'

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/*.csproj'
    arguments: '--configuration Release'

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '**/*Tests.csproj'
    arguments: '--configuration Release'

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: true
    arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)'
    zipAfterPublish: true

- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)'
    artifactName: 'drop'
Enter fullscreen mode Exit fullscreen mode
  • Trigger the pipeline when there is a change in the main branch of the repository.
  • Use a Windows-based hosted agent from the latest image.
  • Restore the NuGet packages for the project files.
  • Build the project files in release mode.
  • Run the unit tests for the test project files.
  • Publish the web project files to an output folder and zip them.
  • Publish the zip file as an artifact.

You can also create this pipeline using the graphical user interface by following these steps:

  • Sign in to your Azure DevOps organization and navigate to your project.
  • In the project page, select Pipelines and then select Create Pipeline.
  • In the Where is your code? page, select the source code repository that contains your project files.
  • In the Configure your pipeline page, select ASP.NET Core as the template.
  • In the Review your pipeline YAML page, review and edit the YAML code as needed.
  • Select Save and run to save and run your pipeline.

You can monitor the progress and status of your pipeline runs in the Pipelines page. You can also view the logs and artifacts of each run by selecting the run number.

Conclusion:

In conclusion, Azure Pipelines simplifies the CI/CD process, and writing pipelines in YAML is a powerful way to define your build and deployment workflows. Choosing between Microsoft-hosted and self-hosted agents depends on your project's specific needs and requirements. Microsoft-hosted agents are convenient for many scenarios, while self-hosted agents provide flexibility and control over the build environment.

I hope this blog post has helped you understand how to write a pipeline for an ASP.NET Core project and how to choose between a Microsoft hosted agent and a self-hosted agent for your pipeline. If you have any questions or feedback, please leave a comment below.

Thank you for reading!

Top comments (0)