Search code examples
azure-devopspackagenuget-package

What is the best way to authenticate for a push to azure devops package feed?


I have an azure devops package feed which was working fine for my purposes, until recently a hard drive failure disrupted operations. Suddenly I found myself unable to push any packages without running into a 401 response. I was able to get it to work eventually after modifying a nuget.config file as described in the answer to this question, but given that this entire process was fairly convoluted, it seems like there should be a much more straight-forward way to set this up when needed. So that in the case I ever need to do this again in the future, and so that I don't have to rely on getting lucky with stack overflow answers, what is the best, most straight-forward method for setting up publishing to azure devops package feeds?


Solution

  • nuget.config is the most commonly used authentication method for push packages to feed. There are two ways to push packages to feed, I think you may need another.

    Pipeline push should be a easy way to achieve your requirement.

    trigger:
    - none
    
    pool:
      vmImage: 'windows-latest'
    
    steps:
    - checkout: self
      persistCredentials: true #This step will do the auth.
    - task: DotNetCoreCLI@2
      inputs:
        command: 'build'
        projects: '**/*.csproj'
        arguments: '--configuration Debug'
    
    - task: DotNetCoreCLI@2
      inputs:
        command: 'pack'
        packagesToPack: '**/*.csproj'
        versioningScheme: 'byPrereleaseNumber'
        majorVersion: '1'
        minorVersion: '0'
        patchVersion: '2'
    
    - task: NuGetCommand@2
      inputs:
        command: 'push'
        packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
        nuGetFeedType: 'internal'
        publishVstsFeed: 'xxx/xxx'
    

    enter image description here

    You just need to 'click' the feed you want push to and then change the step of YAML like the above.

    After that, give the permission to pipeline:

    enter image description here

    Finally, run the pipeline to push artifact to feed:

    enter image description here

    Each step of this method is very clearly and each time you want to push artifact, you just need to change several settings is ok(Version, Repo that the pipeline based on, artifact name). Create once, use for a long time.


    Additional:

    How to change the repo that the pipeline based on:

    enter image description here

    enter image description here

    Repository Structure on my side:

    enter image description here


    Everything of push artifacts:

    Publish Nuget packages(NuGet.exe)

    Publish Nuget packages(dotnet)

    Publish NuGet packages with Azure Pipelines (YAML/Classic)