Saturday, April 15, 2023

Quick Bytes: YAML File To Build Your Package To Your Azure DevOps Custom Feed

 
Here's a quick YAML file to build and publish a class library package to a custom feed. Just replace out the "MyProject" and "MyFeed" with the appropriate names. After the build and publish you can then consume that feed to get that package and the associated libraries. To get your feed information just click on the "Connect To Feed" button on the main Feed page (after you select it from the drop down from the Artifacts page) and then select the system you are using to get the connection information.

# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

# the build will trigger on any changes to the master branch
trigger:
master

# the build will run on a Microsoft hosted agent, using the lastest Windows VM Image
pool:
  vmImage'windows-latest'

# these variables are available throughout the build file
# just the build configuration is defined, in this case we are building Release packages
variables:
  buildConfiguration'Release'

#The build has 3 seperate tasks run under 1 step
steps:

# The first task is the dotnet command build, pointing to our csproj file
taskDotNetCoreCLI@2
  displayName'dotnet build'
  inputs:
    command'build'
    arguments'--configuration $(buildConfiguration)'
    projects'MyProject/MyProject.csproj'

# The second task is dotnet pack command again pointing to the csproj file
# The nobuild means the project will not be compiled before running pack, because its 
# already built in above step
taskDotNetCoreCLI@2
  displayName"dotnet pack"
  inputs:
    command'pack'
    arguments'--configuration $(buildConfiguration)'
    packagesToPack'MyProject/MyProject.csproj'
    nobuildtrue
    versioningScheme'off'

# The last task is a nuget command, nuget push
# This will push any .nupkg files to the 'TestFeed' artifact feed
# allowPackageConflicts allows us to build the same version and not throw an 
# error when trying to push
# instead it just ingores the latest package unless the version changes
taskNuGetCommand@2
  displayName'nuget push'
  inputs:
    command'push'
    feedsToUse'select'
    packagesToPush'$(Build.ArtifactStagingDirectory)/**/*.nupkg;
!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType'internal'
    publishVstsFeed'MyFeedName'
    versioningScheme'off'
    allowPackageConflictstrue