azure-devopsazure-pipelinesnugetpipelineazure-artifacts

Error 400 When Publishing NuGet Packages to Azure Artifacts: Unable to load the service index for source


Description:

I am encountering an error while trying to publish NuGet packages to an Azure Artifacts feed using an Azure DevOps pipeline. The error message I am receiving is as follows:

   at NuGet.CommandLine.Program.MainCore(String workingDirectory, String[] args)
##[error]The nuget command failed with exit code(1) and error(Unable to load the service index for source https://pkgs.dev.azure.com/<<organization>>/<<feed>>/nuget/v3/index.json.
  Response status code does not indicate success: 400 (Bad Request).

Context:

The YAML pipeline configuration is as follows:

trigger:
  branches:
    include:
      - develop

pool:
  vmImage: 'windows-latest'

variables:
  packageVersion: '1.0.$(Build.BuildId)'

steps:
- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '8.x'

- script: |
    dotnet restore
  displayName: 'Restore NuGet packages'

- script: |
    dotnet build --configuration Release
  displayName: 'Build project'

- script: |
    dotnet pack --configuration Release --version-suffix $(packageVersion) --output $(Build.ArtifactStagingDirectory)
  displayName: 'Create NuGet package'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

- task: NuGetCommand@2
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg'
    publishVstsFeed: 'https://pkgs.dev.azure.com/<<organization>>/<<feed>>/nuget/v3/index.json'
    apiKey: '$(System.AccessToken)'
  displayName: 'Push NuGet package to Azure Artifacts'

Problem:

The error indicates that the request to the NuGet feed returned a 400 (Bad Request) status code, with the message:

Unable to load the service index for source https://pkgs.dev.azure.com/<<organization>>/<<feed>>/nuget/v3/index.json.
Response status code does not indicate success: 400 (Bad Request).

Question:

How can I resolve this authorization error and successfully publish my NuGet packages? Are there additional checks or configurations that I should review to fix this issue?

Thank you in advance for your help!

I configured a YAML pipeline in Azure DevOps to restore, build, pack, and publish NuGet packages to an Azure Artifacts feed. I expected the NuGetCommand@2 task to successfully push the package to the feed without errors. However, the process fails with a 400 Bad Request error, indicating issues with loading the service index for the specified feed URL.


Solution

  • I could reproduce the 400 error based on your pipeline definition, which didn't seem to use the properties of the NuGetCommand@2 task correctly.

    enter image description here

    Here is my working nuget push step for your reference.

    - task: NuGetCommand@2
      inputs:
        command: 'push'
        packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg'
        publishVstsFeed: 'OrgScopeFeed-NuGet' # Use the organizational feed name directly
        # apiKey: '$(System.AccessToken)' # apiKey is not a valid property 
      displayName: 'Push NuGet package to Azure Artifacts'
    

    enter image description here