【问题标题】:npm install from Jenkins fails installing module hosted on bitbucket来自 Jenkins 的 npm install 无法安装托管在 bitbucket 上的模块
【发布时间】:2016-10-29 11:53:36
【问题描述】:

我正在使用 Jenkins 来管理持续集成。我的一项工作包含以下任务:

  • 通过 Git 插件从 BitBucket 克隆存储库,使用凭据“bitbucket 访问”(带密码的私钥)=> 工作正常!
  • 在存储库中执行批处理命令:'npm install' => 失败

在 package.json 文件中,有一些常见的 nodeJS 模块,如颜色、fs.extra 等。如果它们是唯一的,则正确安装。

但还有一个模块托管在 BitBucket 上作为私有:

"dependencies": {
    "module": "git+ssh://git@bitbucket.org/<team>/module.git"
}

在“npm install”命令期间无法安装该模块(请参阅下面的 Jenkins 日志)。

作业在 Windows 7 从站上运行。如果我尝试在从属设备上的会话“mySession”上进行“npm install”,它会要求我输入与位于 C:\Users\mySession\.ssh\id_rsa 的私钥相对应的密码(它也具有 BitBucket 访问权限,但它特定于会话“mySession”,因此与“bitbucket 访问”凭证不同)。然后'npm install'通过安装模块正常结束。

我尝试使用带有凭据“bitbucket 访问”的 SSH 代理插件:它可以正确启动,但之后似乎无法使用,因为权限被拒绝。

您对如何解决此问题并使“npm install”正常工作有任何想法吗?

感谢您的回答!

詹金斯日志:

Construction à distance sur MySlave in workspace C:\Jenkins\workspace
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent]   Java/tomcat-native ssh-agent
[ssh-agent] Registered BouncyCastle on the remote agent
[ssh-agent] Started.
[ssh-agent] Using credentials bitbucket access

...

npm WARN package.json deployment@1.0.0 No description
npm WARN package.json deployment@1.0.0 No repository field.
npm WARN package.json deployment@1.0.0 No README data
npm WARN addRemoteGit Error: Command failed: git -c core.longpaths=true config --get remote.origin.url
npm WARN addRemoteGit 
npm WARN addRemoteGit     at ChildProcess.exithandler (child_process.js:203:12)
npm WARN addRemoteGit     at emitTwo (events.js:87:13)
npm WARN addRemoteGit     at ChildProcess.emit (events.js:172:7)
npm WARN addRemoteGit     at maybeClose (internal/child_process.js:818:16)
npm WARN addRemoteGit     at Socket.<anonymous> (internal/child_process.js:319:11)
npm WARN addRemoteGit     at emitOne (events.js:77:13)
npm WARN addRemoteGit     at Socket.emit (events.js:169:7)
npm WARN addRemoteGit     at Pipe._onclose (net.js:469:12)
npm WARN addRemoteGit  git+ssh://git@bitbucket.org/<team>/module.git resetting remote C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0
because of error: { [Error: Command failed: git -c core.longpaths=true config --get remote.origin.url
npm WARN addRemoteGit ]
npm WARN addRemoteGit   killed: false,
npm WARN addRemoteGit   code: 1,
npm WARN addRemoteGit   signal: null,
npm WARN addRemoteGit   cmd: 'git -c core.longpaths=true config --get remote.origin.url' }
npm ERR! git clone --template=C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\_templates --mirror ssh://git@bitbucket.org/<team>/module.git 
C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0: Cloning into bare repository
'C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0'...
npm ERR! git clone --template=C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\_templates --mirror ssh://git@bitbucket.org/<team>/module.git 
C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0: Permission denied (publickey).
npm ERR! git clone --template=C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\_templates --mirror ssh://git@bitbucket.org/<team>/module.git 
C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0: fatal: Could not read from remote repository.
npm ERR! git clone --template=C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\_templates --mirror ssh://git@bitbucket.org/<team>/module.git 
C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0: 
npm ERR! git clone --template=C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\_templates --mirror ssh://git@bitbucket.org/<team>/module.git 
C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0: Please make sure you have the correct access rights
npm ERR! git clone --template=C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\_templates --mirror ssh://git@bitbucket.org/<team>/module.git 
C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0: and the repository exists.
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install"
npm ERR! node v4.2.3
npm ERR! npm  v2.14.7
npm ERR! code 128

npm ERR! Command failed: git -c core.longpaths=true clone --template=C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\_templates --mirror ssh://git@bitbucket.org/<team>/module.git 
C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0
npm ERR! Cloning into bare repository 'C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache\_git-remotes\ssh-git-bitbucket-org-<team>-module-git-2a4d53e0'...
npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR! 
npm ERR! 
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

...

[ssh-agent] Stopped.

可能有用:

  • Jenkins 版本:1.628
  • 凭据插件:2.1.3
  • Git 插件:2.3.5
  • Git 客户端插件:1.19.6
  • Bitbucket 插件:1.1.2
  • SSH 凭据插件:1.12
  • SSH 代理插件:1.13

【问题讨论】:

    标签: git jenkins ssh npm credentials


    【解决方案1】:

    我在Jenkinsfile 中遇到了类似的问题,npm 不会访问私有 git 存储库,即使我可以从命令行以同一用户的身份访问。似乎.ssh/config 被忽略了。我可以在Jenkinsfile 中使用以下 sn-p 解决此问题:

    sh "eval \$(ssh-agent); ssh-add /home/tomcat/.ssh/mysshkey; npm install"
    

    【讨论】:

    • 这帮助我解决了这个问题,更多细节:编辑文件 ~/.ssh/config 使用:主机 github.com 主机名 github.com 用户 git IdentityFile ~/.ssh/
    【解决方案2】:

    您需要在 Bitbucket 中generate an OAuth token,查看创建消费者部分。然后你可以像这样将它与 https 一起使用:

    "dependencies": {
        "module": "git+https://x-token-auth:{access_token}@bitbucket.org/user/module.git"
    }
    

    【讨论】:

    • 感谢您的回答!正如你所说,我在 Bitbucket 中生成了一个 OAuth 令牌。然后,如果我运行“git clone https://x-token-auth:@bitbucket.org/user/module.git”,它会工作并克隆存储库,但如果我运行“npm install”,它失败并出现错误:致命:无法找到“git+https”的远程助手。你对此有什么想法吗?
    • @Benoit 我能找到的唯一一件事就是这个github.com/npm/npm/issues/9053,但它已经很旧了,肯定已经修好了。可以尝试将您的 npm 更新到 v3.7.0 吗?
    • @Benoit 这里是描述该功能docs.npmjs.com/files/package.json#git-urls-as-dependencies 的文档,并且在更改日志github.com/npm/npm/blob/master/CHANGELOG.md 中唯一的类似内容是在 v3.7.0 之后,因此请尝试 3.7+ 版本的 npm
    • 我将 npm 更新到最新版本(v3.9.5),但问题仍然存在。但是,我认为这个解决方案不适合我,因为它意味着令牌是在 package.json 中以明文形式编写的,因此任何曾经访问过该文件的人(例如实习生)都可以稍后下载该模块: /
    • 但是...如果子模块结帐不起作用...无论如何也无济于事...
    猜你喜欢
    • 2015-07-30
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2019-01-17
    • 2014-09-19
    • 1970-01-01
    • 2014-07-19
    • 2019-05-09
    相关资源
    最近更新 更多