3

Question: How can I run git push from node.js with passphrase.

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.

My code.

./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();

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.

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

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.

Thanks in advance.

diegoddox
  • 593
  • 7
  • 23
  • In case you are okay with using username and password you could easily do `git push https://username:password@myrepository.biz/file.git --all` – Jiby Jose Feb 23 '18 at 04:46

2 Answers2

0

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

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your response, it works however if the there is no ssh key the user will be prompt with the username and pass – diegoddox Feb 23 '18 at 11:03
  • @diegoddox Well yes... but in your case, there is an ssh key, right? – VonC Feb 23 '18 at 12:16
  • I do but the people that I will be handed over the module may not have, the best will be if the `exec` could prompt the `passphrase/user:pass` in case of need. – diegoddox Feb 23 '18 at 12:31
  • 1
    @diegoddox Then an https URL might be better: they can cache their own credentials. – VonC Feb 23 '18 at 12:34
0

You may need to add env: process.env to your exec() options if your key is loaded into an ssh-agent process. There are some environment variables ssh-agent exports that other processes use to find ssh-agent to access its keys.

mscdex
  • 104,356
  • 15
  • 192
  • 153