包:NuGet 身份验证
Azure DevOps Services
配置 NuGet 工具以使用 Azure Artifacts 和其他 NuGet 存储库进行身份验证。
重要
此任务仅与 NuGet >兼容 = 4.8.0.5385、dotnet >= 6 或 MSBuild >= 15.8.166.59604
YAML 代码片段
# Authenticate nuget.exe, dotnet, and MSBuild with Azure Artifacts and optionally other repositories
- task: NuGetAuthenticate@1
#inputs:
#nuGetServiceConnections: MyOtherOrganizationFeed, MyExternalPackageRepository # Optional
#forceReinstallCredentialProvider: false # Optional
参数
| 参数 | 说明 |
|---|---|
nuGetServiceConnections此组织外部源的服务连接凭据 |
(可选) 此组织/集合外部源的 NuGet 服务连接 名称的逗号分隔列表以额外设置。 如果只需要此组织/集合中的源,请将此源留空;将自动使用生成凭据。 |
forceReinstallCredentialProvider即使已安装凭据提供程序,也重新安装凭据提供程序 |
(可选) 将凭据提供程序重新安装到用户配置文件目录,即使已安装也是如此。 此设置可能会升级 (或可能降级) 凭据提供程序。 |
| 控件选项 |
示例
还原和推送组织中的 NuGet 包
如果使用的所有 Azure Artifacts 源都与管道位于同一组织中,则可以使用 NuGetAuthenticate 任务,而无需指定任何输入。 对于与管道运行所在的项目不同的项目范围源,必须手动为项目和源授予对管道项目的生成服务的访问权限。
nuget.config
<configuration>
<packageSources>
<!--
Any Azure Artifacts feeds within your organization will automatically be authenticated. Both dev.azure.com and visualstudio.com domains are supported.
Project scoped feed URL includes the project, organization scoped feed URL does not.
-->
<add key="MyProjectFeed1" value="https://pkgs.dev.azure.com/{organization}/{project}/_packaging/{feed}/nuget/v3/index.json" />
<add key="MyProjectFeed2" value="https://{organization}.pkgs.visualstudio.com/{project}/_packaging/{feed}/nuget/v3/index.json" />
<add key="MyOtherProjectFeed1" value="https://pkgs.dev.azure.com/{organization}/{project}/_packaging/{feed@view}/nuget/v3/index.json" />
<add key="MyOrganizationFeed1" value="https://pkgs.dev.azure.com/{organization}/_packaging/{feed}/nuget/v3/index.json" />
</packageSources>
</configuration>
若要使用服务连接,请在 NuGet 身份验证任务的输入中 nuGetServiceConnections 指定服务连接。 然后,可以在任务中引用服务连接 -ApiKey AzureArtifacts 。
nuget.exe
- task: NuGetAuthenticate@1
inputs:
nuGetServiceConnections: OtherOrganizationFeedConnection, ThirdPartyRepositoryConnection
- task: NuGetToolInstaller@1 # Optional if nuget.exe >= 4.8.5385 is already on the path
inputs:
versionSpec: '*'
checkLatest: true
- script: nuget restore
# ...
- script: nuget push -ApiKey AzureArtifacts -Source "MyProjectFeed1" MyProject.*.nupkg
dotnet
- task: NuGetAuthenticate@1
inputs:
nuGetServiceConnections: OtherOrganizationFeedConnection, ThirdPartyRepositoryConnection
- task: UseDotNet@2 # Optional if the .NET Core SDK is already installed
- script: dotnet restore
# ...
- script: dotnet nuget push --api-key AzureArtifacts --source https://pkgs.dev.azure.com/{organization}/_packaging/{feed1}/nuget/v3/index.json MyProject.*.nupkg
在上面的示例中OtherOrganizationFeedConnectionThirdPartyRepositoryConnection,是已配置和授权用于管道的 NuGet 服务连接的名称,并且具有与nuget.config或命令行参数中的 URL 匹配的 URL。
指向 Azure Artifacts 源的包源 URL 可能或可能不包含项目。 项目作用域源的 URL 必须包含项目,组织范围的源的 URL 不得包含项目。 了解详细信息。
在组织外部还原和推送 NuGet 包
如果使用来自其他组织的 Azure Artifacts 源或使用第三方经过身份验证的包存储库,则需要设置 NuGet 服务连接 并在输入中 nuGetServiceConnections 指定它们。
Azure Artifacts 组织中的源也将自动进行身份验证。
nuget.config
<configuration>
<packageSources>
<!-- Any Azure Artifacts feeds within your organization will automatically be authenticated -->
<add key="MyProjectFeed1" value="https://pkgs.dev.azure.com/{organization}/{project}/_packaging/{feed}/nuget/v3/index.json" />
<add key="MyOrganizationFeed" value="https://pkgs.dev.azure.com/{organization}/_packaging/{feed}/nuget/v3/index.json" />
<!-- Any package source listed here whose URL matches the URL of a service connection in nuGetServiceConnections will also be authenticated.
The key name here does not need to match the name of the service connection. -->
<add key="OtherOrganizationFeed" value="https://pkgs.dev.azure.com/{otherorganization}/_packaging/{feed}/nuget/v3/index.json" />
<add key="ThirdPartyRepository" value="https://{thirdPartyRepository}/index.json" />
</packageSources>
</configuration>
nuget.exe
- task: NuGetAuthenticate@1
inputs:
nuGetServiceConnections: OtherOrganizationFeedConnection, ThirdPartyRepositoryConnection
- task: NuGetToolInstaller@1 # Optional if nuget.exe >= 4.8.5385 is already on the path
inputs:
versionSpec: '*'
checkLatest: true
- script: nuget restore
# ...
- script: nuget push -ApiKey AzureArtifacts -Source "MyProjectFeed1" MyProject.*.nupkg
dotnet
- task: NuGetAuthenticate@1
inputs:
nuGetServiceConnections: OtherOrganizationFeedConnection, ThirdPartyRepositoryConnection
- task: UseDotNet@2 # Optional if the .NET Core SDK is already installed
- script: dotnet restore
# ...
- script: dotnet nuget push --api-key AzureArtifacts --source "MyProjectFeed1" MyProject.*.nupkg
OtherOrganizationFeedConnection 以及 ThirdPartyRepositoryConnection 已配置和授权用于管道的 NuGet 服务连接 的名称,并且具有与nuget.config或命令行参数中的 URL 匹配的 URL。
指向 Azure Artifacts 源的包源 URL 可能或可能不包含项目。 项目作用域源的 URL 必须包含项目,组织范围的源的 URL 不得包含项目。 了解详细信息。
开源
此任务在 GitHub 上开放源代码。 欢迎提供反馈和建议。
常见问题解答
哪些工具与此任务兼容?
此任务将配置支持 NuGet 跨平台插件的工具。 目前,其中包括nuget.exe、dotnet 和最新版本的 MSBuild,支持还原 NuGet 包。
具体而言,此任务将配置:
- nuget.exe版本 4.8.5385 或更高版本
- dotnet/ .NET 6 SDK 或更高版本 (NuGetAuthenticateV0 所需的 .NET Core 2.1,现已)
- MSBuild 版本 15.8.166.59604 或更高版本
但是,如果遇到任何问题,建议升级到最新的稳定版本。
在包还原期间,我收到“任务已取消”错误。 应采取何种操作?
NuGet 和 Azure Artifacts 凭据提供程序中的已知问题可能会导致此类错误,并更新到最新的 nuget 可能会有所帮助。
某些版本的 nuget/dotnet 中的 已知问题 可能会导致此错误,尤其是在资源约束计算机上的大型还原期间。 NuGet 5.2 和 .NET Core SDK 2.1.80X 和 2.2.40X 中修复了此问题。 如果使用的是较旧版本,请尝试升级 NuGet 或 dotnet 版本。 .NET Core 工具安装程序任务可用于安装较新版本的 .NET Core SDK。
此任务 () 安装的 Azure Artifacts 凭据提供程序也存在已知问题,包括 artifacts-credprovider/#77 和 artifacts-credprovider/#108。 如果遇到这些问题,请确保在 NuGet 身份验证任务中将输入 forceReinstallCredentialProvider 设置为 true 最新的凭据提供程序。 此设置还将确保凭据提供程序在解决问题时自动更新。
如果上述两者都无法解决问题,请启用 插件诊断日志记录 ,并将问题报告给 NuGet 和 Azure Artifacts 凭据提供程序。
此任务与 NuGetCommand 和 DotNetCoreCLI 任务有何不同?
此任务将nuget.exe、dotnet 和 MSBuild 配置为使用需要身份验证的 Azure Artifacts 或其他存储库进行身份验证。 运行此任务后,可以在后面的步骤中直接或通过脚本 (调用工具,) 还原或推送包。
NuGetCommand 和 DotNetCoreCLI 任务需要使用任务还原或推送包,因为对 Azure Artifacts 的身份验证仅在任务的生存期内进行配置。 这可以防止还原或推送你自己的脚本中的包。 它还可能阻止将特定命令行参数传递给该工具。
NuGetAuthenticate 任务是使用管道中经过身份验证的源的建议方法。
何时应在管道中运行此任务?
在使用 NuGet 工具将包还原或推送到经过身份验证的包源(如 Azure Artifacts)之前,必须运行此任务。 没有其他订购要求。 例如,此任务可以在 NuGet 或 .NET Core 工具安装程序任务之前或之后安全地运行。
如何实现配置使用 ApiKey (“NuGet API 密钥”) (例如 nuget.org)的 NuGet 包源?
某些包源(例如 nuget.org 在推送包时使用 API 密钥进行身份验证,而不是用户名/密码凭据)。 由于 NuGet 中的限制,此任务不能用于设置使用 API 密钥的 NuGet 服务连接。
应该:
- 配置包含 ApiKey 的机密变量
- 使用
nuget push -ApiKey $(myNuGetApiKey)或dotnet nuget push --api-key $(myNuGetApiKey)假设你命名变量时执行包推送myNuGetApiKey
我的代理位于 Web 代理后面。 NuGetAuthenticate 是否会设置 nuget.exe、dotnet 和 MSBuild 以使用我的代理?
错误。 虽然此任务本身将在 代理配置为使用的 Web 代理后面工作,但它不会将 NuGet 工具配置为使用代理。
为此,可以执行以下操作之一:
将环境变量
http_proxy设置为代理设置(可选no_proxy)。 有关详细信息,请参阅 NuGet CLI 环境变量 。 这些变量通常用于其他非 NuGet 工具 (的变量,例如 curl) 也可能使用。警告:
no_proxyLinuxhttp_proxy和 Mac 操作系统上的变量区分大小写,并且必须小写。 尝试使用 Azure Pipelines 变量设置环境变量将不起作用,因为它将被转换为大写。 而是在自承载代理的计算机上设置环境变量,然后重启代理。根据nuget.config 参考文档中所述,手动或使用
nuget config -set用户级nuget.config文件的代理设置。警告:
代理设置 ((如http_proxy) )必须添加到用户级配置中。如果在其他nuget.config文件中指定,将忽略它们。
如果此任务出现问题,如何实现调试?
若要从管道中获取详细日志,请将管道变量 system.debug 添加到 true。
此任务的工作原理是什么?
如果尚未安装 Azure Artifacts 凭据提供程序,此任务会将 Azure Artifacts 凭据提供程序 安装到 NuGet 插件目录中。
然后,它设置环境变量,例如 VSS_NUGET_URI_PREFIXES 并 VSS_NUGET_ACCESSTOKEN 配置凭据提供程序。 这些变量在作业的生存期内保持设置。
还原或推送包时,NuGet 工具会执行凭据提供程序,该提供程序使用上述变量来确定它是否应将凭据返回回该工具。
有关更多详细信息,请参阅凭据提供程序文档。
我的管道需要访问不同项目中的源
如果管道在托管源的项目不同的项目中运行,则必须设置其他项目以授予对生成服务的读/写访问权限。 有关更多详细信息,请参阅 Azure Pipelines 中的包权限 。
这是否适用于从外部分支触发的管道运行?
错误。 从外部分支触发的管道运行无权访问内部源身份验证的正确机密。 因此,它看起来就像身份验证任务成功,但需要身份验证 (的后续任务(如 Nuget 推送) )将失败,并出现以下行的错误: ##[error]The nuget command failed with exit code(1) and error(Response status code does not indicate success: 500 (Internal Server Error - VS800075: The project with id 'vstfs:///Classification/TeamProject/341ec244-e856-40ad-845c-af31c33c2152' does not exist, or you do not have permission to access it. (DevOps Activity ID: C12C19DC-642C-469A-8F58-C89F2D81FEA7)). 将拉取请求合并到源后,从该事件触发的管道将正确进行身份验证。
我已从 NuGetAuthenticateV0 更新到 NuGetAuthenticateV1,现在我的 dotnet 命令失败,401
如果要从 NuGetAuthenticateV0 更新到 NuGetAuthenticateV1 并获取运行 dotnet 命令的错误,请从日志中查找消息 It was not possible to find any compatible framework version 。 对于 dotnet 用户,NuGetAuthenticateV1 需要 .NET 6,而不是 NuGetAuthenticateV0 中所需的 .NET Core 2.1,现在不受支持。 若要解决此问题,请使用 dotnet 命令之前UseDotNet@2任务安装 .NET 6。
- task: UseDotNet@2
displayName: Use .NET 6 SDK
inputs:
packageType: sdk
version: 6.x