简体   繁体   English

如何将 GitHub SSH 密钥用于 Nodejs 中的私有/公共存储库(克隆、拉取、推送和提交)

[英]How to use GitHub SSH key for private/public repositories in a Nodejs for (clone, pull, push, and commit)

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."我尝试以相同的方法使用 Nodegit,但收到一条错误消息,提示“Clone.clone,堆栈:错误:方法克隆已导致错误。” or "Github authentitation failed."或“Github 身份验证失败。”

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.'} {errno: -1, errorFunction: 'Clone.clone', stack: 'Error: Method clone has throw an error.', message: 'Method clone has throw 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);
        });
    }
}

It depends on your code, and on the type of SSH keys used.这取决于您的代码,以及所使用的 SSH 密钥的类型。

If you are using OpenSSH one, as shown in nodegit/nodegit issue 1594 , then it should work with:如果您使用的是 OpenSSH,如nodegit/nodegit issue 1594所示,那么它应该适用于:

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);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM