visit
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
variables:
ProjectName: 'MyHealth.Common'
ProjectPath: '**/MyHealth.Common.csproj'
buildConfiguration: 'Release'
steps:
- task: UseDotNet@2
displayName: 'Install .NET Core SDK'
inputs:
packageType: 'sdk'
version: '3.x'
- task: DotNetCoreCLI@2
displayName: 'dotnet restore'
inputs:
command: 'restore'
projects: '$(ProjectPath)'
- task: DotNetCoreCLI@2
displayName: 'Build $(ProjectName)'
inputs:
command: 'build'
arguments: '--configuration $(buildConfiguration)'
projects: '$(ProjectPath)'
To create a NuGet package in our build file, we need to add a DotNetCoreCLI task like so:
- task: DotNetCoreCLI@2
displayName: 'Pack $(ProjectName)'
inputs:
command: 'pack'
arguments: '--configuration $(buildConfiguration)'
packagesToPack: '$(ProjectPath)'
nobuild: true
versioningScheme: 'off'
In this task, we run the pack command and tell the task to pack our project path that I’ve defined earlier in the YAML file. I’ve set the nobuild argument to true since I’ve already built my project.
My package is a .NET Standard package. For .NET Core and .NET Standard packages, Microsoft recommends that you use the DotNetCoreCLI tasks. If you’re building packages for .NET Framework, you can use a NuGet task.
There are a couple of ways we can do this. In my package .csproj file, I’ve specified my Version number like so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Description>Common Helper Library for MyHealth Applications</Description>
<Authors>Will Velida</Authors>
<Product>MyHealth</Product>
<Version>1.4.0</Version>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.1.2" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.8.1" />
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>
variables:
Major: '1'
Minor: '0'
Patch: '0'
steps:
- task: NuGetCommand@2
inputs:
command: pack
versioningScheme: byPrereleaseNumber
majorVersion: '$(Major)'
minorVersion: '$(Minor)'
patchVersion: '$(Patch)'
With the NuGet task, we can use the Major.Minor.Patch semantic versioning scheme for our builds. However, once a version has been produced, we can’t update or replace that version. They are immutable. To produce new versions of our package each time we update them, we can do the following:
Use the $(rev:.r) variable for the version number that we wish to increment. This will automatically increment the build number for that variable each time we push to our branch while keeping the other variables constant.
Use the $(date:yyyyMMdd) variable. This is ideal for creating prelease labels for the build while keeping our major, minor, and patch versions constant.
- task: NuGetAuthenticate@0
- task: NuGetCommand@2
displayName: 'Publish $(ProjectName)'
inputs:
command: push
feedsToUse: 'select'
packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
nuGetFeedType: 'internal'
publishVstsFeed: '<projectname>/<feedname>'
versioningScheme: 'off'
allowPackageConflicts: true
Here, we are using the NuGetAuthenticate command to authenticate our Build server to push packages to our internal NuGet feed. We then run a NuGetCommand task with the push command to push our package (stored in our Artifact Staging Directory) to our internal NuGet feed.
Here, we have used <projectname>/<feedname> as our VSTS feed to publish to. Remember, we can scope our feeds at either the project level or an organization level in Azure DevOps. Since my feed is scoped at the project level, I need to put the project name here.
I’ve also included the nuGetFeedType argument and stated that our target field is an internal feed. Since we are using the push command in the NuGet command task, this argument is required.
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
variables:
ProjectName: 'MyHealth.Common'
ProjectPath: '**/MyHealth.Common.csproj'
buildConfiguration: 'Release'
steps:
- task: UseDotNet@2
displayName: 'Install .NET Core SDK'
inputs:
packageType: 'sdk'
version: '3.x'
- task: DotNetCoreCLI@2
displayName: 'dotnet restore'
inputs:
command: 'restore'
projects: '$(ProjectPath)'
- task: DotNetCoreCLI@2
displayName: 'Build $(ProjectName)'
inputs:
command: 'build'
arguments: '--configuration $(buildConfiguration)'
projects: '$(ProjectPath)'
- task: DotNetCoreCLI@2
displayName: 'Pack $(ProjectName)'
inputs:
command: 'pack'
arguments: '--configuration $(buildConfiguration)'
packagesToPack: '$(ProjectPath)'
nobuild: true
versioningScheme: 'off'
- task: NuGetAuthenticate@0
- task: NuGetCommand@2
displayName: 'Publish $(ProjectName)'
inputs:
command: push
feedsToUse: 'select'
packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
nuGetFeedType: 'internal'
publishVstsFeed: 'projectname/feedname'
versioningScheme: 'off'
allowPackageConflicts: true
Previously published on .