从gitlab私有存储库安装npm模块
我们正在为我们的私人项目使用。github中有一些分叉库,我们希望将其安装为npm模块。直接从npm安装该模块是可以的,例如:从gitlab私有存储库安装npm模块,git,npm,gitlab,Git,Npm,Gitlab,我们正在为我们的私人项目使用。github中有一些分叉库,我们希望将其安装为npm模块。直接从npm安装该模块是可以的,例如: npm安装git://github.com/FredyC/grunt-stylus-sprite.git …也可以正常工作,但对GitLab执行同样的操作时,仅更改域就会导致此错误 npm WARN `git config --get remote.origin.url` returned wrong result (git://git.domain.com/libra
npm安装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\u rsa”
我使用的是Windows7x64
更新
自NPMV3以来,内置了对GitLab和其他源(BitBucket、Gist)的支持,您可以从这些源安装软件包。不幸的是,它只适用于公共应用程序,所以它与此并不完全相关,但有些人可能会发现它很有用
npm安装gitlab://[#]
查看文档:据我所知,git://协议出了问题。GitLab仅支持克隆的HTTP和SSH。因此,您必须使用其中一种方法,而不是git协议。而不是git://,使用
git+ssh://
,npm应该做正确的事情。对于其他偶然发现这一点的人,我根本无法通过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存储库
使用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}
git+https://:@gitlab.com/Username/Repository{branch|tag}
虽然这个问题是关于Gitlab的,但这个问题在google搜索中排名很高,所以这里有一些关于如何解决我在Github中遇到的类似问题的更多信息
对我来说,仅仅更改url并不能使其正常工作。以下是我必须采取的步骤来解决此问题:
git+ssh://git@github.com:owner/repo.git#master
- 创建部署密钥并将其添加到repo
- 编辑git config(
如果文件不存在,则创建该文件)以强制使用DeployKey而不是默认ssh密钥~/.ssh/config
在那之后,npm安装简单地工作了。所有其他选项和解决方案都是由于npm安装中断而产生的其他答案都不适用于我的私人gitlab.com repo 然而,这是可行的:
npmi-sgit+ssh://git@gitlab.com://.git
它只是项目页面“clone”输入字段中的git-ssh克隆url,前面添加了
git+ssh://
。对于我来说,设置package.json如下所示
"dependencies": {
"<module-name>": "git+http://<username>:<token>@url.git",
}
“依赖项”:{
“”:“git+http://:@url.git”,
}
令牌是从您的“配置文件设置-访问令牌”获取的。更新 正如@felix在评论中提到的(谢谢@felix),使用
deploy token
与读取gitlab
上的私有注册表更相关。这样一来,令牌就被破坏了,攻击者只能读取该存储库,无法进行更改
GitLab
帐户展开
读取注册表
权限创建一个新的访问令牌
复制生成的token
,我们需要它作为我们的package.json
文件
现在在package.json
中添加依赖项
,如下所示:
"my-module": "git+https://Xaqron:token@gitlab.com/Xaqron/my-module"
用您的用户名替换Xaqron
,用生成的令牌替换token
。您可以通过{branch | tag}
在url末尾指定branch
和tag
注意:由于访问令牌位于
包.json
中,因此任何有权访问此项目的人都可以读取存储库,因此我假设您的项目本身是私有的。Gitlab现在有一个包注册表,可以在其中构建、部署和托管npm包。使用私有存储库,可以提供良好的性能-对存储库内容和包的粒度访问控制
NPM包可以通过在package.json
旁边添加一个.npmrc
文件从私有Gitlab存储库安装。更多信息。
尽管在同一代码库中为不同的存储库使用多个部署令牌会变得复杂
使用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”:“https://:@gitlab.com/api/v4/projects//packages/npm/@foo/bar/-/@foo/bar-1.0.0.tgz”
@foo/bar在URL中出现两次。
@foo是项目范围,bar是模块名,1.0.0是模块名