How to add custom code via private Azure DevOps Artifact Feeds to your projects and build pipelines
Last updated on April 5, 2023
Whenever you implement code that is so generic that it could be taken advantage of by multiple solutions, it is a good idea to separate the code into a reusable project. The project can then be packaged and distributed to the solutions that benefit from it via a package feed such as NuGet or npm.
You’ve most likely already utilized libraries implemented by someone else from the public feeds. However, did you know you can also make your custom code available similarly and share it only with projects implemented within your organization? This way, you can easily take advantage of previously written code (that you cannot share publicly) in new work projects. A natural way to make this happen in the Microsoft ecosystem is via the Azure DevOps Artifact Feeds.
In this blog post, I’ll give you step-by-step instructions on adding custom .NET code to a private Azure DevOps Artifact Feed and how to consume it as a NuGet package from a different solution, all the way from start to finish. I hope you’ll find this blog post helpful and share it with your colleagues who might benefit from the information.
Prepare your development environment
Before you begin, install the Azure Artifacts Credential Provider and its prerequisites. Without it, authentication to your private artifact feed will fail.
Create the artifact feed
Let’s start by creating the artifact feed on Azure DevOps!
- On Azure DevOps, go to the project where you want to consume the reusable code package.
- Click on Artifacts on the left-hand navigation.
- Click on the Create Feed button at top right.
- Create a new feed with the following settings:
- Name: Give your feed a descriptive name. It could be, e.g., the name of a library you are about to add to the feed.
- Visibility: Grant appropriate view permissions to the feed. In the picture below, I’ve chosen to allow the people who are members of the Azure DevOps organization to view the feed.
- Upstream sources: As I am going to include only my private code in the feed, I unchecked this box. However, if you want to add public packages to the feed, keep the box checked.
- Scope: Select Project if you want the feed to be used only within the project. Alternatively, you can select Organization to make the feed available across all projects in your Azure DevOps organization.
Note down the feed reference
After the feed has been created, do the following steps to note down the information we’ll later need for connecting to the feed:
- Click on the Connect to Feed button at top right.
- Select dotnet.
- In the view that opens, you’ll see a bunch of XML (looks similar to the markup below). Copy-paste it to, e.g., Notepad.
Add the new artifact feed to your local package sources
Run the command below to add a reference on your machine to the Azure DevOps artifact feed we just created. You can get the source URL from the XML we noted earlier.
Add code to the artifact feed via a build pipeline
Coming soon!
Add code to the artifact feed manually
When implementing code, you should store code in a version control system, such as Azure DevOps or Github. It is not a requirement for setting up and consuming a package feed, but it is something that you should still do. There’s always a chance that someone in your organization wants to read or modify the packaged code in the future, requiring access to the original unpackaged source code. Because of this, always add the source code to version control even if you now think you only need to add it once to the feed.
I use Azure DevOps for work projects, but other platforms (e.g., Github) work just as fine. You can find instructions for creating a new Azure DevOps repository and pushing code into it on the Microsoft Docs.
Package the artifact feed code
After you’ve made sure the code is stored in a version control system, package the code by performing the following steps in Visual Studio:
- Change the build configuration to Release (dropdown).
- Right-click on the project.
- Select Pack.
Alternatively, you can run the following command, which does the same trick.
Push the package into the feed
Push the code we packaged in the previous step into the feed.
Tada! Your reusable code is in the Azure DevOps Artifacts feed, ready to be consumed by other projects!
Use the artifact feed packages in another project
So far, your commonly used code has been packaged and pushed into a private feed, and a reference to that feed has been added to your machine. Now, you can install the package from the private feed to your project like any package from the public NuGet feed.
- In Visual Studio, right-click on the project for which you wish to install the common code package.
- Select Manage NuGet packages.
- In the top right corner of the view, you should see a Package source dropdown. Open it and select your private feed.
- On the Browse tab, you’ll see your custom package. Select it and click on Install.
- You should now be able to reference the methods in the custom package and build your code successfully in Visual Studio!
If you don’t see your private feed in the Package source dropdown, reopen your Visual Studio solution to update the package sources to display the source we just added with the dotnet command a moment ago.
Include the package feed in your Azure DevOps build pipeline
Create a nuget.config file
It is best practice to publish your code to a hosting platform (e.g., Azure) via CI/CD pipelines instead of using the publishing feature in Visual Studio. Because of this, we need to include a reference to our new private package feed so that our Azure DevOps build pipeline can also find it.
- In the root folder of the project for which you installed the package from the private feed, create a new file called nuget.config.
- Do you remember the XML we copied to Notepad earlier? Now, copy and paste all of that into this file.
- Does your project also use publicly available NuGet packages? If yes, you should also add a reference for the public nuget.org feed. The file should look end up looking similar to this:
Alright, we are now done with that file.
Edit the pipeline YAML file
- Next, open your pipeline YAML file. By default, it is called azure-pipelines.yml and is located at the root of your solution.
- Your build pipeline most likely already includes the NuGetCommand@2. Add feedsToUse and nugetConfigPath settings to it. It should end up looking something like the below:
Push the new nuget.config and the changes to the pipeline file to version control, so they get used when the pipeline runs. After that, you are all set!
Afterword
Whenever we do something for the first time, finding all the required steps clearly explained and in a single location is always helpful. I hope this blog post can serve as a valuable resource for people looking to set up their first private feed. Seeing people avoiding duplicate code by implementing reusable methods, classes, and libraries — and ideally sharing those with others — always makes me happy.
If you enjoyed reading this blog post and would like to read more similar content in the future, feel free to follow me on your favorite social media platforms, and sign up for my Insider newsletter.
Let me know if you notice any information missing if some instructions could be made more evident, or if you simply found the blog post delightful! All of this will help me produce more useful content for you in the future. Other than that, thank you for reading, and until next time!
Laura