NPM 註冊表驗證
私有 NPM 註冊表可讓您的單一儲存庫發佈 NPM 套件以供內部使用。它的運作方式與公用 https://www.npmjs.com/ 註冊表相同,只是存取私有註冊表需要授權。每位使用者都需要取得一個存取權杖,該權杖通常會儲存在其電腦上的 ~/.npmrc 檔案中。
大多數大型單一儲存庫最終都需要私有 NPM 註冊表。它適用於
- 在團隊之間私下分享程式碼
- 代理存取公用註冊表,以提高可靠性、稽核套件使用情況和應用安全性篩選
- 透過安裝預先建置的工具套件來加速 CI 作業,而不是在呼叫工具之前執行
rush install && rush build
- 在將套件發佈到公用 NPM 註冊表之前測試安裝行為
- 發佈第三方套件的包裝函式或臨時分支
(與 GitHub URL 相依性相比,NPM 套件提供正確的 SemVer 版本控制和更好的快取語意。)
一些熱門的提供者包括
為了測試目的,Verdaccio 是一個輕量級的 Node.js 伺服器,可以在 https://#
上執行,並實作具有代理功能的完整私有註冊表。
註冊表對應
您的私有註冊表的對應指定在 單一儲存庫 .npmrc 檔案中。
以下是一個範例組態,可從私有註冊表安裝公司套件,但從公用註冊表取得所有其他套件。公司套件由其 @example
NPM 範圍識別。
common/config/rush/.npmrc
# Map your company's NPM scope ("@example") to the private registry URL:
@example:registry=https://my-registry.example.com/npm-private/
# Otherwise, all other packages come from the public NPM registry:
registry=https://registry.npmjs.org/
always-auth=false
# Here we specify how the package manager should authenticate to the private registry.
# For security reasons, CI jobs should obtain their tokens from environment variables.
# The exact syntax depends on your registry provider. If a line references an environment
# variable that is undefined, Rush will ignore that line. This avoids producing an invalid
# string that might interfere with a developer who obtains their credentials from ~/.npmrc.
//my-registry.example.com/npm-private/:_password=${MY_CI_TOKEN}
//my-registry.example.com/npm-private/:username=${MY_CI_USER}
//my-registry.example.com/npm-private/:always-auth=true
更常見的是,您的私有註冊表將充當快取代理,以便它也可以提供來自公用 NPM 註冊表的套件。在這種情況下,不需要對應 NPM 範圍。您的設定可能如下所示
common/config/rush/.npmrc
# Map everything to the private registry URL
registry=https://my-registry.example.com/npm-private/
always-auth=true
# Here we specify how the package manager should authenticate to the private registry.
# For security reasons, CI jobs should obtain their tokens from environment variables.
# The exact syntax depends on your registry provider. If a line references an environment
# variable that is undefined, Rush will ignore that line. This avoids producing an invalid
# string that might interfere with a developer who obtains their credentials from ~/.npmrc.
//my-registry.example.com/npm-private/:_password=${MY_CI_TOKEN}
//my-registry.example.com/npm-private/:username=${MY_CI_USER}
有關 .npmrc 設定的查閱優先順序的詳細資訊,請參閱 .npmrc 頁面。
使用「rush setup」提示輸入認證
Rush 最近推出了一個實驗性功能,其中 rush install
可以偵測到使用者的註冊表認證遺失或過期。如果是這樣,系統會要求他們執行 rush setup
,這會引導使用者完成取得權杖的流程,然後更新他們的 ~/.npmrc 檔案。新設定將會與該檔案的任何現有內容智慧合併。
一個 rush setup
互動範例如下
NPM credentials are missing or expired
==> Fix this problem now? (y/N) Yes
This monorepo consumes packages from an Artifactory private NPM registry.
==> Do you already have an Artifactory user account? (y/n) Yes
Please open this URL in your web browser:
https://my-company.jfrog.io/
Your user name appears in the upper-right corner of the JFrog website.
==> What is your Artifactory user name? example-user
Click "Edit Profile" on the JFrog website. Click the "Generate API Key" button if you haven't already done so
previously.
==> What is your Artifactory API key? ***************
Fetching an NPM token from the Artifactory service...
Adding Artifactory token to: /home/example-user/.npmrc
初始實作僅支援 JFrog Artifactory 服務。未來將會實作其他服務。
若要使用此功能,只需在您的 artifactory.json 設定檔中指定 "registryUrl"
欄位,並設定 "enabled": true
即可。檔案範本包含可用於自訂對話框的其他選用設定文件。
另請參閱
- rush setup
- artifactory.json 設定檔
- .npmrc 設定檔
- .npmrc-publish 設定檔