从gitlab私有存储库安装npm模块

Fre*_*dyC 80 git npm gitlab

我们正在为我们的私人项目使用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
Run Code Online (Sandbox Code Playgroud)

从GitLab的Web界面,我有这个URL git@git.domain.com:library/grunt-stylus-sprite.git.针对npm install它运行此尝试git从npm注册表安装模块.

但是使用URL:git+ssh@git.domain.com:library/grunt-stylus-sprite.git突然问我密码.我的SSH密钥不包含密码,因此我认为它无法加载该密钥.也许我错过了一些配置?密钥位于我的主目录中的标准位置,并带有名称"id_rsa".

我在Windows 7 x64上.

UPDATE

从NPM v3开始,内置支持GitLab和其他源(BitBucket,Gist),您可以从中安装软件包.不幸的是它只适用于公共的,所以它与此并不完全相关,但有些人可能会发现它很有用.

npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]

查看文档:https://docs.npmjs.com/cli/install

sid*_*uko 127

您可以使用以下方法连接到私有gitlab存储库

用SSH

git+ssh://git@git.mydomain.com:Username/Repository#{branch|tag}
git+ssh://git@git.mydomain.com/Username/Repository#{branch|tag}
Run Code Online (Sandbox Code Playgroud)

使用HTTPS

git+https://git@git.mydomain.com/Username/Repository#{branch|tag}
Run Code Online (Sandbox Code Playgroud)

使用HTTPS和部署令牌

git+https://<token-name>:<token>@gitlab.com/Username/Repository#{branch|tag}
Run Code Online (Sandbox Code Playgroud)

  • 这实际上只对我有真正的URL语法,例如`git + ssh:// git @ git.mydomain.com/Username/Repository`(注意/分离主机和用户名).这可能是纱线特定的东西,也许建议的例子确实适用于npm (4认同)
  • 部署令牌效果很好。示例中的“&lt;token-name&gt;”看起来像“gitlab+deploy-token-17034”,它不是您分配给令牌的任意名称。 (2认同)

isa*_*acs 22

而不是git://,使用git+ssh://和npm应该做正确的事情.

  • npm 创建者自己的回答 (8认同)

Xaq*_*ron 17

更新

正如评论中提到的@felix(感谢@felix)使用deploy token与阅读私人注册表更相关gitlab.这种方式是令牌被泄露,攻击者只能读取该存储库而无法进行更改.

创建部署令牌

  1. 登录您的GitLab帐户.
  2. 转到要为其创建Deploy Tokens的项目.
  3. 转到设置>存储库.
  4. 单击ExpandDeploy Tokens部分.
  5. 选择令牌的名称和可选的到期日期.
  6. 选择所需的范围.<=选择read_repository
  7. 单击"创建部署令牌".
  8. 将部署令牌保存在安全的地方.离开或刷新页面后,您将无法再次访问它.

老答案

转到User Settings > Access Tokens并创建一个新access tokenread_registry权限.

在此输入图像描述

复制生成token,我们需要它为我们的package.json文件.

在此输入图像描述

现在package.json添加dependency如下:

"my-module": "git+https://Xaqron:token@gitlab.com/Xaqron/my-module"
Run Code Online (Sandbox Code Playgroud)

替换Xaqron为您的用户名和token生成的令牌.您可以指定branchtag在URL的末尾#{branch|tag}.

注意:由于访问令牌位于package.json任何有权访问此项目的人都可以读取存储库,因此我假设您的项目本身就是私有的.

  • 另一种方法是为该特定存储库创建部署令牌。然后,它不会授予您所有存储库的读取访问权限。`“私有模块”:“ git + https:// gitlab + deploy-token-用户名:deploy-token@gitlab.com/you/project”,` (4认同)
  • @chovy:安装 `git` [参见](/sf/ask/3953157631/) (2认同)
  • 我还想指出,如果您的项目在一个组内,那么您应该定义: `"private-module": "git+https://gitlab+deploy-token-username:deploy-token@gitlab.com/ group_name/project"`,因此您必须在项目名称之前定义`group_name`,而不是`您的用户名` (2认同)

Sep*_*phy 7

虽然问题是关于Gitlab,但这个问题在谷歌搜索中排名很好,所以这里有一些关于如何解决我在Github上遇到的类似问题的更多信息.

对我来说,只更改网址并没有使它工作.以下是我必须采取的步骤来解决这个问题:

  • git+ssh://git@github.com:owner/repo.git#master
  • 创建部署密钥并将其添加到repo
  • 编辑git config(~/.ssh/config如果文件不存在则创建该文件)以强制使用DeployKey而不是默认的ssh密钥

之后,npm安装工作正常.所有其他选项和解决方案都是由npm安装破坏引起的

  • 我该如何处理`~/.ssh/config`?我仍然没有完成 npm/gitlab (2认同)

chr*_*isf 6

只是谁比谁过这个跌倒,我无法得到它的工作通过HTTPS在所有-看来它不支持直接链接到回购(如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


cho*_*ovy 6

对于私人 gitlab.com 存储库,其他任何答案都不适用于我...

然而,这有效:

npm i -S git+ssh://git@gitlab.com:<org>/<project>.git

它只是来自项目页面的“克隆”输入字段的 git ssh clone url,并git+ssh://添加到它的前面。


she*_*aoy 5

对我来说,将 package.json 设置为如下有效。

"dependencies": {
    "<module-name>": "git+http://<username>:<token>@url.git",
}
Run Code Online (Sandbox Code Playgroud)

令牌是从您的“配置文件设置 - 访问令牌”中获取的。