无法通过在Node.js中添加ssh-key来克隆克隆

# Write the SSH-KEY to the disk fs.writeFile "/cgrepos/.ssh/#{repo.id}.pub", repo.public_key, (err) -> throw err if err fs.writeFile "/cgrepos/.ssh/#{repo.id}", repo.private_key, (err) -> throw err if err exec "chmod 400 /cgrepos/.ssh/#{repo.id} && eval `ssh-agent -s` && ssh-add /cgrepos/.ssh/#{repo.id}", (error) -> throw error if error # First, delete the git repo on the hard drive, if it exists exec "rm -rf #{git_location}", options, (error) -> throw error if error # Second, clone the repo into the location console.log "Cloning repo #{repo.id}: #{repo.repo_name} into #{git_location}. This could take a minute" exec "git clone #{repo.url} #{git_location}", options, (error) -> throw error if error 

我试图在节点(使用coffee的那些真棒)。 但由于某种原因,当它运行时,它给了我一个错误: Error: Command failed: conq: repository access denied. deployment key is not associated with the requested repository. Error: Command failed: conq: repository access denied. deployment key is not associated with the requested repository.

不知道我在做什么错。 如果我直接从命令行运行这些命令,一切似乎工作正常。 有任何想法吗?

当你尝试从node.js执行git clone进程时,它运行在不同的环境中。

当你在受保护的(在ssh协议上)存储库上使用git clone时, ssh-agent首先尝试用提供的公钥来validation你。 由于exec为每个调用使用不同的运行时环境,即使您明确地添加了您的私钥,由于不同的运行时环境,也不会工作。

在ssh中进行身份validation时,git clone会查找SSH_AUTH_SOCK 。 通常这个envvariables有你的密码钥匙环服务的path,例如(gnome-keyring或kde-wallet)。

试试这个先检查一下。

 env | grep -i ssh 

它应该列出SSH_AGENT_PID和SSH_AUTH_SOCK。 问题是运行git clone这些环境variables没有设置。 所以你可以设置它们(只要SSH_AUTH_SOCK就足够了)作为exec函数调用的选项。 在这里看看如何将env密钥对传递给exec。

 var exec = require('child_process').exec, child; child = exec('git clone cloneurl', { cwd: cwdhere, // working dir path for git clone env: { envVar1: envVarValue1, SSH_AUTH_SOCK: socketPathHere } }, callback); 

如果这不起作用,请尝试在exec函数中执行ssh -vvv user@git-repo-host 。 看到这个过程的输出,你会发现错误。

如果错误说debug1: No more authentication methods to try. Permission denied (publickey). debug1: No more authentication methods to try. Permission denied (publickey). ,然后像这样添加一个主机别名到$ HOME / .ssh / config文件。

 Host hostalias Hostname git-repo-host IdentityFile ~/.ssh/your_private_key_path 

这将使用提供的私钥到指定主机的所有authentication请求。 在这个选项中,你也可以改变你的origin's URL来使用上面configuration的hostalias。 reporoot/.git/config文件将如下所示。

 [remote "origin"] url = user@hostalias:repo.git