Home > OS >  How to use GitHub SSH key for private/public repositories in a Nodejs for (clone, pull, push, and co
How to use GitHub SSH key for private/public repositories in a Nodejs for (clone, pull, push, and co

Time:03-28

I tried using Nodegit in the same method, but I got an error message saying "Clone.clone, stack: Error: Method clone has caused an error." or "Github authentitation failed."

I have tried it but it gives me below error message

Error: Method clone has thrown an error. {errno: -1, errorFunction: 'Clone.clone', stack: 'Error: Method clone has thrown an error.', message: 'Method clone has thrown an error.'}

class GitClient {
    constructor(realname, email, token, username, repoName, branch, local) {
        this.config = {
            branch,
            remote: "SSH URL",
            local,
            username,
            realname,
            email,
            token
        };
        this.cloneOpts = {
            callbacks: {
                certificateCheck: () => { return 0; },
                credentials: (url, username) => {
                    return NodeGit.Cred.sshKeyNew(
                        username,
                        path.join(this.config.local, '.ssh/id_rsa.pub'),
                        path.join(this.config.local, '.ssh/id_rsa'),
                        ''
                    );
                    // return NodeGit.Cred.sshKeyFromAgent(username);
                }
            }
        };
        this.cloneOpts.fetchOpts = { callbacks: this.cloneOpts.callbacks };
    }

    async clone(options) {
        this.cloneOpts.checkoutBranch = options.branch;
        return NodeGit.Clone(options.remote, options.local, this.cloneOpts).then((data) => {
            console.log(data)
            return data;
        }).catch(err => {
            console.log(err);
        });
    }
}

CodePudding user response:

It depends on your code, and on the type of SSH keys used.

If you are using OpenSSH one, as shown in nodegit/nodegit issue 1594, then it should work with:

const Git = require('nodegit');

const cloneURL = "git@URL:PATH_TO_GIT.git";
var local_publickey = local("/ssh/ssh-public-manual.pub");
var local_privatekey = local("/ssh/openssh-private-key");
var tmpGitPath = "./tmp";

var opts = {
    fetchOpts: {
        callbacks: {
            certificateCheck: () => 0,
            credentials: function(url, userName) {
                return Git.Cred.sshKeyNew(
                    userName,
                    local_publickey,
                    local_privatekey,
                    "SECRET_OF_PRIVATE_KEY"
                );
            }
        }
    }
};

Git.Clone.clone(cloneURL,tmpGitPath,opts)
    .then(function(repo) {
        if(repo instanceof Git.Repository) {
            console.log("Good!");
        } else {
            console.log("Bad!");
        }
        console.log('Cloning DONE');
    })
    .catch(function(err) {
        console.log('/!\\ ERROR /!\\');
        console.log(err);
    });
  • Related