The Problem
Recently, I was working on a Xamarin.Forms project consuming a private NuGet feed. Because the account I use to log in to Visual Studio has permissions to consume that feed, I just added the feed address to Visual Studio and I could use it without any problems.
But when I tried to build my app in in CI/CD pipeline in AppCenter, it was unable to restore the packages because it didn’t have access, and the build failed with the following error message:
The Solution
One way to tell AppCenter where the feed is located is to add a nuget.config file in the same folder where the solution (.sln) file is and to include the following content (you will need to use your own URL).
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="nuget.org" value="<https://api.nuget.org/v3/index.json>" /> <add key="MyProject" value="<https://pkgs.dev.azure.com/myorganization/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/_packaging/MyProject/nuget/v3/index.json>" /> </packageSources> <activePackageSource> <add key="All" value="(Aggregate source)" /> </activePackageSource> <packageSourceCredentials> <MyProject> <add key="Username" value="%NUGET_USER%" /> <add key="ClearTextPassword" value="%NUGET_PASSWORD%" /> </MyProject> </packageSourceCredentials> </configuration>
The %NUGET_USER% and %NUGET_PASSWORD% are parameters that I will later replace in AppCenter.
Next, I need to go to the account on Azure DevOps and create a Personal Access Token (PAT) used as the password. That will look something like this:
I will need to set a name, select the organization, and set the expiration date (up to one year). For the scope, I selected custom, with Packaging Read set. After creating the PAT, I must copy and safely store the PAT somewhere, because it will not be available anymore after closing the window.
The final step is to set the missing user and password parameters on AppCenter. For that, I create two environment variables that will be replaced on my nuget.config file when the branch is built.
The NUGET_USER is not required, but I like to specify it, so I could use it in the future if I want to, instead of a PAT. The NUGET_PASSWORD is the value of our PAT.
That’s all it takes! On our next build, AppCenter will be able to access the private Nuget feed and restore all of our packages.
Thanks for reading!