我在应用程序中克隆git仓库时遇到了问题。
KEY_FILE = "/opt/app/.ssh/id_rsa"
def read_git_branch(config_id, branch):
config = RepoConfig.objects.get(id=config_id)
save_rsa_key(Credentials.objects.get(id=1).key)
git_ssh_identity_file = os.path.expanduser(KEY_FILE)
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
with tempfile.TemporaryDirectory() as tmpdir:
repo = Repo.clone_from(config.url, tmpdir, branch=branch)
branch_obj, _ = Branch.objects.get_or_create(name=branch)
....
def save_rsa_key(key):
if not os.path.exists(os.path.dirname(KEY_FILE)):
try:
os.makedirs(os.path.dirname(KEY_FILE))
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
with open(KEY_FILE, 'w') as id_rsa:
id_rsa.write(key)
os.chmod(KEY_FILE, 0o600)
期望的结果是将仓库克隆到临时目录,做一些操作然后删除所有文件。但实际得到的结果是:
当我尝试使用上面代码创建的密钥文件直接从计算机连接到相同的仓库时出现问题。[DEBUG/ForkPoolWorker-2] AutoInterrupt wait stderr: b'Host key verification failed.\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n'
git.exc.GitCommandError: Cmd('git') failed due to: exit code(128) cmdline: git clone --branch=master -v git@gitlab.foo:bar/project.git /tmp/tmpi_w2xhgt stderr: 'Host key verification failed.
ssh-agent bash -c 'ssh-add /opt/app/.ssh/id_rsa; git clone git@gitlab.foo:bar/project.git'
存储库已成功克隆+主机已添加到known_hosts
。
完成此操作后,我的代码按预期工作...
这一定与known_hosts
有关。 有人遇到过类似的问题吗?
感谢您的帮助。