82 lines
2.5 KiB
Bash
Executable File
82 lines
2.5 KiB
Bash
Executable File
#!/bin/bash
|
|
set -e
|
|
|
|
# Check if required environment variables are set
|
|
if [ -z "$SIA_GITHUB_API_KEY" ]; then
|
|
echo "Error: SIA_GITHUB_API_KEY environment variable is not set"
|
|
exit 1
|
|
fi
|
|
|
|
# Install dependencies
|
|
apt-get update
|
|
apt-get install -y jq openssh-client git
|
|
|
|
# Create SSH directory if it doesn't exist
|
|
mkdir -p ~/.ssh
|
|
chmod 700 ~/.ssh
|
|
|
|
# Generate SSH key if it doesn't exist
|
|
if [ ! -f ~/.ssh/sia_repo ]; then
|
|
echo "Generating new SSH key..."
|
|
ssh-keygen -t ed25519 -f ~/.ssh/sia_repo -N ""
|
|
fi
|
|
|
|
# Ensure proper permissions on SSH keys
|
|
chmod 600 ~/.ssh/sia_repo
|
|
chmod 644 ~/.ssh/sia_repo.pub
|
|
|
|
# Get the public key
|
|
SSH_PUBLIC_KEY=$(cat ~/.ssh/sia_repo.pub)
|
|
|
|
# Add host key to known_hosts to avoid verification prompt
|
|
echo "Adding host key to known_hosts..."
|
|
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
|
|
chmod 600 ~/.ssh/known_hosts
|
|
|
|
# Check if a key with title "sia" already exists and delete it
|
|
echo "Checking for existing SSH key in GitHub..."
|
|
EXISTING_KEYS=$(curl -s -X GET "https://api.github.com/user/keys" \
|
|
-H "Accept: application/vnd.github+json" \
|
|
-H "Authorization: token $SIA_GITHUB_API_KEY")
|
|
KEY_ID=$(echo "$EXISTING_KEYS" | jq -r '.[] | select(.title=="sia") | .id')
|
|
if [ ! -z "$KEY_ID" ]; then
|
|
echo "Found existing key with ID $KEY_ID, deleting..."
|
|
curl -X DELETE "https://api.github.com/user/keys/$KEY_ID" \
|
|
-H "Accept: application/vnd.github+json" \
|
|
-H "Authorization: token $SIA_GITHUB_API_KEY"
|
|
fi
|
|
|
|
# Add the SSH key to GitHub using the API
|
|
echo "Adding SSH key to GitHub using API..."
|
|
curl -X POST "https://api.github.com/user/keys" \
|
|
-H "Accept: application/vnd.github+json" \
|
|
-H "Content-Type: application/json" \
|
|
-H "Authorization: token $SIA_GITHUB_API_KEY" \
|
|
-d "{\"title\":\"sia\", \"key\":\"$SSH_PUBLIC_KEY\"}"
|
|
|
|
# Create SSH config file to specify which key to use for GitHub
|
|
echo "Configuring SSH to use the correct key for GitHub..."
|
|
cat > ~/.ssh/config << EOF
|
|
Host github.com
|
|
HostName github.com
|
|
User git
|
|
IdentityFile ~/.ssh/sia_repo
|
|
IdentitiesOnly yes
|
|
EOF
|
|
|
|
chmod 600 ~/.ssh/config
|
|
|
|
# Start SSH agent and add the key
|
|
eval $(ssh-agent -s)
|
|
ssh-add ~/.ssh/sia_repo
|
|
|
|
# Ensure git is configured with a name and email
|
|
if ! git config --global user.email > /dev/null; then
|
|
echo "Configuring default git user email..."
|
|
git config --global user.email "niels.geens@gmail.com"
|
|
fi
|
|
|
|
if ! git config --global user.name > /dev/null; then
|
|
echo "Configuring default git user name..."
|
|
git config --global user.name "Niels Geens"
|
|
fi |