Created
January 20, 2026 21:52
-
-
Save cameroncking/fa4e4c502e39db2b7fae70ce29a2ada5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| # Parse repository URL and extract username/reponame | |
| parse_repo_info() { | |
| local url="$1" | |
| # Handle git@github.com:username/repo.git format | |
| if [[ "$url" =~ ^git@github\.com:(.*)\.git$ ]]; then | |
| echo "${BASH_REMATCH[1]}" | |
| # Handle https://github.com/username/repo.git format | |
| elif [[ "$url" =~ ^https://github\.com/(.*)\.git$ ]]; then | |
| echo "${BASH_REMATCH[1]}" | |
| else | |
| echo "Error: Unsupported URL format. Expected: git@github.com:user/repo.git or https://github.com/user/repo.git" >&2 | |
| exit 1 | |
| fi | |
| } | |
| # Generate SSH key for repository | |
| generate_key() { | |
| local repo_path="$1" | |
| local key_name="${repo_path//\//_}_deploy_key" | |
| local key_path="$HOME/.ssh/$key_name" | |
| if [[ -f "$key_path" ]]; then | |
| echo "Warning: SSH key already exists at $key_path" | |
| return 0 | |
| fi | |
| echo "Generating SSH key for $repo_path..." | |
| ssh-keygen -t rsa -b 4096 -f "$key_path" -C "$repo_path deploy key" -N "" | |
| echo "SSH key generated: $key_path" | |
| } | |
| # Add SSH config entry | |
| add_ssh_config() { | |
| local repo_path="$1" | |
| local key_name="${repo_path//\//_}_deploy_key" | |
| local host_alias="github.com-${repo_path//\//_}" | |
| local config_entry=" | |
| Host $host_alias | |
| HostName github.com | |
| User git | |
| IdentityFile ~/.ssh/$key_name | |
| IdentitiesOnly yes" | |
| echo "Adding SSH config entry for $host_alias..." | |
| echo "$config_entry" >> "$HOME/.ssh/config" | |
| echo "SSH config updated." | |
| } | |
| # Display public key and instructions | |
| show_instructions() { | |
| local repo_path="$1" | |
| local key_name="${repo_path//\//_}_deploy_key" | |
| local public_key_path="$HOME/.ssh/$key_name.pub" | |
| local host_alias="github.com-${repo_path//\//_}" | |
| echo "" | |
| echo "=== SETUP INSTRUCTIONS ===" | |
| echo "1. Add deploy key at: https://github.com/$repo_path/settings/keys" | |
| echo " Copy the public key below and paste it in the deploy key form:" | |
| echo "" | |
| cat "$public_key_path" | |
| echo "" | |
| echo "2. Clone the repository using this command:" | |
| echo " git clone $host_alias:$repo_path.git" | |
| echo "" | |
| echo "3. If you already have the repository cloned, update its remote URL:" | |
| echo " cd /path/to/repo" | |
| echo " git remote set-url origin $host_alias:$repo_path.git" | |
| echo "" | |
| } | |
| main() { | |
| if [[ $# -ne 1 ]]; then | |
| echo "Usage: $0 <repository-url>" | |
| echo "Example: $0 git@github.com:user/reponame.git" | |
| exit 1 | |
| fi | |
| local repo_url="$1" | |
| local repo_path | |
| repo_path=$(parse_repo_info "$repo_url") | |
| echo "Setting up deploy key for repository: $repo_path" | |
| generate_key "$repo_path" | |
| add_ssh_config "$repo_path" | |
| show_instructions "$repo_path" | |
| echo "Setup complete! Follow the instructions above to complete the configuration." | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment