DevOps is a complicated discipline, so when we as developers have to set up a build pipeline in Azure Devops we are sometimes in deep water.
2023-05-25: Updated Example to use Windows-2022 pool, not windows-2019 pool.
Thats why I made this yml template that I can copy from whenever I need to build a .net 6 project:
trigger:
- main
pool:
name: Azure Pipelines
vmImage: 'windows-2022'
variables:
buildConfiguration: 'Release'
steps:
- task: UseDotNet@2
displayName: 'Use .NET Core 6'
inputs:
version: 6.0.x
- task: DotNetCoreCLI@2
displayName: Restore
inputs:
command: restore
projects: '**/*.csproj'
- task: DotNetCoreCLI@2
displayName: Build
inputs:
projects: '**/*.csproj'
- task: DotNetCoreCLI@2
displayName: Publish
inputs:
command: publish
publishWebProjects: false
projects: '**/*.csproj'
arguments: '--configuration release --output $(build.artifactstagingdirectory) /property:PublishWithAspNetCoreTargetManifest=false'
zipAfterPublish: false
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
inputs:
ArtifactName: myproject
I name the file “azure-pipelines.yml” and place it in the root of my project:
When the pipeline has run it creates an artifact that can be deployed:
SO WHAT DOES EACH SECTION DO?
trigger | Determines which branch will trigger the pipeline. Is used for continuous deployment |
pool | Determines the server, and what’s installed on the server that it uses as build platform. For example, windows-2022 is a server with Visual Studio 2022 + a lot of libraries. Use windows-2022, not ubuntu if you wish to build .net core 6 code |
variables | Sets up build variables |
task: UseDotNet@2 | This step can be left out, if you choose the windows-2022 pool. If you use the windows-2019 pool. you need this step. Tells the build to use .net core 6 when building. If you leave out this step, you risk that you build using .net core 3.1 instead. The “windows-2022” pool already runs .net 6, so you don’t need to specify it seperately. |
DotNetCoreCLI@2/Restore | Gets NuGet packages |
DotNetCoreCLI@2/Build | Builds the project |
DotNetCoreCLI@2/Publish | Copies the files to a folder. publishWebProjects will determine what to copy. If true, it will copy web projects zipAfterPublish determines if the out should be zipped. property:PublishWithAspNetCoreTargetManifest=false will copy your NuGet dll’s along with the dlls of the project |
PublishBuildArtifacts@1 | Creates an artifact that can be used by your release pipeline |
SO WHAT’S THE IMPORTANT KNOWLEGDE HERE?
The “pool” is the real trick here. A pool determines which tools will build your project. The pool called “windows-2019” is a server that includes Visual Studio 2019 + a lot of tools like NuGet, Yarn, Newman, GitHub Cli, Rust, etc.
The pool called “windows-2022” is similar to windows-2019, but it contains Visual Studio 2022 instead.
So if you select windows-2022 as pool, you basically build your code with the Visual Studio 2022 installation you would have on your own desktop computer.
WRAP-UP:
This is just an introduction to DevOps. I hope that the file will give you a hint of what to do before panicking if you are given the task of setting up your own build and release pipelines.
Happy DevOps’ing.
MORE TO READ:
- Force ‘dotnet publish’ to publish dependencies using PublishWithAspNetCoreTargetManifest by Benjamin Day
- Build, test, and deploy .NET Core apps from Microsoft
- .NET Core CLI task by Microsoft
- Publish Build Artifacts task by Microsoft