Skip to content

Instantly share code, notes, and snippets.

@gyscos
Created May 24, 2017 20:03
Show Gist options
  • Save gyscos/41e827739c72eab371886c754e3b6f21 to your computer and use it in GitHub Desktop.
Save gyscos/41e827739c72eab371886c754e3b6f21 to your computer and use it in GitHub Desktop.
# Boostrap script to create and upload a ssh key to gitlab.
# Update with ./update_bootstrap.sh
function main() {
set -e
KEY="$1"
TARGET="$2"
EMAIL="$3"
API="$TARGET/api/v4"
AUTH="private_token=$KEY"
jq -h > /dev/null 2>&1 || { echo "The jq command was not found. Please install it and try again."; exit 1; }
ID=$(curl -s "$API/users?$AUTH" | jq '.[] | select(.email == "'$EMAIL'") | .id')
if [ -z $ID ]
then
echo "User $EMAIL not found."
exit 1
fi
SSH_FILE="$HOME/.ssh/gitlab"
if [ ! -f "$SSH_FILE" ]
then
ssh-keygen -q -f "$SSH_FILE" -t ed25519 -N ''
fi
if ! grep gitlab ~/.ssh/config
then
echo "Host git.diffbot.com
Hostname git.diffbot.com
Port 10022
User git
IdentityFile $SSH_FILE" >> ~/.ssh/config
fi
TITLE="gitlab $USER@$(hostname)"
CMD="curl -s -X POST --data-urlencode 'title=$TITLE' --data-urlencode 'key=$(cat $SSH_FILE.pub)' '$API/users/$ID/keys?$AUTH'"
# echo $CMD
RESPONSE=$(bash -c "$CMD")
KEY_ID=$(echo $RESPONSE | jq .id)
if [ -z $KEY_ID ] || [ $KEY_ID = null ]
then
echo "Error creating key":
echo $RESPONSE | jq .
exit 1
fi
echo "Key created: $KEY_ID"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment