如何使用SSH使用simple-git克隆存储库
我试着用这个代码克隆
const git: SimpleGit = simpleGit();
const sshUri = 'git@..........'
const localPath = '/usr/workspace';
git
 .clone(
   sshUri,
   localPath
 ).then((data) => {
    console.log('success');
 }).catch((err) => {
     console.log(err);
 });但我得到了一个例外
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.发布于 2022-03-12 03:42:47
如果您的git回购是私有的,则需要生成SSH密钥并将此密钥添加到您的GIT帐户中。
生成SSH密钥的https://docs.github.com/en/authentication/connecting-to-github-with-ssh
代码示例
    private async cloneRepo() {
        const gitSSH = 'FILL YOUR SSH GIT ADDRESS';
        const sourceDir = path.resolve(`${os.tmpdir()}/${this.projectKey}`);
        const sshKnownHosts = path.resolve(`${process.cwd()}/settings/ssh/known_hosts`)
        const sshKey = path.resolve(`${process.cwd()}/settings/ssh/id_ed25519`)
        const GIT_SSH_COMMAND = `ssh -o UserKnownHostsFile=${sshKnownHosts} -o StrictHostKeyChecking=no -i ${sshKey}`;
        console.log(sourceDir);
        const git: SimpleGit = simpleGit()
            .env('GIT_SSH_COMMAND', GIT_SSH_COMMAND)
            .clean(CleanOptions.FORCE);
        await git.clone(gitSSH, sourceDir, ['-b', 'YOUR-BRANCH-NAME', '--single-branch'])
            .then(() => console.log('finished'))
            .catch((err) => console.error('failed: ', err));
    }https://stackoverflow.com/questions/69480019
复制相似问题