githubazure-pipelinesmauinuget-package-restoregithub-packages

Restore all nuget packages in Azure pipeline YML


I have a MAUI application that is stored in a Github repo. Along with that repo there is a custom nuget package also stored in Github Packages. I have no problems with all needed processes when in Visual Studio 2022 and running local. But I have created a Azure project with the Github source linked so that I can create an Azure pipeline for production branch build and then a release pipeline. My build is failing because it can not find the custom nuget package. I have added two tasks to my pipeline YML (configure package feed and the dotnet restore) to fix this issue but it is still failing.

      - task: DotNetCoreCLI@2
        displayName: Configure Nuget Feed
        inputs:
          targetType: 'inline'
          script: |
            dotnet nuget add source $(nuget_feed_url) -n $(nuget_feed_name) -u $(nuget_user_name) -p $(nuget_token) --store-password-in-clear-text
       
      - task: DotNetCoreCLI@2
        displayName: Restore packages
        inputs:
          targetType: 'inline'
          script: 
            dotnet restore src/HealthManager/MyProjectName.csproj

Error within Azure Build window is:

##[error]src\MyProjectName\MyProjectName.csproj(0,0): Error NU1101: Unable to find package MyProjectName.Maui.Package. No packages exist with this id in source(s): C:\hostedtoolcache\windows\dotnet\library-packs, Microsoft Visual Studio Offline Packages, nuget.org

Can someone help me with the proper way to get the custom nuget package restored in the Azure pipeline?

As stated I tried adding two new tasks in the Build pipeline YML

So I have one custom package that comes from a private Github packages and all other packages are public from nuget.org of several different providers. I have Visual Studio 2022 settings to use "Package Source: All" as the package sources are setup in the Visual Studio Options screen and then to restore I just use the Visual Studio built-in restore menu or Manage Packages screen.


Solution

  • Update

    Per you update to restore a private GitHub package with all other packages from the public source of nuget.org, that are referenced by your dotnet project, you may try the two options below.

    Add both https://nuget.pkg.github.com/<GitHub_ACC>/index.json and https://api.nuget.org/v3/index.json as the sources for dotnet restore in a script task;

    - script: |
        dotnet nuget add source --name nuget.org "https://api.nuget.org/v3/index.json"
        dotnet nuget add source --username $(GITHUB_ACC) --password "$(GITHUB_PAT)" --store-password-in-clear-text --name github1 "https://nuget.pkg.github.com/$(GITHUB_ACC)/index.json"
    
        dotnet restore $(System.DefaultWorkingDirectory)/path/to/TheProjectFile.csproj \
        --packages $(System.DefaultWorkingDirectory)/NuGet1 \
        --verbosity minimal
        tree $(System.DefaultWorkingDirectory)/NuGet1
    

    Add both https://nuget.pkg.github.com/<GitHub_ACC>/index.json and https://api.nuget.org/v3/index.json as the sources in the nuget.config file for DotNetCoreCLI@2 task to restore packages, referencing the NuGet service connection that stores the GitHub PAT.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <packageSources>
            <clear />
            <add key="github" value="https://nuget.pkg.github.com/<GitHub_ACC>/index.json" />
            <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
        </packageSources>
    </configuration>
    
    - task: DotNetCoreCLI@2
      inputs:
        command: 'restore'
        projects: '$(System.DefaultWorkingDirectory)/path/to/TheProjectFile.csproj '
        feedsToUse: 'config'
        nugetConfigPath: '$(System.DefaultWorkingDirectory)/nuget.config'
        externalFeedCredentials: 'GitHubNuGetSvcCnn'
        restoreDirectory: '$(System.DefaultWorkingDirectory)/NuGet'
    

    Your DotNetCoreCLI@2 pipeline task didn't seem to use a valid syntax. As far as I have tested, the task will not reference the nuget.config file where the github source was added by the dotnet nuget add source command, it references the tempNuGet.config file generated by the combination of the existing nuget.config file in your repo with the credentials stored in the NuGet service connection.

    Therefore, we can either run dotnet nuget add source and dotnet restore command in a singe - script task;

    - script: |
        dotnet nuget add source --username $(GITHUB_ACC) --password "$(GITHUB_PAT)" --store-password-in-clear-text --name github1 "https://nuget.pkg.github.com/$(GITHUB_ACC)/index.json"
        dotnet restore --packages $(System.DefaultWorkingDirectory)/NuGet1 --verbosity normal
        tree $(System.DefaultWorkingDirectory)/NuGet1
    

    Image Or we can add the nuget.config file in our repo and create the NuGet service connection like below to add source and authentication information for the DotNetCoreCLI@2 task to restore from.

    - task: DotNetCoreCLI@2
      inputs:
        command: 'restore'
        projects: '**/*.csproj'
        feedsToUse: 'config'
        nugetConfigPath: '$(System.DefaultWorkingDirectory)/nuget.config'
        externalFeedCredentials: 'GitHubNuGetSvcCnn'
        restoreDirectory: '$(System.DefaultWorkingDirectory)/NuGet'
    

    nuget.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <packageSources>
            <clear />
            <add key="github" value="https://nuget.pkg.github.com/<GitHub_ACC>/index.json" />
        </packageSources>
    </configuration>
    

    GitHubNuGetSvcCnn authenticating with a personal access token Image

    Image