Search code examples
c#azurenugetazure-pipelinesnuget-package-restore

Problem with nuget.config - error NU1301: Unable to load the service index for source https://pkgs.dev.azure.com/[PRIVATE FEED]/nuget/v3/index.json


I'm having a problem with nuget.config in the following scenario.

I put a policy on the branch to realize a few tasks related restore, build and tests, image bellow.

Image policy branch

And I'm using the Artifacts Services with many private nuget feeds in my Azure DevOps, image bellow.

Image artifacts

Well, I've two project in .net 7, let's go call Project A and Project B.

The Project A references, through nuget.config, to Project B.

When I create a Pull Request, the build validation started and throw the following error, image bellow.

Pipeline result from Build Validation

D:\a\1\s\[NAME PROJECT].csproj : error NU1301: Unable to load the service index for source https://pkgs.dev.azure.com/[PRIVATE FEED]/nuget/v3/index.json.

YAML restore task in Azure DevOps Pipeline

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: restore
    projects: '**/*.csproj'
    feedsToUse: config
    nugetConfigPath: nuget.config

This is my nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
  <packageRestore>
    <add key="enabled" value="true" />
    <add key="automatic" value="true" />
  </packageRestore>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="[PRIVATE FEED]" value="https://pkgs.dev.azure.com/[PRIVATE FEED]/nuget/v3/index.json" />
  </packageSources>
  <apikeys>
    <add key="https://pkgs.dev.azure.com/[PRIVATE FEED]/nuget/v3/index.json" value="[PRIVATE APIKEY]" />
  </apikeys>
</configuration>

I'm already tried some solutions and the error persists.

  1. Use APIKEY in nuget.config.
  2. Use Basic Auth using my personal account in nuget.config.
  3. Create a service connections in Azure DevOps -> Project Settings -> Pipelines -> Service Connection.
  4. Set false Azure DevOps -> Project Settings -> Settings -> Limit job authorization scope to current project for release pipelines

Does someone please help me how to solve this problem and make my restore look for the nuget.config?


Solution

    • Check if your credentials for Basic Auth and API Key are correctly configured in nuget.config file
    • Make sure the nuget.config file is configured correctly.

    Try to provide the complete path to the root of the repository for your nuget.config file in your nuget restore task like below:-

    trigger:
    - cnov-SourceGenerator
    
    pool:
      vmImage: 'windows-latest'
    
    variables:
      solution: '**/*.sln'
      buildPlatform: 'Any CPU'
      buildConfiguration: 'Release'
    
    steps:
    - task: DotNetCoreCLI@2
      displayName: 'dotnet restore'
      inputs:
        command: 'restore'
        projects: '**/*.csproj'
        feedsToUse: 'config'
        nugetConfigPath: '$(System.DefaultWorkingDirectory)/PackageSourceMappingExample/nuget.config'
    

    enter image description here

    My nuget.config file from this sample repository

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <!-- Create a repo-specific global packages folder for full security benefit -->
      <config>
        <add key="globalPackagesFolder" value="globalPackagesFolder" />
      </config>
    
      <!-- Declare package sources/feeds -->
      <packageSources>
        <!-- To inherit the global NuGet package sources remove the <clear/> line below -->
        <clear />
        <add key="nuget" value="https://api.nuget.org/v3/index.json" />
        <add key="nuget-build" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/nuget-build/nuget/v3/index.json" />
      </packageSources>
    
      <!-- Map package ID prefixes and exact package IDs to specific sources target sources, so you always know where your packages come from -->
    
      <packageSourceMapping>
        <packageSource key="nuget">
          <package pattern="Microsoft.*" />
          <package pattern="Newtonsoft.Json" />
          <package pattern="NuGet.*" />
        </packageSource>
        <packageSource key="nuget-build">
          <package pattern="A" />
          <package pattern="B" />
          <package pattern="C" />
          <package pattern="NuGet.Protocol.VisualStudio" />
        </packageSource>
      </packageSourceMapping>
    
    </configuration>