问题:如何使用node.js
从CC_2运行git push
。
我正在尝试构建一个小模块,其中我需要将git push
从node.js
运行到远程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(/[nr]/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
使用Passphrase运行git push
?
请记住,我很乐意在没有任何外部Libs的情况下解决此问题。
预先感谢。
如下所述,检查同一程序是否有效时:
- SSH-Agent启动
- 您的钥匙是ssh-add'ed
不仅提示不再应该对您的密码(现在由代理商封存(查询,而且您的脚本也可能受益于该缓存。
如果将键加载到SSH-Agent进程中,则可能需要将env: process.env
添加到exec()
选项。有一些环境变量SSH代理导出,其他过程用于查找SSH-Encent访问其键的SSH代理。