简体   繁体   English

将 nuget 包从 VSTS 推送到 nuget.org 时忽略重复项

[英]Ignore duplicates when pushing nuget package to nuget.org from VSTS

I got a build in VSTS that are triggered on every commit in the repository.我在 VSTS 中构建了一个在存储库中的每次提交时都会触发的构建。 Everything works great with one exception.除了一个例外,一切都很好。

We do not release a new version of the nuget package on every commit.我们不会在每次提交时发布新版本的 nuget 包。 So our nuget push build step fails with http status code 409. I've configured that step so that it can continue anyway.所以我们的 nuget push 构建步骤失败,http 状态代码为 409。我已经配置了该步骤,以便它可以继续。

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)?如何告诉 VSTS 忽略 409 或仅替换现有包(在 nuget.org 上)?

You can't ignore 409 error in VSTS build and can't replace the existing package in server.您不能忽略 VSTS 构建中的 409 错误,也不能替换服务器中的现有包。

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 (eg PowerShell, REST API) during the build and set the condition for push package task (Custom Condition).另一种方式是,您可以在构建过程中在推送包之前检查服务器中的包(例如PowerShell,REST API)并设置推送包任务的条件(自定义条件)。

For example:例如:

  1. Add a variable to build definition (eg hasPackage true)添加一个变量来构建定义(例如 hasPackage true)
  2. Check packages (PowerShell, Rest API etc…)检查包(PowerShell、Rest API 等...)
  3. If the package is existing, set the variable to false ( "##vso[task.setvariable variable=hasPackage;]false" )如果包存在,将变量设置为 false ( "##vso[task.setvariable variable=hasPackage;]false" )
  4. Set Custom condition for push package task (eg eq(variables['hasPackage'],'false'))为推送包任务设置自定义条件(例如 eq(variables['hasPackage'],'false'))

Update:更新:

Allow duplicates to be skipped is supported in NuGet Push Task now!现在 NuGet 推送任务支持允许跳过重复项! (Just check Allow duplicates to be skipped option in NuGet Push task. (只需在 NuGet 推送任务中选中允许跳过重复项选项。

Use -skipDuplicate flag (available since NuGet 5.1):使用-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. (5.1+) 如果包和版本已经存在,跳过它并继续推送中的下一个包(如果有)。

Source: https://docs.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-push#options来源: 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.我们在 Azure Pipelines 上遇到重复包的问题。

Solution proposed by starian chen-MSFT is cool but it requires some scripting. starian chen-MSFT 提出的解决方案很酷,但它需要一些脚本。

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:

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.关键是参数 --skip-duplicate 如果它们已经存在,它只会跳过包。

In variable $(Config.NuGetApiKey) define your API key for NuGet.org.在变量 $(Config.NuGetApiKey) 中定义 NuGet.org 的 API 密钥。 You should make it secret variable, so that it does not appear anywhere in the logs.你应该让它成为秘密变量,这样它就不会出现在日志中的任何地方。

Here is the YAML for this command:这是此命令的 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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有一种自动的方法来生成NuGet包,准备从VS项目发布到nuget.org - Is there an automated way to generate NuGet package ready for publishing to nuget.org from VS project nuget.org上的源无法访问? - The source at nuget.org is unreachable? 即使我已经从 NuGet.config 中删除了提要,Jenkins 也会从 nuget.org 下载 nuget 包 - Jenkins downloads nuget packages from nuget.org even though I have removed the feed from NuGet.config 无法找到包 ***。 源中不存在具有此 ID 的包:“Local Nuget”、Microsoft Visual Studio 离线包、nuget.org - Unable to find package ***. No packages exist with this id in source(s): "Local Nuget", Microsoft Visual Studio Offline Packages, nuget.org Visual Studio 无法连接到 Windows 上的 Nuget.org 7 - Visual Studio cannot connect to Nuget.org on Windows 7 错误 NU1101:无法找到包 ''。 源中不存在具有此 ID 的包:Microsoft Visual Studio 离线包,nuget.org - error NU1101: Unable to find package ''. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages, nuget.org 为什么VSTS nuget feed中的nuget想要多次安装相同的软件包并抛出错误? - Why nuget from VSTS nuget feed wants to install the same package multiple times and throwing error? 如何在vsts中检查nuget的“程序包管理”? - how to check for nuget “package management” in vsts? 来自 CMake 的 Nuget 包 - Nuget package from CMake 从nuget包引用nuget包 - Referencing nuget packages from nuget package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM