This article demonstrates how we can easily publish a class library project into Azure Artifacts using a simple CI/CD pipeline. If the project needs to store as a package in the private repository instead of a public repository like Nuget.org, we can go ahead and use Azure artifacts. From a security perspective, all the packages are basically local to the company/ organization itself.
In the artifacts, all the packages are stored in something known as feeds. So, to start publishing and consuming packages as your artifacts, the first thing that we must do is create a feed. Now, that feeds we can group packages together and can control the access onto the package itself.
Let’s go ahead and develop a class library project and let's create feed-in Azure artifacts into Azure DevOps.
Step 1 - Create a simple .NET Class Library project
Now, we will create a simple class library project in a visual studio. And add a simple method for addition and multiplication into the class.
Let’s check project properties and check package property. And over here we can check all the details about the package.
Step 2 – Push project to Azure Repos
Let’s push the changes into Azure DevOps repos. Click on git and remote URL. The assumption here is that we already created a new repository in Azure DevOps.
Step 3 - Create a new feed-in Azure Artifacts
Let’s create a new feed-in Artifacts. Just giving a name for the feed, and in terms of the visibility, we can go ahead and choose the visibility.
Step 4 – Create Nuget Package using Simple CI/CD Pipeline
Let’s create a simple build pipeline to pack and push the project into the target feed location that we created in earlier steps. Below is a sequence of tasks that we need to configure.
Detailed YAML for build pipeline that we created above.
# Variable 'BUILD_BUILDNUMBER' was defined in the Variables tab
# Variable 'BuildEnv' was defined in the Variables tab
trigger:
branches:
include:
- refs/heads/main
name: 1.0.$(Rev:r)
jobs:
- job: Job_1
displayName: Agent job 1
pool:
vmImage: windows-2019
steps:
- checkout: self
- task: DotNetCoreCLI@2
displayName: dotnet restore
inputs:
command: restore
projects: '**/MyAwesomeLibray/*.csproj'
feedRestore: <Project_Name>/<Feed_Name>
- task: DotNetCoreCLI@2
displayName: dotnet build
inputs:
projects: '**/MyAwesomeLibray/*.csproj'
arguments: --configuration $(BuildConfiguration)
- task: DotNetCoreCLI@2
displayName: dotnet pack
inputs:
command: pack
publishWebProjects: false
projects: '**/*MyDemoApp.sln'
arguments: --output $(Build.ArtifactStagingDirectory)/
zipAfterPublish: True
searchPatternPack: '**/MyAwesomeLibray/*.csproj'
versioningScheme: byEnvVar
versionEnvVar: BuildEnv
- task: DotNetCoreCLI@2
displayName: dotnet nuget push
inputs:
command: push
feedPublish: <Project_Name>/<Feed_Name>
- task: PublishBuildArtifacts@1
displayName: publish artifact
condition: succeededOrFailed()
inputs:
PathtoPublish: $(build.artifactstagingdirectory)
TargetPath: '\\my\share\$(Build.DefinitionName)\$(Build.BuildNumber)'
...
Make sure that the build number format in the options tab is correct. This will make sure the version of your Nuget package.
In the variables tab, create these variables.
In the triggers tab, enable continuous integration should be checked.
Once the build pipeline setup is done, click on save and queue.
Let’s check Feed-in Azure artifacts… we can see that package got created.
Step 5 - Access and Install Package into Another Application
Now, if we click on Connect to feed over here, it will show the different ways we can connect to the feed. We will choose the Visual Studio option. Here we will see the name and source URL and we will use this value in subsequent steps.
On the Tools menu in visual studio, select Options > NuGet Package Manager > Package Sources. Select the green plus icon in the upper-right corner and enter the name and source URL.
Let’s add this package into the new application where we will use the package functionalities. While clicking on manage NuGet Package of a project.
Awesome! We are done with the demonstration and see that how easily we can easily create a NuGet package using CI/CD pipeline and push it into a private feed in Azure Artifacts. And lastly, we access that package and install it into a client app. Hope you find this detailed explanation useful.
NOTE: Alterante YAML we can use to implement this using Nuget Task.
# Variable 'BuildEnv' was defined in the Variables tabname: 2.0.1
jobs:
- job: Job_1
displayName: Agent job 1
pool:
vmImage: windows-2019
steps:
- checkout: self
- task: NuGetCommand@2
displayName: NuGet restore
inputs:
solution: '**/Logging.csproj'
feedRestore: <projectName>/<feed>;
- task: DotNetCoreCLI@2
displayName: dotnet build
inputs:
projects: '**/Logging.csproj'
arguments: --configuration $(BuildConfiguration)
- task: NuGetCommand@2
displayName: NuGet pack
inputs:
command: pack
searchPatternPack: '**/Logging.csproj'
versioningScheme: byEnvVar
versionEnvVar: BuildEnv
- task: DotNetCoreCLI@2
displayName: dotnet test
inputs:
command: test
projects: '**/Logging.UnitTest.csproj'
arguments: --configuration $(BuildConfiguration)
- task: NuGetCommand@2
displayName: NuGet push
inputs:
command: push
searchPatternPush: $(Build.ArtifactStagingDirectory)/**/*.nupkg
feedPublish: <projectName>/<feed>;
- task: PublishBuildArtifacts@1
displayName: publish artifact
condition: succeededOrFailed()
inputs:
PathtoPublish: $(build.artifactstagingdirectory)
TargetPath: '\\my\share\$(Build.DefinitionName)\$(Build.BuildNumber)'
...
Happy Learning!