Files
michaelschiemer/deployment/gitea-runner/register.sh

118 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Gitea Actions Runner Registration Script
# This script registers the runner with the Gitea instance
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}Gitea Actions Runner Registration${NC}"
echo "======================================"
# Check if .env exists
if [ ! -f .env ]; then
echo -e "${RED}Error: .env file not found${NC}"
echo "Please copy .env.example to .env and configure it"
echo ""
echo " cp .env.example .env"
echo " nano .env"
echo ""
exit 1
fi
# Load environment variables
set -a
source .env
set +a
# Validate required variables
if [ -z "${GITEA_INSTANCE_URL:-}" ]; then
echo -e "${RED}Error: GITEA_INSTANCE_URL is not set in .env${NC}"
exit 1
fi
if [ -z "${GITEA_RUNNER_REGISTRATION_TOKEN:-}" ]; then
echo -e "${RED}Error: GITEA_RUNNER_REGISTRATION_TOKEN is not set in .env${NC}"
echo ""
echo "To get registration token:"
echo "1. Go to Gitea: ${GITEA_INSTANCE_URL}/admin/actions/runners"
echo "2. Click 'Create New Runner'"
echo "3. Copy the registration token"
echo "4. Add it to .env file"
echo ""
exit 1
fi
echo -e "${YELLOW}Configuration:${NC}"
echo " Gitea URL: ${GITEA_INSTANCE_URL}"
echo " Runner Name: ${GITEA_RUNNER_NAME:-dev-runner}"
echo " Labels: ${GITEA_RUNNER_LABELS:-ubuntu-latest:docker://node:16-bullseye}"
echo ""
# Check if runner is already registered
if [ -f ./data/.runner ]; then
echo -e "${YELLOW}Warning: Runner appears to be already registered${NC}"
echo "Registration file exists at: ./data/.runner"
echo ""
read -p "Do you want to re-register? This will unregister the existing runner. (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Registration cancelled"
exit 0
fi
echo "Removing existing registration..."
rm -f ./data/.runner
fi
# Create data directory if it doesn't exist
mkdir -p ./data
# Start services
echo ""
echo -e "${YELLOW}Starting services...${NC}"
docker compose up -d
# Wait for services to be ready
echo "Waiting for services to start..."
sleep 5
# Register runner
echo ""
echo -e "${YELLOW}Registering runner...${NC}"
docker compose exec -T gitea-runner act_runner register \
--instance "${GITEA_INSTANCE_URL}" \
--token "${GITEA_RUNNER_REGISTRATION_TOKEN}" \
--name "${GITEA_RUNNER_NAME:-dev-runner}" \
--labels "${GITEA_RUNNER_LABELS:-ubuntu-latest:docker://node:16-bullseye}"
if [ $? -eq 0 ]; then
echo ""
echo -e "${GREEN}✓ Runner registered successfully!${NC}"
echo ""
echo "Runner is now active and will start accepting jobs."
echo ""
echo "Useful commands:"
echo " docker compose logs -f gitea-runner # View runner logs"
echo " docker compose restart gitea-runner # Restart runner"
echo " docker compose down # Stop runner"
echo ""
echo "Check runner status in Gitea:"
echo " ${GITEA_INSTANCE_URL}/admin/actions/runners"
echo ""
else
echo ""
echo -e "${RED}✗ Registration failed${NC}"
echo "Check the logs for details:"
echo " docker compose logs gitea-runner"
echo ""
exit 1
fi