Thursday, August 22, 2019

Using NuGet Packages as Azure DevOps Artifacts


When we work with Azure DevOps deployment pipelines, we have several options to select the build artifacts. Most common way is selecting the build artifacts published in the build pipeline. Other than this we can publish the artifacts as NuGet packages and save in the Azure DevOps Artifacts feeds. This post will explain how to create a NuGet feed, build and push NuGet packages using an Azure DevOps build pipeline to the feed. Finally, we will discuss how to deploy artifacts packaged as the NuGet packages using Azure DevOps deployment pipelines. 


prerequisites :

1 .Net MVC application with the nuspec file. Nuspec file is a XML file with packaging meta data, used to build and package the NuGet Packages. To learn more about nuspec (nuget specification) refer https://docs.microsoft.com/en-us/nuget/reference/nuspec 
            

 2.  Azure windows based web app with .NET runtime created using the Azure portal 

Let's get started.
  • Go to Azure DevOps Artifacts section.
  • Click on New feed to create a new feed

  • Enter the name for the feed and select suitable access control option for this feed.Click on create to create a Package feed.

  • Now we have created a Feed. This is the place where we are going to save the build artifacts as NuGet package.
  • Let's create a build to create and push NuGet package to this feed
  • Go to Azure DevOps build pipeline.
  • Create new build using classic editor option. Add build steps as explain below.
  • Let's identify build steps now

1 - NuGet tool installer to download the NuGet packages use in the project
2 - Restore the NuGet packages
3 - Build the solution
4 - Add Msbuild arguments
/p:OutDir="$(Build.ArtifactStagingDirectory)" - Give the out put directory of the build artifacts
  /p:TransformConfigFiles=true - Transform config files and create one final config file


  • In the nuspec file we haven't provide file locations. So it will package all the files content in the current location.Our sample nuspec file is as follows.

  • We need to package the content of the published files. So, we need to copy our NuGet file to published file location. To do that we use copy files step in the build

1 - Source location where we have the nuspec file
2 - Give the nuspec file name
3 - Give the published website file location as target location we need to copy the nuspec file to
  • Next step is execute NuGet package command to create the NuGet package

1 - Give the NuGet command. Here we are going to create a package. So select pack command
2 - Give path to nuspec file
3 - Give the location of the files going to be packaged
4 - Use build number for package versioning. This allows to create a package with the same name as build. But, build number format should be x.x.x or x.x.x.x as that is the only support version format for NuGet. To learn more on nuget package version support, refer here. https://docs.microsoft.com/en-us/nuget/concepts/package-versioning#version-basics  
  • We have created a package now. Next step is NuGet push where we push the created NuGet package to Artifacts feed

1 - Select NuGet command to push the NuGet package to NuGet feed
2 - Give the path to NuGet package to publish
3 - Select the target feed from the drop down to push the NuGet package
  • So far, we have discussed the build steps. Trigger a new build. After the build succeeded go to Azure DevOps Artifacts section. You would be able to see the published NuGet package under relevant NuGet feed.

  • Now we have NuGet package in the NuGet feed. Let's try to understand how we can use this NuGet package in deployment pipeline.
  • Create new Azure DevOps release pipeline
  • Click on Add button on artifact section to select the artifact to the pipeline. Pane will open and you would be able to see the available artifact options.


  • Select Azure artifact as source type
    1 - Select the feed where we have the NuGet package
    2 - Select the package type. Here we select NuGet from the drop down. Other than NuGet, In Azure DevOps we can create Maven, npm, Python and Universal oackages
    3 - Select the view option as Local, prerelease or Release
    4 - Select the package from the list
    5 - Select the version to deploy
  • Now you can deploy the NuGet package to Azure web app using Azure App service deploy task.


1.   Select the Connection type. We have created the web app in Azure. So, select Azure Resource Manager as connection type. 
2.   Select the Azure subscription where the web application created (If you have a different account owned Azure Subscription you may have to create the service connection using a service principle manually.For more info https://chamindac.blogspot.com/2019/05/azure-devops-service-connection-for-and.html) 
3.     Select the app service type of the web app 
4.     Select the app service name 
5.     Select the package in the Artifacts feed 

  • Trigger a release. After deployment succeeded, Go to Azure web site and check the site. you would be able to navigate to the site

This post explained how to build and package a web app as a NuGet package using Nuget pack and use push tasks in build pipeline to publish a nuget package to Azure Artifacts. Further, we were able to learn how to deploy the created NuGet package using Azure DevOps pipeline. 

No comments:

Post a Comment