从 VSTS 将 nuget 包推送到 nuget.org 时忽略重复项
Ignore duplicates when pushing nuget package to nuget.org from VSTS
我在 VSTS 中构建了一个在存储库中的每次提交时触发的构建。一切正常,只有一个例外。
我们不会在每次提交时发布新版本的 nuget 包。所以我们的 nuget 推送构建步骤失败,http 状态代码为 409。我已经配置了该步骤,以便它无论如何都可以继续。
由于错误,构建只是 "partially successful"。我使用的是构建徽章,它也声明相同(没有上下文)。
如何告诉 VSTS 忽略 409 或仅替换现有包(nuget.org)?
你不能忽略 VSTS 构建中的 409 错误,也不能替换服务器中现有的包。
我建议您可以在发布中推送包,如果包存在则发布失败。
另一种方法是,您可以在构建期间在推送包之前检查服务器中的包(例如PowerShell,REST API)并设置推送包任务的条件(自定义条件)。
例如:
- 为构建定义添加一个变量(例如 hasPackage true)
- 检查软件包(PowerShell、Rest API 等…)
- 如果包存在,将变量设置为 false ("##vso[task.setvariable variable=hasPackage;]false")
- 为推送包任务设置自定义条件(例如eq(variables['hasPackage'],'false'))
更新:
NuGet 推送任务现在支持允许跳过重复项! (只需选中 NuGet 推送任务中的 允许跳过重复项 选项。
使用 -skipDuplicate
标志(自 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.
来源:https://docs.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-push#options
我们在 Azure Pipelines 上遇到了重复包的相同问题。
starian chen-MSFT 提出的解决方案很酷,但它需要一些脚本。
我们找到了需要较少努力的解决方案。您可以创建命令行步骤并使用以下参数调用 dotnet nuget push:
dotnet nuget push $(Build.ArtifactStagingDirectory)/*.nupkg --skip-duplicate --api-key $(Config.NuGetApiKey) --source https://api.nuget.org/v3/index.json
关键是参数 --skip-duplicate,如果包已经存在,它会跳过它们。
在变量 $(Config.NuGetApiKey) 中定义 NuGet.org 的 API 键。你应该把它设为秘密变量,这样它就不会出现在日志中的任何地方。
这是此命令的 YAML:
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'
我在 VSTS 中构建了一个在存储库中的每次提交时触发的构建。一切正常,只有一个例外。
我们不会在每次提交时发布新版本的 nuget 包。所以我们的 nuget 推送构建步骤失败,http 状态代码为 409。我已经配置了该步骤,以便它无论如何都可以继续。
由于错误,构建只是 "partially successful"。我使用的是构建徽章,它也声明相同(没有上下文)。
如何告诉 VSTS 忽略 409 或仅替换现有包(nuget.org)?
你不能忽略 VSTS 构建中的 409 错误,也不能替换服务器中现有的包。
我建议您可以在发布中推送包,如果包存在则发布失败。
另一种方法是,您可以在构建期间在推送包之前检查服务器中的包(例如PowerShell,REST API)并设置推送包任务的条件(自定义条件)。
例如:
- 为构建定义添加一个变量(例如 hasPackage true)
- 检查软件包(PowerShell、Rest API 等…)
- 如果包存在,将变量设置为 false ("##vso[task.setvariable variable=hasPackage;]false")
- 为推送包任务设置自定义条件(例如eq(variables['hasPackage'],'false'))
更新:
NuGet 推送任务现在支持允许跳过重复项! (只需选中 NuGet 推送任务中的 允许跳过重复项 选项。
使用 -skipDuplicate
标志(自 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.
来源:https://docs.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-push#options
我们在 Azure Pipelines 上遇到了重复包的相同问题。
starian chen-MSFT 提出的解决方案很酷,但它需要一些脚本。
我们找到了需要较少努力的解决方案。您可以创建命令行步骤并使用以下参数调用 dotnet nuget push:
dotnet nuget push $(Build.ArtifactStagingDirectory)/*.nupkg --skip-duplicate --api-key $(Config.NuGetApiKey) --source https://api.nuget.org/v3/index.json
关键是参数 --skip-duplicate,如果包已经存在,它会跳过它们。
在变量 $(Config.NuGetApiKey) 中定义 NuGet.org 的 API 键。你应该把它设为秘密变量,这样它就不会出现在日志中的任何地方。
这是此命令的 YAML:
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'