简体   繁体   English

simple-git:使用 simple-git 的 SSH 身份验证示例

[英]simple-git: SSH authentication example using simple-git

How to clone a repository with SSH using simple-git ?如何使用simple-git克隆SSH的存储库?

I'm trying to clone using this code我正在尝试使用此代码进行克隆

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

But I am getting an exception但我遇到了一个例外

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

If your git repo is private, you will need to generate SSH key and add this key to your GIT account.如果您的 git 存储库是私有的,您将需要生成 SSH 密钥并将此密钥添加到您的 GIT 帐户。

Code example代码示例

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

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

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