如何在 Nodejs 中为 private/public 存储库使用 GitHub SSH 密钥(克隆、拉取、推送和提交)
How to use GitHub SSH key for private/public repositories in a Nodejs for (clone, pull, push, and commit)
我尝试在相同的方法中使用 Nodegit,但我收到一条错误消息“Clone.clone,堆栈:错误:方法克隆导致了错误。”或“Github 身份验证失败。”
我试过了,但它给我以下错误信息
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);
});
}
}
这取决于您的代码,以及所使用的 SSH 密钥类型。
如果您使用的是 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);
});
我尝试在相同的方法中使用 Nodegit,但我收到一条错误消息“Clone.clone,堆栈:错误:方法克隆导致了错误。”或“Github 身份验证失败。”
我试过了,但它给我以下错误信息
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);
});
}
}
这取决于您的代码,以及所使用的 SSH 密钥类型。
如果您使用的是 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);
});