问题:如何使用 passphrase
从 node.js
运行 git Push
。
我正在尝试构建一个小模块,需要在其中从 node.js
运行 git push
到远程 repo
,但是当我从 node.js exec
但不是从终端使用时,我收到错误。
我的代码。
./command.ts
import * as util from 'util';
const exec = util.promisify(require('child_process').exec);
export default function command(command: string): Promise<string> {
return exec(command, {cwd: process.cwd()}).then((resp) => {
const data = resp.stdout.toString().replace(/[\n\r]/g, '');
return Promise.resolve(data);
});
}
./index.ts
import command from './command';
async function init() {
try {
await command('git add .');
await command('git commit -m "my commit" ');
conat result = await command('git push');
} catch (e) {
console.log(e);
}
}
init();
当我运行 ts-node ./index.ts
时,出现以下错误。
Error: Command failed: git push
git@hostname.org: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
但是当我从终端运行 git push
时,我会收到密码提示,并且它可以工作。
关于如何解决此问题的任何想法,有没有办法使用 node.js
运行带密码的 git Push
?
请记住,我很乐意在没有任何外部库的情况下解决这个问题。
提前致谢。
最佳答案
如described here ,检查同一程序在以下情况下是否有效:
- ssh-agent 已启动
- 您的key is ssh-add'ed
不仅提示不应再查询您的密码(现在由代理缓存),而且您的脚本也可能从该缓存中受益。
关于javascript - Node.JS执行git命令错误: Permission denied (publickey),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48941193/