Skip to content

Instantly share code, notes, and snippets.

@medvedev
Created May 29, 2024 10:14
Show Gist options
  • Save medvedev/661cae2f5b9cae7a0e0af9ae6074a770 to your computer and use it in GitHub Desktop.
Save medvedev/661cae2f5b9cae7a0e0af9ae6074a770 to your computer and use it in GitHub Desktop.
Clone private GitHub repository from within a Kaggle notebook
import os
from pathlib import Path
from kaggle_secrets import UserSecretsClient
github_username='<USERNAME>'
github_repository='<REPOSITORY>'
# Note: Key should be already configured at Github side and
# added as a secret, excluding lines "BEGIN .. KEY" and "END .. KEY" to your notebook
private_key_content = UserSecretsClient().get_secret("GITHUB_SSH_KEY")
key_lines = [
"-----BEGIN OPENSSH PRIVATE KEY-----",
*private_key_content.strip().split(' '),
"-----END OPENSSH PRIVATE KEY-----\n"
]
formatted_key = "\n".join(key_lines)
private_key_path = Path('/root/.ssh/id_rsa')
private_key_path.parent.mkdir(parents=True, exist_ok=True)
private_key_path.write_text(formatted_key)
private_key_path.chmod(0o600)
!ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
!GIT_SSH_COMMAND="ssh -i {private_key_path}" git clone git@github.com:{github_username}/{github_repository}.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment