Skip to content

Instantly share code, notes, and snippets.

@blvz
Last active August 23, 2023 23:15
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save blvz/8eeebacae11011c25fc79eff12f49ae9 to your computer and use it in GitHub Desktop.
Save blvz/8eeebacae11011c25fc79eff12f49ae9 to your computer and use it in GitHub Desktop.
Creates a deploy key and clones the repository.
#!/usr/bin/env bash
read -r -d '' usage << EOM
Usage:
gh-deploy-clone user/repo [ENVIRONMENT]
EOM
[ -z "$1" ] && echo && echo "$usage" && echo && exit 1
repo="$1"
repo_name="${repo#*/}"
env=$([ -n "$2" ] && echo "$2_" || echo)
key="github_$env$repo_name"
key_path="$HOME/.ssh/$key.rsa"
cat >> "$HOME/.ssh/config" << EOM
Host $repo_name.github.com
HostName github.com
User git
IdentityFile $key_path
IdentitiesOnly yes
EOM
ssh-keygen -f "$key_path" -N '' -t rsa -C "$key"
echo
echo
echo Add the deploy key below to
echo "https://github.com/$repo/settings/keys"
echo and press enter to clone the repo.
echo
echo
cat "$key_path.pub"
echo
echo
read -p "Press enter to continue..." </dev/tty
git clone "git@$repo_name.github.com:$repo.git"
@blvz
Copy link
Author

blvz commented Aug 26, 2017

Install:

curl https://gist.githubusercontent.com/blvz/8eeebacae11011c25fc79eff12f49ae9/raw/6f2f7f3709a0fe852d8a3a5bb125325e3ffbc7d8/gh-deploy-clone.sh > /usr/local/bin/gh-deploy-clone
chmod +x /usr/local/bin/gh-deploy-clone

@acangiano
Copy link

Nicely done! Thank you, Rafael.

@E-OssO
Copy link

E-OssO commented Jul 31, 2020

Very helpfull:+1:
I justed switch 2 lines to use host with alias and it's works

Host $repo_name.github.com

Host $repo_name $repo_name.github.com

git clone "git@$repo_name.github.com:$repo.git"

git clone "git@$repo_name:$repo.git"

😉

@ricardo
Copy link

ricardo commented Jan 24, 2021

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment