[英]Install npm module from gitlab private repository
我們將GitLab用於我們的私人項目。 有一些來自 github 的分叉庫,我們想將它們安裝為 npm 模塊。 直接從 npm 安裝該模塊是可以的,例如:
npm install git://github.com/FredyC/grunt-stylus-sprite.git
...也能正常工作,但對 GitLab 做同樣的事情,只是改變域就會出現這個錯誤。
npm WARN `git config --get remote.origin.url` returned wrong result (git://git.domain.com/library/grunt-stylus-sprite.git)
npm ERR! git clone git://git.domain.com/library/grunt-stylus-sprite.git Cloning into bare repository 'D:\users\Fredy\AppData\Roaming\npm-cache\_git-remotes\git-git-domain-com-library-grunt-stylus-sprite-git-6f33bc59'...
npm ERR! git clone git://git.domain.com/library/grunt-stylus-sprite.git fatal:unable to connect to git.domain.com:
npm ERR! git clone git://git.domain.com/library/grunt-stylus-sprite.git git.domain.com[0: 77.93.195.214]: errno=No error
npm ERR! Error: Command failed: Cloning into bare repository 'D:\users\Fredy\App
Data\Roaming\npm-cache\_git-remotes\git-git-domain-com-library-grunt-stylus-spr
ite-git-6f33bc59'...
npm ERR! fatal: unable to connect to git.domain.com:
npm ERR! git.domain.com[0: xx.xx.xx.xx]: errno=No error
從 GitLab 的 Web 界面,我有這個 URL git@git.domain.com:library/grunt-stylus-sprite.git
。 針對npm install
運行它會嘗試從 npm 注冊表安裝git
模塊。
但是使用 URL: git+ssh@git.domain.com:library/grunt-stylus-sprite.git
突然問我密碼。 我的 SSH 密鑰不包含密碼,所以我認為它無法加載該密鑰。 也許我錯過了一些配置? 密鑰位於我的主目錄中的標准位置,名稱為"id_rsa"
。
我在 Windows 7 x64 上。
更新
從 NPM v3 開始,內置了對 GitLab 和其他來源(BitBucket、Gist)的支持,您可以從中安裝軟件包。 不幸的是,它只適用於公共的,所以它與此並不完全相關,但有些人可能會發現它很有用。
npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]
查看文檔: https : //docs.npmjs.com/cli/install
您有以下連接到私有 gitlab 存儲庫的方法
使用 SSH
git+ssh://git@git.mydomain.com:Username/Repository#{branch|tag}
git+ssh://git@git.mydomain.com/Username/Repository#{branch|tag}
使用 HTTPS
git+https://git@git.mydomain.com/Username/Repository#{branch|tag}
使用 HTTPS 和部署令牌
git+https://<token-name>:<token>@gitlab.com/Username/Repository#{branch|tag}
更新
正如@felix 在評論中提到的(感謝@felix),使用deploy token
與讀取gitlab
上的私有注冊表更相關。 這種方式是令牌被泄露,攻擊者只能讀取該存儲庫而無法進行更改。
GitLab
帳戶。Expand
部署令牌部分。read_repository
舊答案
轉到User Settings > Access Tokens
並創建一個具有read_registry
權限的新access token
。
復制生成的token
,我們的package.json
文件需要它。
現在在package.json
添加如下dependency
:
"my-module": "git+https://Xaqron:token@gitlab.com/Xaqron/my-module"
更換Xaqron
與您的用戶名和token
與生成的令牌。 您可以通過#{branch|tag}
在 url 的末尾指定branch
和tag
。
注意:由於訪問令牌位於package.json
任何有權訪問此項目的人都可以讀取存儲庫,因此我假設您的項目本身是私有的。
而不是git://
,使用git+ssh://
並且 npm 應該做正確的事情。
雖然這個問題是關於 Gitlab 的,但這個問題在 google 搜索中排名很好,所以這里有一些關於如何解決我在 Github 上遇到的類似問題的更多信息。
對我來說,僅更改 url 並沒有使其工作。 以下是我必須采取的步驟來解決這個問題:
git+ssh://git@github.com:owner/repo.git#master
~/.ssh/config
如果文件不存在則創建文件)以強制使用 DeployKey 而不是默認的 ssh 密鑰之后 npm install 就可以正常工作了。 所有其他選項和解決方案都導致 npm install 中斷
對於偶然發現此問題的其他任何人,我根本無法通過 HTTPS 使其工作 - 似乎它不支持到 repo 的直接鏈接(例如https://git.domain.com/user/somerepo.git
),也不支持.tar
、 .tar.bz
或.zip
存檔版本。
它似乎只適用於.tar.gz
存檔。
完整示例(帶有標記版本):
https://git.domain.com/user/somerepo/repository/archive.tar.gz?ref=v1.2.3
對於私人 gitlab.com 存儲庫,其他任何答案都不適用於我...
然而,這有效:
npm i -S git+ssh://git@gitlab.com:<org>/<project>.git
它只是來自項目頁面的“克隆”輸入字段的 git ssh clone url,在它的前面添加了git+ssh://
。
對我來說,將 package.json 設置為如下有效。
"dependencies": {
"<module-name>": "git+http://<username>:<token>@url.git",
}
令牌是從您的“配置文件設置 - 訪問令牌”中獲取的。
據我所知,您出錯的地方是git://
協議。 GitLab 僅支持 HTTP(s) 和 SSH 進行克隆。 因此,您必須使用其中一種方法而不是 git 協議。
Gitlab 現在有一個包注冊表,可以在其中構建、部署和托管 npm 包。 使用私有存儲庫,可以提供對存儲庫內容和包的細粒度訪問控制。
通過在package.json
旁邊添加.npmrc
文件,可以從私有 Gitlab 存儲庫安裝 NPM 包。 更多信息在這里。 盡管在同一代碼庫中為不同的存儲庫使用多個部署令牌時會變得復雜。
使用 Gitlab 可以直接使用HTTPS訪問包.tgz
文件並部署令牌。 只需像這樣添加項目依賴項:
"@foo/bar": "https://<username>:<token>@gitlab.com/api/v4/projects/<project-id>/packages/npm/@foo/bar/-/@foo/bar-1.0.0.tgz"
@foo/bar 在 URL 中出現兩次。 @foo 是項目范圍,bar 是模塊名稱,1.0.0 是模塊名稱。 project-id (8位數字)是Gitlab的項目ID,在項目頁面名稱下可以看到。 甚至可以從模塊名稱(但不是鏈接)中省略 @foo。
使用具有相同范圍和不同部署令牌的多個模塊可以確保管理私有存儲庫的安全。 此外,部署令牌可能只能訪問package registry
,這意味着最終用戶將無法從存儲庫訪問完整的源代碼。
此解決方案僅適用於yarn
,而不適用於npm
,但是...
如果您需要在既沒有git
也沒有ssh
可執行文件的環境中安裝依賴項(例如在 docker 中),您可以使用repo 訪問密鑰和 read API 訪問創建一個 tarball 鏈接,然后像這樣引用它:
包.json
{
"dependencies": {
"your-lib-name": "https://gitlab.com/api/v4/projects/1234567/repository/archive?private_token=ABC_123asdfg&sha=abcdef0123456789abcdef0123456789abcdef01",
在哪里:
1234567
是您的項目 ID,可以在它的 gitlab項目概覽頁面上看到。ABC_123asdfg
是您創建的具有 API 讀取權限的項目 Access Keyabcdef0123456789abcdef0123456789abcdef01
是提交哈希
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.