Continuous Deployment of Nuget Packages
In the previous articles, we have seen how to create Nuget packages for our software and how to share them with other people using Azure Artifacts. Moreover, we explained a few best practices that could be helpful, when we want to version our packages. In this last article, we will automate the whole process to create and version them using Azure Pipelines.
What is Azure Pipelines?
Azure Pipelines helps us to create release pipelines for our software. The use of Azure Pipelines has a lot of benefits:
- It is cross-platform;
- We can compile and distribute the code, written using different languages and different platforms (i.e.: web, mobile);
- There are on-premise or hosted agents available;
- We can create a release pipeline using YAML;
- it has Good extendibility thanks to a marketplace;
- It is free for freelancers or small teams (more info here);
In Azure Pipelines there is a distinct separation between build and release process: the former creates artifacts, the latter distributes them on different stages.
Another interesting feature about Azure Pipelines is its simplicity: the user experience or the pipeline status is always neat and easy to understand. Please note that Azure Pipelines is only one of the many products available in Azure DevOps.
Create a build for Nuget Packages
Let’s see how to create Nuget packages. We move to the Azure Pipelines section in Azure DevOps and create a new pipeline through the button “New” and then “Build Pipeline”. Inside the wizard, we set the code repository and then we can create both a build based on a YAML pipeline, or a build using the classic editor based on task composition. We are gonna explain the second option.
After choosing the repository, we define which technology we need to compile: let’s pick “.NET Desktop” as pipeline template.
Then we provide some information such as (see next picture):
- Build name;
- Agent pool (hosted or private);
- Agent Specification to compile our project based on development platform used(i.e.: .NET Framework, Node.js).
Now, we are ready to edit our build template to create the Nuget Package to upload to our feed. Firstly, we replace the task “Copy Files to: $(build.artifactstagingdirectory)” with a new task of type “Nuget” using the button “+”. Once added, we can set a few parameters such as:
- “Pack” as the command to execute;
- The path of the projects or the nuspec file, that we have seen how to create in one of the previous articles. If we take a look at the next picture, we see that, thanks to a string pattern, we can choose all the projects inside our solution;
- To add all referenced projects in our packages using the “Include referenced projects” flag.
We are ready to execute our build using the button “Queue”. After the execution, inside the summary page, we click on the “Artifacts” button to display the generated nupkg.
Nuget packages versioning
The next step is to package versioning: we will use GitVersion to generate version numbers. Sure enough, thanks to an extension, it is possible to automate number generation in our build version following SemVer principles. We just need to add a task using the “+” button and search for the term “GitVersion” in the search box. If it is not already present in our account, we can install it using the marketplace (more info at this link).
Now, let’s select the GitVersion task and set the following parameters:
- the path to the GitVersion.yml (as created in the previous article);
- enable the flag “Update AssemblyInfo files”.
Moreover, we must edit GitVersion.yml with the following line to set the right versioning format:
This way, the AssemblyInformationalVersion field is set using the right version number format, according to what we discussed in the last article.
Upload Nuget packages to Azure Artifacts
We are finally ready to upload our packages to the Azure Artifacts feed. For this purpose, we create a new release, that starts right after the build, which retrieves all nupkgs and upload them to the feed. In the build summary page, we press the “Release” button to create a new deploy pipeline.
In this case, we use an empty template through the “Empty Job” button. We should see something like this:
Next steps are:
- Set as stage name “Nuget Feed”;
- Add a task in this stage to upload our packages.
For the second step, we press on the “1 job, 0 task” link to switch to the release process detail page. This window is fairly similar to the build definition page and allows us to use tasks to define a release process. Also in this situation, we have an agent (private or hosted) that will execute the deploy process.
Let’s add a new task named “Nuget” using again the “+” button and set it as in the following picture:
Firstly, we set the “push” command (1). This command will retrieve all nupkg files in the build artifacts using the search pattern “/**/*.nupkg” (2) and it will upload them on the selected “Target feed” (3). Lastly, we enable the “Allow duplicates to be skipped” flag (4) to avoid that the release will generate an error in case that a version is already present in the feed. We are ready to launch our release using “Create Release” button and to verify that all packages have been correctly uploaded on the feed.
Happy versioning to y’all!