g1-ts-common-packages/scripts/setup-deploy-key.sh

55 lines
1.7 KiB
Bash

#!/bin/bash
# Script to generate and configure a Gitea deploy key for this repository
# Ensure the scripts directory exists
mkdir -p scripts
# Configuration
KEY_NAME="g1-ts-common-packages-deploy-key"
KEY_FILE=".deploy-key"
KEY_FILE_PUB=".deploy-key.pub"
TOKEN_FILE=".gitea-token"
GITEA_URL="https://git.generation.one"
REPO_OWNER="GenerationOne"
REPO_NAME="g1-ts-common-packages"
# Check if keys already exist
if [ -f "$KEY_FILE" ] && [ -f "$KEY_FILE_PUB" ]; then
echo "Deploy keys already exist. Using existing keys."
else
echo "Generating new SSH key pair for deployment..."
ssh-keygen -t ed25519 -f "$KEY_FILE" -N "" -C "$KEY_NAME"
echo "SSH key pair generated."
fi
# Read the public key
PUBLIC_KEY=$(cat "$KEY_FILE_PUB")
# Check if token file exists
if [ ! -f "$TOKEN_FILE" ]; then
echo "Gitea token file not found."
echo "Please create a file named '$TOKEN_FILE' with your Gitea access token."
echo "You can generate a token at $GITEA_URL/user/settings/applications"
echo "Make sure the token has 'write:repository' permission."
exit 1
fi
# Read the token
GITEA_TOKEN=$(cat "$TOKEN_FILE")
# Add the deploy key to the repository
echo "Adding deploy key to repository..."
curl -X POST \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"title\":\"$KEY_NAME\", \"key\":\"$PUBLIC_KEY\", \"read_only\":false}" \
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/keys"
echo ""
echo "Deploy key setup complete."
echo "The private key is stored in $KEY_FILE"
echo "The public key is stored in $KEY_FILE_PUB"
echo ""
echo "To use this key in CI/CD pipelines, add the private key as a secret."
echo "For GitHub Actions, you can add it as a repository secret named 'GITEA_DEPLOY_KEY'."