12

I got a build in VSTS that are triggered on every commit in the repository. Everything works great with one exception.

We do not release a new version of the nuget package on every commit. So our nuget push build step fails with http status code 409. I've configured that step so that it can continue anyway.

Due to the error the build is just "partially successful". I'm using the a build badge which also states the same (without context).

How can I tell VSTS to ignore 409 or just replace the existing package (on nuget.org)?

jgauffin
  • 99,844
  • 45
  • 235
  • 372

4 Answers4

7

You can’t ignore 409 error in VSTS build and can’t replace the existing package in server.

I recommend that you can push the package in the release and fail the release if package is existing.

Another way is that, you can check the package in server before push package (e.g. PowerShell, REST API) during the build and set the condition for push package task (Custom Condition).

For example:

  1. Add a variable to build definition (e.g. hasPackage true)
  2. Check packages (PowerShell, Rest API etc…)
  3. If the package is existing, set the variable to false ("##vso[task.setvariable variable=hasPackage;]false")
  4. Set Custom condition for push package task (e.g. eq(variables['hasPackage'],'false'))

Update:

Allow duplicates to be skipped is supported in NuGet Push Task now! (Just check Allow duplicates to be skipped option in NuGet Push task.

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • Any reason why It was removed from the dotnetcore push task? Thanks – MeTitus Jul 03 '18 at 15:22
  • 2
    "Allow duplicates to be skipped" option is not available for Nuget.org or any other external NuGet server. – Antao Almada Dec 11 '18 at 08:37
  • 2
    We tried to use the "Allow duplicates to be skipped" option no TFS 2018. But it seems not to work. The log displays: "T12:07:21.8171891Z ##[warning]The 'Allow duplicates to be skipped' option is currently only available on Visual Studio Team Services. If NuGet.exe encounters a conflict, the task will fail." Any idea? – MiGro Jan 23 '19 at 12:11
  • @Neo unfortunately not. – MiGro Aug 20 '19 at 08:16
  • The _allowPackageConflicts_ can be specified in YAML snippet https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/package/nuget?view=azure-devops – lyolikaa Feb 18 '22 at 16:33
6

Use -SkipDuplicate flag (available since NuGet 5.1):

(5.1+) If a package and version already exists, skip it and continue with the next package in the push, if any.

Source: https://learn.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-push#options

Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
1

We had the same problem with duplicated packages on Azure Pipelines.

Solution proposed by starian chen-MSFT is cool but it requires some scripting.

We came to solution which requires less efforts. You can create command line step and invoke dotnet nuget push with the following parameters:

dotnet nuget push $(Build.ArtifactStagingDirectory)/*.nupkg --skip-duplicate --api-key $(Config.NuGetApiKey) --source https://api.nuget.org/v3/index.json

The key is parameter --skip-duplicate which just skips the packages if they already exist.

In variable $(Config.NuGetApiKey) define your API key for NuGet.org. You should make it secret variable, so that it does not appear anywhere in the logs.

Here is the YAML for this command:

steps:
- script: |
   dotnet nuget push $(Build.ArtifactStagingDirectory)/*.nupkg --skip-duplicate --api-key $(Config.NuGetApiKey) --source https://api.nuget.org/v3/index.json
   
  failOnStderr: true
  displayName: 'Publish NuGet Package'
CodeFuller
  • 30,317
  • 3
  • 63
  • 79
0

I have had zero luck with dotnet push - and here's my context:

My dotnet pack is set to include symbols and source inlucding source or symbols

and always generating a unique preRelease semVer.. (here's yaml version of above dotnet pack)

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet pack'
  inputs:
    command: pack
    nobuild: true
    includesymbols: true
    includesource: true
    versioningScheme: byPrereleaseNumber
    majorVersion: '$(AssemblyInfo.AssemblyInformationalVersion.Major)'
    minorVersion: '$(AssemblyInfo.AssemblyInformationalVersion.Minor)'
    patchVersion: '$(AssemblyInfo.AssemblyInformationalVersion.Patch)'

... and yet I get 409 responses with my freshly generated semVer!! ** BLAH!! **

My Solution: just use NuGet Push instead!

- task: NuGetCommand@2
  displayName: 'NuGet push'
  inputs:
    command: push
    publishVstsFeed: 'b64b1c9a-18fc-4e6f-aae7-3726c813f034'
  continueOnError: true

my preferred pipeline

Now my debug build-configuration CI pipeline builds full-source-and-symbol-accompanied NuGet packages in pre-release mode without failing the pipeline or putting caution symbols on it. YAY

I use a separate near identical CI pipeline to build in 'release' mode and create 'release' style semVer's.

bkwdesign
  • 1,953
  • 2
  • 28
  • 50