Creating and Publishing NuGet Packages
To ensure successful software deployment, you need a way to distribute and deploy as one unit. NuGet packages can help, if you know how to use them.
Software can be complicated. No longer is it as simple as packing everything into an exe. Some software runs into the gigabytes and even terabytes, and can contain a ton of files, scripts, installers and many other artifacts. And, to make sure you have a successful deployment, everything has to be in its proper path. Developers and IT professionals also require a way to distribute and deploy this software as one unit. One way to package all of those items up and to version the entire suite of artifacts as one is through the NuGet package.
What is NuGet?
NuGet is a package manager which allows users to package up just about anything, give it a version, and distribute it as a single unit. NuGet is very popular amongst .NET developers, but it can also be used for DevOps engineers and even system administrators. NuGet is a way of wrapping up related artifacts into one, single unit.
Once a NuGet package is made, it is then published to a feed. A feed is a website which is configured to understand NuGet. This feed allows the user to publish packages and provide a source of distribution for these packages.
Creating an NUSPEC file
Once a NuGet package is made, it also has to include a manifest of the contents of a NUSPEC file. A NUSpec file is an XML file in a specific structure which describes what is in the package. There are a lot of elements that can be used inside of a NUSPEC file. Here is an example of one using only the required elements.
<?xml version="1.0" encoding="utf-8"?> <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata> <id>MySpecialSoftware</id> <version>1.0</version> <description>Some software I'm packaging up.</description> <authors>Adam Bertram</authors> </metadata> </package>
After it’s created, this file will be placed in the same directory as other contents and should then be ready to be packaged up.
Creating a NuGet package
There are many ways to create a NuGet package. A very traditional way to do this is by using visual studio. NuGet was made by .NET developers to package their software, so after browsing the web for examples, you will see how to build packages with Visual Studio regularly. However, we are IT pros. Visual Studio is not needed; instead, we are going to be using PowerShell and a community module made by yours truly to create and also publish the package.
Suppose we’ve made this great PowerShell module. You took all of the steps needed to create a professional module by not only building the PSM1 file, but you also have a manifest, an external help file, and a couple of other files that are needed for the module. To share and distribute this, you could just copy a folder, but there’s a chance of certain files getting lost, and you also won’t get the ability to version your PowerShell module. You’d like to create a NuGet package from this PowerShell module and then share it on the PowerShell Gallery. After all, the PowerShell Gallery is simply a NuGet feed.
To begin, install the PSPostMan module from the PowerShell Gallery by downloading the package from the Gallery’s NuGet feed with Install-Module.
Install-Module -Name PSPostMan
We have a module called Lab which we’ve been working on, located in C:\Program Files\WindowsPowerShellModules\Lab. We’d like to create a NuGet package from that folder. PSPostMan provides a function named New-PMModulePackage that is preset to handle this. We just have to point the command to the Lab module’s folder.
New-PMModulePackage -Path 'C:Program FilesWindowsPowerShellModulesLab' -Verbose VERBOSE: Attempting to build package from '113ee5db-9f60-4c84-828d-f423f01521f4.nuspec'. Successfully created package 'C:Program FilesWindowsPowerShellModulesLabLab.1.0.0.nupkg'.
You might see that this has created a NUPKG file. This was the default format for a NuGet package. The New-PmModulePackage command even took care of making the NUSPEC file for us.
Once we’ve got the NuGet Package created, we then just have to point it to a NuGet feed URL. We can, for example, publish this package to the PowerShell Gallery if we know the feed URL. After we have the feed URL, we then have to know an optional API key.
Certain feeds need authentication first, but that will be discussed another time. To publish our package to any feed URL, we can use the Publish-PmPackage command:
Publish-PMPackage -Path 'C:Program FilesWindowsPowerShellModulesLabLab.1.0.0.nupkg' -FeedUrl 'https://www.powershellgallery.com/api/v2/' -ApiKey XXXXX
Once we get the hang of everything, we can even skip the Publish-PmPackage command and use the Publish-PMModule to read an installed module and automatically publish it:
Publish-PMModule -Name Lab -NuGetApiKey XXXX -FeedUrl 'https://www.powershellgallery.com/api/v2/'