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/#77 和 artifacts-credprovider/#108。 如果您遇到這些問題,請確定您有最新的認證提供者,方法是將輸入 forceReinstallCredentialProvider
設定為 NuGet 驗證工作中 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 套件來源(“NuGet API 金鑰”),例如 nuget.org?
某些套件來源,例如 nuget.org 在推送套件時使用 API 金鑰進行驗證,而不是 username/password
認證。 由於 NuGet 的限制,此工作無法用來設定使用 API 金鑰的 NuGet 服務連線。
相反:
- 設定包含 ApiKey 秘密變數
- 假設您命名變數
myNuGetApiKey
,請使用nuget push -ApiKey $(myNuGetApiKey)
或dotnet nuget push --api-key $(myNuGetApiKey)
來執行套件推送
我的代理程式位於 Web Proxy 後方。 NuGetAuthenticate 是否會設定 nuget.exe、dotnet 和 MSBuild 來使用我的 Proxy?
不。 雖然此工作本身會在 Web Proxy 後方運作,您的代理程式已設定為使用,但它不會將 NuGet 工具設定為使用 Proxy。
若要這樣做,您可以:
將環境變數設定
http_proxy
,並選擇性地no_proxy
至您的 Proxy 設定。 如需詳細資訊,請參閱 NuGet CLI 環境變數。 這些變數通常是其他非 NuGet 工具(例如 curl)也可以使用的變數。注意:
Linux 和 Mac 操作系統上的http_proxy
和no_proxy
變數會區分大小寫,而且必須小寫。 嘗試使用 Azure Pipelines 變數來設定環境變數將無法運作,因為它會轉換成大寫。 相反地,請在自我裝載代理程式的計算機上設定環境變數,然後重新啟動代理程式。手動或使用
nuget config -set
,如 nuget.config 參考 檔中所述,將 Proxy 設定新增至 用戶層級 nuget.config 檔案。注意:
Proxy 設定(例如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,而不是 .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
在上述範例中,OtherOrganizationFeedConnection
和 ThirdPartyRepositoryConnection
是已設定並授權用於管線之 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
OtherOrganizationFeedConnection
和 ThirdPartyRepositoryConnection
是 NuGet 服務連線的名稱, 已設定並授權在管線中使用,且具有符合您 nuget.config 或命令行自變數中 URL 的名稱。
指向 Azure Artifacts 摘要的套件來源 URL 不一定包含專案。 專案範圍摘要的 URL 必須包含專案,而組織範圍摘要的 URL 不得包含專案。 深入瞭解 項目範圍摘要。
要求
要求 | 描述 |
---|---|
管線類型 | YAML、傳統組建、傳統版本 |
執行於 | Agent、DeploymentGroup |
需求 | 沒有 |
功能 | 此工作不符合作業中後續工作的任何需求。 |
命令限制 | 任何 |
Settable 變數 | 任何 |
代理程式版本 | 2.144.0 或更新 |
工作類別 | 包 |
要求 | 描述 |
---|---|
管線類型 | YAML、傳統組建、傳統版本 |
執行於 | Agent、DeploymentGroup |
需求 | 沒有 |
功能 | 此工作不符合作業中後續工作的任何需求。 |
命令限制 | 任何 |
Settable 變數 | 任何 |
代理程式版本 | 2.120.0 或更新 |
工作類別 | 包 |