NuGetAuthenticate@1 - NuGet 对 v1 任务进行身份验证

配置 NuGet 工具以使用 Azure Artifacts 和其他 NuGet 存储库进行身份验证。 需要 NuGet >= 4.8.5385、dotnet >= 6,或 MSBuild >= 15.8.166.59604。

语法

# NuGet authenticate v1
# Configure NuGet tools to authenticate with Azure Artifacts and other NuGet repositories. Requires NuGet >= 4.8.5385, dotnet >= 6, or MSBuild >= 15.8.166.59604.
- task: NuGetAuthenticate@1
  inputs:
    #forceReinstallCredentialProvider: false # boolean. Reinstall the credential provider even if already installed. Default: false.
    #nuGetServiceConnections: # string. Service connection credentials for feeds outside this organization.
# NuGet authenticate v1
# Configure NuGet tools to authenticate with Azure Artifacts and other NuGet repositories. Requires NuGet >= 4.8.5385, dotnet >= 6, or MSBuild >= 15.8.166.59604.
- task: NuGetAuthenticate@1
  inputs:
    #nuGetServiceConnections: # string. Service connection credentials for feeds outside this organization. 
    #forceReinstallCredentialProvider: false # boolean. Reinstall the credential provider even if already installed. Default: false.

输入

forceReinstallCredentialProvider - 重新安装凭据提供程序,即使已安装
boolean。 默认值:false

自选。 将凭据提供程序重新安装到用户配置文件目录,即使它已安装也是如此。 如果凭据提供程序已安装在用户配置文件中,该任务将确定是否被任务提供的凭据提供程序覆盖。 这可能会升级凭据提供程序(或可能降级)。


nuGetServiceConnections - 此组织外部源的服务连接凭据
string

自选。 NuGet 服务连接的逗号分隔列表 此组织或集合外部源的名称。 对于此组织或集合中的源,请将此保留为空;将自动使用生成的凭据。


任务控制选项

除任务输入之外,所有任务都具有控制选项。 有关详细信息,请参阅 控件选项和常见任务属性

输出变量

没有。

言论

重要

此任务仅适用于 NuGet >= 4.8.0.5385、dotnet >= 6,或 MSBuild >= 15.8.166.59604。

哪些工具与此任务兼容?

此任务配置支持 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/#77artifacts-credprovider/#108。 如果遇到这些问题,请确保具有最新的凭据提供程序,方法是在 NuGet 身份验证任务中将输入 forceReinstallCredentialProvider 设置为 true。 此设置还将确保凭据提供程序在解决问题时自动更新。

如果上述两项都无法解决问题,请启用 插件诊断日志记录,并将问题报告给 NuGetAzure Artifacts 凭据提供程序

此任务与 NuGetCommand 和 DotNetCoreCLI 任务有何不同?

此任务将 nuget.exe、dotnet 和 MSBuild 配置为使用 Azure Artifacts 或其他需要身份验证的存储库进行身份验证。 运行此任务后,可以在后续步骤(直接或通过脚本)调用工具以还原或推送包。

NuGetCommand 和 DotNetCoreCLI 任务需要使用任务还原或推送包,因为对 Azure Artifacts 的身份验证仅在任务的生存期内进行配置。 这可以防止你在自己的脚本中还原或推送包。 它还可能会阻止你将特定的命令行参数传递给该工具。

建议使用 NuGetAuthenticate 任务在管道中使用经过身份验证的源。

何时应在管道中运行此任务?

在使用 NuGet 工具将包还原或推送到经过身份验证的包源(例如 Azure Artifacts)之前,必须运行此任务。 没有其他订购要求。 例如,此任务可以在 NuGet 或 .NET Core 工具安装程序任务之前或之后安全地运行。

如何配置使用 ApiKey(“NuGet API 密钥”)的 NuGet 包源,例如 nuget.org?

某些包源(例如 nuget.org 在推送包时使用 API 密钥进行身份验证,而不是 username/password 凭据。 由于 NuGet 中的限制,此任务不能用于设置使用 API 密钥的 NuGet 服务连接。

相反:

  1. 配置包含 ApiKey 的 机密变量
  2. 使用 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)也可以使用的变量。

    警告:
    Linux 和 Mac 操作系统上的 http_proxyno_proxy 变量区分大小写,并且必须小写。 尝试使用 Azure Pipelines 变量设置环境变量将不起作用,因为它将转换为大写。 而是在自承载代理的计算机上设置环境变量,然后重启代理。

  • 根据 nuget.config 参考 文档中所述,手动或使用 nuget config -set 将代理设置添加到 用户级 nuget.config 文件中。

    警告:
    代理设置(如 http_proxy)必须添加到用户级配置中。如果在其他 nuget.config 文件中指定,将忽略它们。

如果此任务出现问题,如何调试?

若要从管道获取详细日志,请添加管道变量 system.debug 并设置为 true

此任务的工作原理是什么?

此任务将 Azure Artifacts 凭据提供程序 安装到 NuGet 插件目录中(如果尚未安装)。 然后,它会设置环境变量,例如 VSS_NUGET_URI_PREFIXESVSS_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 而不是 .NET Core 2.1,这是 NuGetAuthenticateV0 中必需的,不再受支持。 若要解决此问题,请在 dotnet 命令安装 .NET 6 之前使用UseDotNet@2任务。

- task: UseDotNet@2
  displayName: Use .NET 6 SDK
  inputs:
    packageType: sdk
    version: 6.x

例子

还原和推送组织中的 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

在上面的示例中,OtherOrganizationFeedConnectionThirdPartyRepositoryConnectionNuGet 服务连接的名称, 已配置和授权在管道中使用,并且具有与 nuget.config 或命令行参数中的连接匹配的 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

OtherOrganizationFeedConnectionThirdPartyRepositoryConnectionNuGet 服务连接的名称, 已配置并授权在管道中使用,并且具有与 nuget.config 或命令行参数中的 URL 匹配的 URL。

指向 Azure Artifacts 源的包源 URL 可以包含项目,也可能不包含该项目。 项目作用域源的 URL 必须包含项目,并且组织范围的源的 URL 不得包含项目。 详细了解 项目范围的源

要求

要求 描述
管道类型 YAML,经典版本,经典版本
运行时间 代理,DeploymentGroup
需求 没有
功能 此任务不满足作业中后续任务的任何要求。
命令限制 任何
Settable 变量 任何
代理版本 2.144.0 或更高版本
任务类别
要求 描述
管道类型 YAML,经典版本,经典版本
运行时间 代理,DeploymentGroup
需求 没有
功能 此任务不满足作业中后续任务的任何要求。
命令限制 任何
Settable 变量 任何
代理版本 2.120.0 或更高版本
任务类别