使用 Azure Devops 发布 Nuget 包

Publish Nuget Package using Azure Devops

我正在尝试在 Azure DevOps 中创建一个发布管道,它将向 Nuget.org 发布一个包。构建管道工作得很好,并将包创建为构建工件。我可以发布到 Azure Artifacts 中托管的提要,但我无法发布到 Nuget.org。我认为问题出在服务连接上。 .

我试过使用 ApiKey,但会引发错误

DotNet Core does not support encrypted API Key' error

这是尝试推送包的发布步骤。

我也尝试过 https://api.nuget.org/v3/index.json 供稿 URL 但这似乎没有什么不同。

日志输出。

2018-10-21T23:27:36.3177322Z ##[section]Starting: Nuget Push 2018-10-21T23:27:36.3183449Z ============================================================================== 2018-10-21T23:27:36.3183547Z Task : .NET Core 2018-10-21T23:27:36.3183635Z Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command. For package commands, supports NuGet.org and authenticated feeds like Package Management and MyGet. 2018-10-21T23:27:36.3183729Z Version : 2.141.0 2018-10-21T23:27:36.3183791Z Author : Microsoft Corporation 2018-10-21T23:27:36.3183871Z Help : More Information 2018-10-21T23:27:36.3183936Z ============================================================================== 2018-10-21T23:27:37.1663123Z [command]C:\Windows\system32\chcp.com 65001 2018-10-21T23:27:37.1762529Z Active code page: 65001 2018-10-21T23:27:37.1808736Z SYSTEMVSSCONNECTION exists true 2018-10-21T23:27:37.3473599Z SYSTEMVSSCONNECTION exists true 2018-10-21T23:27:37.4707171Z SYSTEMVSSCONNECTION exists true 2018-10-21T23:27:37.4739974Z e3e8a3af-5c6c-44e9-820c-c62af0972256 exists true 2018-10-21T23:27:37.4807474Z Saving NuGet.config to a temporary config file. 2018-10-21T23:27:37.4833034Z Saving NuGet.config to a temporary config file. 2018-10-21T23:27:37.4919745Z Using authentication information for the following URI: https://www.nuget.org/api/v2/package 2018-10-21T23:27:37.4988034Z [command]C:\hostedtoolcache\windows\dncs.1.105\x64\dotnet.exe nuget push "{package}" --source https://www.nuget.org/api/v2/package --api-key RequiredApiKey 2018-10-21T23:27:38.3984300Z info : Pushing TranslatorConsole.1.0.0.6.nupkg to 'https://www.nuget.org/api/v2/package'... 2018-10-21T23:27:38.4171650Z info : PUT https://www.nuget.org/api/v2/package/ 2018-10-21T23:27:38.8798808Z info : Forbidden https://www.nuget.org/api/v2/package/ 462ms 2018-10-21T23:27:38.9562536Z error: Response status code does not indicate success: 403 (The specified API key is invalid, has expired, or does not have permission to access the specified package.). 2018-10-21T23:27:40.2195255Z ##[error]Error: C:\hostedtoolcache\windows\dncs.1.105\x64\dotnet.exe failed with return code: 1 2018-10-21T23:27:40.2206711Z ##[error]Packages failed to publish 2018-10-21T23:27:40.2307763Z ##[section]Finishing: Nuget Push

Github 中跟踪了一个问题:DotNetCore currently does not support using an encrypted Api Key

Using an ApiKey is currently not supported in dotnet because the required libraries for encrypting the key are not available, sorry for the inconvenience. You should be able to use a service endpoint configured with a username/password combination. If you can only use an ApiKey, I would suggest using the nuget 2.* task to push.

所以,您可以尝试使用Nuget 2.*任务来推送包。 (添加任务 --> 包 --> Nuget)

或者您可以尝试通过调用 dotnet nuget push 命令通过命令行任务将包推送到 NuGet 服务器来处理此问题。引用此线程:

我正在处理同样的问题 - 从 Azure DevOps 向 NuGet.org 提要发布一个 nuget。尽管这个 仍然有效,但有一个简单的方法可以做到,答案对我帮助不大。

解决方案

第 1 步

在 NuGet.org administration.

中生成 ApiKey

第 2 步

将 ApiKey 作为 secret variable 添加到您的管道中。

最终产品应如下所示:

第 3 步

通过

使用 PowerShell 任务更新管道 YAML
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'nuget push $(Build.ArtifactStagingDirectory)\*.nupkg -ApiKey $(myNuGetApiKey) -Source https://api.nuget.org/v3/index.json'

您无需在此任务中进行任何更改。它适用于 Azure DevOps 提供的任何标准 NuGet 打包方式。

发布更新后的 yaml 管道,一切顺利。

整个管道

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:
- task: NuGetToolInstaller@1
  inputs:
    versionSpec: '5.7.0'
    checkLatest: true
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/YourProjectNameOr*ForAll.csproj'
- task: NuGetCommand@2
  inputs:
    command: pack
    packagesToPack: '**/YourProjectNameOr*ForAll.csproj'
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'nuget push $(Build.ArtifactStagingDirectory)\*.nupkg -ApiKey $(myNuGetApiKey) -Source https://api.nuget.org/v3/index.json'
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'nuget push $(Build.ArtifactStagingDirectory)\*.snupkg -ApiKey $(myNuGetApiKey) -Source https://api.nuget.org/v3/index.json'

注意:第二个任务是发布符号包。如果您的项目不支持源 link,则可以省略此任务。 This 来自 NuGet 程序经理 Karam Nandwani 的文章指出,*.nupkg*.snupkg 将由一个 nuget 命令自动发布,但这是事实。至少现在是这样。