As you may already know, there’s no App Center support for .NET MAUI yet (and possibly won’t be). Trailhead is using Azure Pipelines for many of our projects, including for generating our mobile builds and distributing them to the app stores (Apple and Android).
One of the challenges when you do this is bringing in private NuGet packages from a private feed. In this blog, we’ll review everything to take into account to be able to use a private NuGet feed with Azure Pipelines.
General steps
1. Create a Personal Access Token.
- Go to User settings (on the top-right menu on Azure DevOps) > Personal access token.
- Create the Personal access token.
- Copy the token, we’ll use in the next step.
2. Create a nuget.config file.
- See the reference in this blog, we’ll create the nuget.config file with this content. You can replace:
- YOUR_USERNAME with the username you prefer, this username will be used in the Android steps.
- YOUR_PASSWORD with the token created previously.
- MyProject: the name of your private NuGet feed.
<?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="YOUR_USERNAME" />
<add key="ClearTextPassword" value="YOUR_PASSWORD" />
</MyProject>
</packageSourceCredentials>
</configuration>
3. Add the nuget.config to the root of the project.
iOS Config
Surprise! There are no more steps to perform for iOS. So, you can update the branch where you define your yml.file with this change and run it.
Android Config
To get this working for your Android build, follow these steps:
1. Go to Azure Project settings > Pipelines – Service connections
2. Add a new service connection, look for Nuget, and click Next.
3. Select Basic Authentication
- Feed URL: replace with the URL you use to create your nuget.config file. Example:
https://pkgs.dev.azure.com/myorganization/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/_packaging/MyProject/nuget/v3/index.json
. - Username: replace with YOUR_USERNAME set in General Steps – 2.
- Password: replace with YOUR_PASSWORD set in General Steps – 2.
- Service connection name: the name of your private feed. This example will be MyProject.
Select the Grant access permission to all pipelines option and Save.
4. Update the yml.file
- Make sure to set the Task CmdLine@2 before the NuGet tasks.
- Replace ‘MyProject’ with the name of your private feed (in NuGetAuthenticate@1 and NuGetCommand@2).
- Replace restoreSolution (in NuGetCommand@2) with the Path to solution. For this example will be ‘
*.sln
‘.
...
steps:
- task: UseDotNet@2
displayName: 'Install .NET sdk'
inputs:
packageType: sdk
version: $(DotNetVersion)
installationPath: $(Agent.ToolsDirectory)/dotnet
- task: CmdLine@2
displayName: 'Install Maui Workload'
inputs:
script: 'dotnet workload install maui'
- task: NuGetAuthenticate@1
inputs:
nuGetServiceConnections: 'MyProject'
- task: NuGetToolInstaller@1
displayName: Use Nuget 6.6.1
inputs:
versionSpec: 6.x
- task: NuGetCommand@2
inputs:
command: 'restore'
restoreSolution: '*.sln'
feedsToUse: 'config'
nugetConfigPath: 'nuget.config'
externalFeedCredentials: 'MyProject'
...
Conclusion
Using a private NuGet feed with Azure Pipelines for your .NET MAUI projects is essential for managing and distributing your private packages into your cross-platform mobile projects efficiently. In this blog, we’ve walked through all the necessary steps to set this up for both Android and iOS builds. I hope this guide has been helpful in demystifying this process!
References
- NuGetCommand@2: link