5

I am using Jenkins to manage continuous integration. One of my jobs contains the following tasks :

  • Cloning a repository from BitBucket through the Git plugin, using credential "bitbucket access" (private key with passphrase) => works fine !
  • Executing batch command in the repository : 'npm install' => fails !

In the package.json file, there are some common nodeJS modules like colors, fs.extra, etc. Those are correctly installed if they are the only ones.

But there is also a module hosted on BitBucket as private :

"dependencies": {
    "module": "git+ssh://[email protected]/<team>/module.git"
}

That module fails to be installed during the 'npm install' command (see Jenkins log below).

The job is run on a Windows 7 slave. If I try to make 'npm install' on a session "mySession" on the slave, it asks me for the passphrase corresponding to the private key located at C:\Users\mySession\.ssh\id_rsa (which has BitBucket access too, but which is specific to the session "mySession", and so different from the "bitbucket access" credential). Then 'npm install' ends normally by installing the module.

I tried to use the SSH Agent plugin, with the credential "bitbucket access" : it starts correctly, but it seems not to be used afterward, as the permission is denied.

Do you have any ideas how to resolve this problem and make the 'npm install' work correctly ?

Thank you for your aswers !

Jenkins log :

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 [email protected] No description
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] 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://[email protected]/<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://[email protected]/<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://[email protected]/<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://[email protected]/<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://[email protected]/<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://[email protected]/<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://[email protected]/<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://[email protected]/<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.

May be usefull :

  • Jenkins version : 1.628
  • Credentials plugin : 2.1.3
  • Git plugin : 2.3.5
  • Git client plugin : 1.19.6
  • Bitbucket plugin : 1.1.2
  • SSH Credentials plugin : 1.12
  • SSH Agent plugin : 1.13

4 Answers 4

3

I had a similar problem in a Jenkinsfile where npm would not access a private git repo even though I could do it as the same user from the commandline. It seemed as if the .ssh/config was being ignored. I was able to work around this with the following snippet in my Jenkinsfile:

sh "eval \$(ssh-agent); ssh-add /home/tomcat/.ssh/mysshkey; npm install"
Sign up to request clarification or add additional context in comments.

1 Comment

This helped me with the issue, some more details: Edit the file ~/.ssh/config With: Host github.com HostName github.com User git IdentityFile ~/.ssh/<rsa-key-name>
0

You need to generate an OAuth token in Bitbucket, look at the create consumer section. Then you can use it with https like this:

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

6 Comments

Thank you for the answers ! I generated an OAuth token in Bitbucket like you said. Then, if I run "git clone h t t p s://x-token-auth:<token>@bitbucket.org/user/module.git", it works and clones the repository, but if I run "npm install", it fails with error : fatal: Unable to find remote helper for 'git+https'. Do yo have any idea about that ?
@Benoit only thing I could find about that was this github.com/npm/npm/issues/9053 but it's quite old, must have been fixed already. Could try updating your npm to v3.7.0?
@Benoit here are the docs that describe that feature docs.npmjs.com/files/package.json#git-urls-as-dependencies and the only refs to something in the resemblance of that in the Changelogs github.com/npm/npm/blob/master/CHANGELOG.md is after v3.7.0 so try 3.7+ versions of npm
I updated npm to the latest version (v3.9.5) but the problem still exists. However, I think this solution doesn't suit me, because it implies that the token is written in clear text in the package.json, so that anyone who ever had access to the file (a trainee e.g.) can download the module later :/
But still... if submodule checkout is not working... doesn't help anyway...
|
0

it's a common issue of npm pipleline.It's failed due to the git ssh private key is not stored on the jenkins host,you need read the private key from jenkins credential,with version 2.30+ git,git support use GIT_SSH_COMMAND to set private key,the code in jenkinsfile as bellow

 stage('install') {
                withCredentials([sshUserPrivateKey(credentialsId: "keyId", keyFileVariable: 'key')]) {
                // from git 2.30+,git support use GIT_SSH_COMMAND to set private key 
                sh 'GIT_SSH_COMMAND="ssh -i $key" npm install'
                }
     }

Comments

0

The formal why to do this is to use the sshagent plugin to inject ssh-credential on-demand.

node {
  sshagent(['your-ssh-credential-id']) {
    sh 'npm install'
  }
}

Actually this a general method when you use other ssh-based tool like rsync, scp, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.