Ignore duplicates when pushing nuget package to nuget.org from VSTS
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://docs.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-push#options
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'
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:
- Add a variable to build definition (e.g. hasPackage true)
- Check packages (PowerShell, Rest API etc…)
- If the package is existing, set the variable to false ("##vso[task.setvariable variable=hasPackage;]false")
- 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.