NuGetAuthenticate@1 - NuGet 驗證 v1 工作

設定 NuGet 工具以向 Azure Artifacts 和其他 NuGet 存放庫進行驗證。 需要 NuGet > = 4.8.5385、dotnet > = 6 或 MSBuild > = 15.8.166.59604。

Syntax

# 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.

輸入

nuGetServiceConnections - 此組織外部摘要的服務連線認證
string.

選擇性。 此組織或集合外部摘要的 NuGet 服務連接 名稱逗號分隔清單。 對於此組織或集合中的摘要,請將此保留空白;組建的認證會自動使用。


forceReinstallCredentialProvider - 即使已安裝認證提供者,仍請重新安裝
boolean. 預設值:false

選擇性。 即使已安裝認證提供者,仍會將認證提供者重新安裝至使用者設定檔目錄。 如果認證提供者已安裝在使用者設定檔中,工作會判斷是否以工作提供的認證提供者覆寫。 這可能會升級 (或可能降級) 認證提供者。


工作控制項選項

除了工作輸入之外,所有工作都有控制選項。 如需詳細資訊,請參閱 控制選項和一般工作屬性

輸出變數

無。

備註

重要

此工作只與 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 Proxy 後方。 NuGetAuthenticate 是否會設定nuget.exe、dotnet 和 MSBuild 來使用我的 Proxy?

否。 雖然這項工作本身會在 代理程式設定為使用的Web Proxy 後方運作,但不會將 NuGet 工具設定為使用 Proxy。

若要這樣做,您可以:

  • 設定環境變數 http_proxy ,並選擇性地 no_proxy 設定您的 Proxy 設定。 如需詳細資訊,請參閱 NuGet CLI 環境變數 。 這些變數通常是其他非 NuGet 工具 (的變數,例如 curl) 也可以使用。

    注意
    http_proxyno_proxy 變數在 Linux 和 Mac 作業系統上區分大小寫,而且必須小寫。 嘗試使用 Azure Pipelines 變數來設定環境變數將無法運作,因為它會轉換成大寫。 相反地,在自我裝載代理程式的電腦上設定環境變數,然後重新開機代理程式。

  • 將 Proxy 設定新增至 使用者層級nuget.config 檔案,手動或使用 nuget config -set ,如 nuget.config參考 檔中所述。

    注意
    proxy 設定 (,例如 http_proxy) 必須新增至使用者層級設定。如果在不同的nuget.config檔案中指定,則會忽略它們。

如果我有這項工作的問題,如何?偵錯?

若要從管線取得詳細資訊記錄,請新增管線變數 system.debug 並設定為 true

這項工作如何運作?

如果尚未安裝 Azure Artifacts 認證提供者,此工作會將 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 命令之前UseDotNet@2工作來安裝 .NET 6。

- 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

在上述範例中, OtherOrganizationFeedConnectionThirdPartyRepositoryConnection 是已設定並授權用於管線的 NuGet 服務連線 名稱,且具有符合您 或 命令列引數中的 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

OtherOrganizationFeedConnectionThirdPartyRepositoryConnection 是已設定並授權用於管線的 NuGet 服務連線 名稱,且具有符合您nuget.config或命令列引數中 URL 的 URL。

指向 Azure Artifacts 摘要的套件來源 URL 可能會或未包含專案。 專案範圍摘要的 URL 必須包含專案,而組織範圍摘要的 URL 不得包含專案。 深入瞭解 專案範圍摘要

需求

需求 描述
管線類型 YAML、傳統組建、傳統版本
執行于 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 任意
Settable 變數 任意
代理程式版本 2.144.0 或更新版本
工作類別 套件
需求 描述
管線類型 YAML、傳統組建、傳統版本
執行于 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 任意
Settable 變數 任意
代理程式版本 2.120.0 或更新版本
工作類別 套件