Skip to content

Instantly share code, notes, and snippets.

@scsskid
Last active April 25, 2023 18:45
Show Gist options
  • Select an option

  • Save scsskid/e66f28174b74586e22743e3c170e1f19 to your computer and use it in GitHub Desktop.

Select an option

Save scsskid/e66f28174b74586e22743e3c170e1f19 to your computer and use it in GitHub Desktop.

Revisions

  1. scsskid revised this gist Apr 25, 2023. No changes.
  2. scsskid revised this gist Apr 25, 2023. No changes.
  3. scsskid created this gist Apr 25, 2023.
    45 changes: 45 additions & 0 deletions generate-deploy-key.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/bin/sh
    # This script generates a ssh key for a single repository
    # and adds a custom configuration to the users (not global) ssh config file,
    # and outputs the public key for you to copy and paste as the repo deploy key
    # and outputs the url for you to clone the repo on the machine.
    # Github docs ref:
    # https://docs.github.com/en/developers/overview/managing-deploy-keys#using-multiple-repositories-on-one-server
    #
    # 1. Add the script to the user account of the machine. The home directory is fine.
    # 2. Make the script executable by running the following command as the user:
    # chmod u+x generateDeployKey.sh
    # 3. Run script like `./generateDeployKey.sh REPO_OWNER_NAME REPO_NAME` Note the space between owner and repo name. Example:
    # ./generateDeployKey.sh yourname hello_world
    # If you make a mistake with what you pass in, you can remove change from your ~/.ssh/config file
    # by deleting the most recent "New Key Generated on...." and deleting the related .pub and private keys


    # Check if user passed in both parameters
    if [ -z "$1" ] || [ -z "$2" ]
    then
    echo "Make sure to pass in both parameters REPO_OWNER_NAME and REPO_NAME. Example:"
    echo "./generateDeployKey.sh yourname hello_world"
    else
    REPO_OWNER_NAME=$1
    REPO_NAME=$2
    KEY_PATH=~/.ssh/id_rsa.$REPO_NAME
    echo "Generating ssh key At ${KEY_PATH}"
    ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa.${REPO_NAME}
    echo "Your ssh deploy key is:"
    PUB_KEY_PATH=$KEY_PATH".pub"
    cat $PUB_KEY_PATH
    echo ""
    # Will create config if it does not exist
    echo "Updating ~/.ssh/config"
    DATE_TIME=$(date +"%Y-%m-%d at %r")
    echo "
    # New Key Generated on $DATE_TIME
    Host github.com-$REPO_NAME
    HostName github.com
    User git
    IdentityFile $KEY_PATH" >> ~/.ssh/config
    echo ""
    echo "Here is your hostname's alias to interact with the repository using SSH:"
    echo "git clone [email protected]$REPO_NAME:$REPO_OWNER_NAME/$REPO_NAME.git"
    fi