目标
您好,我正在创建一个 Node.js 应用程序,以便在自动推送时更新我的代码。
问题
除了实际的 git pull 之外,一切都在其中运行。该存储库是私有(private)的,需要使用 ssh,但是当我在终端中使用相同的命令时,它可以工作。我打开了钥匙串(keychain),所以它不会要求我输入密码。有什么想法以及如何解决这个问题吗?
相关代码
const exec = require("child_process").exec;
exec('cd ' + repo + ' && git pull origin deployment', (egitpull,stdoutgitpull,stderrgitpull)=>{
if(egitpull) return console.error(`git pull exec error:${egitpull}`)
console.log(`git pull stdout: ${stdoutgitpull}`);
console.log(`git pull stderr: ${stderrgitpull}`);
命令运行:
cd /mp/ && git pull origin deployment
子进程之外在子进程中
编辑:
从 key 中完全删除密码似乎确实可以解决问题,但出于安全原因,我更愿意将其保留在其中。
最佳答案
如果您使用 ssh-agent 作为 ssh key ,请尝试将 SSH_AUTH_SOCK
和 SSH_AGENT_PID
环境变量转发到子进程,如下所示:
exec('cd ' + repo + ' && git pull origin deployment',
{
env: {
SSH_AUTH_SOCK: process.env.SSH_AUTH_SOCK,
SSH_AGENT_PID: process.env.SSH_AGENT_PID
}
},
(egitpull,stdoutgitpull,stderrgitpull) => {
// ...
});
关于Node.js 子进程未能通过 GitHub ssh 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54565635/