Node.JS exec git命令错误:权限被拒绝(publickey) [英] Node.JS exec git command error: Permission denied (publickey)

查看:456
本文介绍了Node.JS exec git命令错误:权限被拒绝(publickey)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何从 node.js 运行 git push passphrase

我正在尝试构建一个小模块,我需要运行 git push node.js 到远程 repo ,但是当我使用<$ c时出现错误$ c> node.js exec 但不是来自终端。

I'm trying to build a small module where I need to run git push from node.js to a remote repo, but I'm getting an error when I with from node.js exec but not from the terminal.

我的代码。

./ 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我收到以下错误。

and when I run ts-node ./index.ts I get the following error.

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 时终端我得到了密码的提示并且它有效。

But when I run git push from the terminal I get prompt with the passphrase and it works.

如何解决这个问题,是否有办法运行 git push 使用密码短语使用 node.js

Any idea on how to solve this issue, is there a way to run git push with passphrase using node.js?

请记住我会很乐意修复没有任何外部库。

bear in mind that I will love to fix this without any external libs.

提前致谢。

推荐答案

as 此处描述,检查相同的程序是否有效:

As described here, check if the same program works when:

  • ssh-agent is launched
  • your key is ssh-add'ed

不仅提示不应该对密码的更长查询(现在由代理缓存),但您的脚本也可能从该缓存中受益。

Not only the prompt should no longer query for your passphrase (now cached by the agent), but your script might benefit from that cache as well.

这篇关于Node.JS exec git命令错误:权限被拒绝(publickey)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆