npmAuthenticate@0 - npm 驗證工作執行器) v0 工作的 (

使用此工作為 npm.npmrc 組建範圍提供存放庫中檔案的認證。 這可讓 npm ,以及 npm gulp 和 Grunt 等工作執行器向私人登錄進行驗證。

Syntax

# npm authenticate (for task runners) v0
# Don't use this task if you're also using the npm task. Provides npm credentials to an .npmrc file in your repository for the scope of the build. This enables npm task runners like gulp and Grunt to authenticate with private registries.
- task: npmAuthenticate@0
  inputs:
    workingFile: # string. Required. .npmrc file to authenticate. 
    #customEndpoint: # string. Credentials for registries outside this organization/collection.
# npm Authenticate (for task runners) v0
# Don't use this task if you're also using the npm task. Provides npm credentials to an .npmrc file in your repository for the scope of the build. This enables npm task runners like Gulp and Grunt to authenticate with private registries.
- task: npmAuthenticate@0
  inputs:
    #workingFile: # string. .npmrc file to authenticate. 
    #customEndpoint: # string. Credentials for registries outside this account/collection.

輸入

workingFile - 要驗證的 .npmrc 檔案
string. 必要。

指定您要使用之登錄的 .npmrc 檔案路徑。 選取檔案,而非資料夾,例如 /packages/mypackage.npmrc


workingFile - 要驗證的 .npmrc 檔案
string.

指定您要使用之登錄的 .npmrc 檔案路徑。 選取檔案,而非資料夾,例如 /packages/mypackage.npmrc


customEndpoint - 此組織/集合外部登錄的認證
string.

此組織或集合外部登錄的 npm 服務連線 名稱逗號分隔清單。 指定的 .npmrc 檔案必須包含對應至服務連線的登錄專案。 如果您只需要此組織或集合中的登錄,請將此保留空白。 組建的認證會自動使用。


customEndpoint - 此帳戶/集合外部登錄的認證
string.

此組織或集合外部登錄的 npm 服務連線 名稱逗號分隔清單。 指定的 .npmrc 檔案必須包含對應至服務連線的登錄專案。 如果您只需要此組織或集合中的登錄,請將此保留空白。 組建的認證會自動使用。


工作控制項選項

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

輸出變數

無。

備註

使用此工作為 npm.npmrc 組建範圍提供存放庫中檔案的認證。 這可讓 npm ,以及 npm gulp 和 Grunt 等工作執行器向私人登錄進行驗證。

這項工作如何運作?

此工作會搜尋指定的 .npmrc 檔案中是否有登錄專案,然後將探索到的登錄的驗證詳細資料附加至檔案結尾。 針對目前組織/集合中的所有登錄,會使用組建的認證。 對於不同組織或由協力廠商裝載的登錄,登錄 URI 會與輸入所 customEndpoint 指定的npm 服務連線URI 進行比較,並使用對應的認證。 檔案 .npmrc 會在管線執行結束時還原成其原始狀態。

我管線中的何時應該執行這項工作?

此工作必須先執行,才能使用 npmnpm 工作執行器,將套件安裝或推送至已驗證的 npm 存放庫,例如 Azure Artifacts。 沒有其他排序需求。

我有多個 npm 專案。 我需要針對每個 .npmrc 檔案執行這項工作嗎?

這項工作一次只會將驗證詳細資料新增至一個 .npmrc 檔案。 如果您需要多個 .npmrc 檔案的驗證,您可以針對每個檔案執行一次工作 .npmrc 。 或者,請考慮建立一個 .npmrc 檔案,以指定專案所使用的所有登錄、在此檔案上 .npmrc 執行 npmAuthenticate ,然後設定環境變數,將此檔案指定 .npmrc 為每個使用者組態檔的 npm。

- task: npmAuthenticate@0
  inputs:
    workingFile: $(agent.tempdirectory)/.npmrc
- script: echo ##vso[task.setvariable variable=NPM_CONFIG_USERCONFIG]$(agent.tempdirectory)/.npmrc
- script: npm ci
  workingDirectory: project1
- script: npm ci
  workingDirectory: project2

我的代理程式位於 Web Proxy 後方。 是否會 npmAuthenticate 設定 npm/gulp/Grunt 為使用我的 Proxy?

答案是不可能。 雖然此工作本身會在 Web Proxy 後方運作, 但代理程式已設定為使用,但不會設定 npmnpm 工作執行器使用 Proxy。

若要這樣做,您可以:

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

  • 使用npm組態集手動將 Proxy 設定新增至npm 組態,或藉由設定前面加上 的 NPM_CONFIG_環境變數

    注意
    npm 工作執行器可能與 支援 npm 的所有 Proxy 組態方法不相容。

  • 呼叫 npm 時,請使用命令列旗標指定 Proxy。

    - script: npm ci --https-proxy $(agent.proxyurl)
    

如果您的 Proxy 需要驗證,您可能需要新增額外的建置步驟來建構已驗證的 Proxy URI。

- script: node -e "let u = url.parse(`$(agent.proxyurl)`); u.auth = `$(agent.proxyusername):$(agent.proxypassword)`; console.log(`##vso[task.setvariable variable=proxyAuthUri;issecret=true]` + url.format(u))"
- script: npm publish --https-proxy $(proxyAuthUri)

我的管線需要存取不同專案中的摘要

如果管線在裝載摘要的專案不同的專案中執行,您必須設定其他專案,以授與建置服務的讀取/寫入權限。 如需詳細資訊 ,請參閱 Azure Pipelines 中的套件許可權

範例

從組織內的登錄還原 npm 專案的套件

如果您使用的唯一 .npmrc 已驗證登錄是組織中的 Azure Artifacts 登錄,您只需要指定工作檔案 npmAuthenticate 的路徑。

.npmrc

registry=https://pkgs.dev.azure.com/{organization}/_packaging/{feed}/npm/registry/
always-auth=true

npm

- task: npmAuthenticate@0
  inputs:
    workingFile: .npmrc
- script: npm ci
# ...
- script: npm publish

還原和發佈 npm 組織外部的套件

.npmrc如果您的包含來自不同組織的 Azure Artifacts 登錄,或使用協力廠商已驗證的套件存放庫,您必須設定npm 服務連線,並在輸入中 customEndpoint 指定它們。 Azure Artifacts 組織內的登錄也會自動進行驗證。

.npmrc

registry=https://pkgs.dev.azure.com/{organization}/{project}/_packaging/{feed}/npm/registry/
@{scope}:registry=https://pkgs.dev.azure.com/{otherorganization}/_packaging/{feed}/npm/registry/
@{otherscope}:registry=https://{thirdPartyRepository}/npm/registry/
always-auth=true

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

npm

- task: npmAuthenticate@0
  inputs:
    workingFile: .npmrc
    customEndpoint: OtherOrganizationNpmConnection, ThirdPartyRepositoryNpmConnection
- script: npm ci
# ...
- script: npm publish -registry https://pkgs.dev.azure.com/{otherorganization}/_packaging/{feed}/npm/registry/

OtherOrganizationNpmConnectionThirdPartyRepositoryNpmConnection 是已設定並授權用於管線的 npm 服務連線 名稱,而且具有符合指定 .npmrc 檔案中之連線的 URL。

規格需求

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