Files
gitlab-deployment/scripts/setup-runner.sh
2026-03-16 02:43:58 +08:00

80 lines
2.9 KiB
Bash

#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "=========================================="
echo " GitLab Runner Automated Setup Script "
echo "=========================================="
echo ""
read -p "Enter your GitLab URL / SERVER IP (e.g., http://git.amiirkhl.ir): " GITLAB_URL
read -p "Enter your Runner Token (starts with glrt-): " RUNNER_TOKEN
echo ""
echo "Registering runner to $GITLAB_URL..."
docker exec gitlab-runner gitlab-runner register \
--non-interactive \
--url "$GITLAB_URL" \
--token "$RUNNER_TOKEN" \
--executor "docker" \
--docker-image "alpine:latest" \
--description "Auto-Registered Docker Runner" \
--docker-volumes "/var/run/docker.sock:/var/run/docker.sock" \
--docker-network-mode "gitlab_net"
if [ $? -eq 0 ]; then
echo -e "\n${GREEN}==========================================${NC}"
echo -e "${GREEN} SUCCESS! Runner registered successfully. ${NC}"
echo -e "${GREEN}==========================================${NC}"
echo ""
echo "--- Tuning Runner for Host Resources ---"
CORES=$(nproc 2>/dev/null || echo 1)
RAM_GB=$(awk '/MemTotal/ {printf "%.0f", $2/1024/1024}' /proc/meminfo 2>/dev/null || echo 2)
echo "Detected $CORES CPU Cores and ${RAM_GB}GB RAM."
CONCURRENT_JOBS=$CORES
if [ "$CONCURRENT_JOBS" -lt 1 ]; then
CONCURRENT_JOBS=1
fi
echo "Applying runner concurrency limit of $CONCURRENT_JOBS..."
CONFIG_FILE="./runner-config/config.toml"
if [ -f "$CONFIG_FILE" ]; then
if ! sed -i "s/^concurrent = .*/concurrent = $CONCURRENT_JOBS/" "$CONFIG_FILE" 2>/dev/null; then
echo -e "\n${RED}[ERROR] Permission denied: sed cannot modify $CONFIG_FILE.${NC}"
echo -e "${RED}The script failed. This usually happens if config.toml is owned by root (created by Docker).${NC}"
echo -e "${RED}Try running this script with sudo.${NC}"
exit 1
fi
if ! docker restart gitlab-runner >/dev/null 2>&1; then
echo -e "\n${RED}[ERROR] Permission denied: Cannot restart docker container.${NC}"
echo -e "${RED}Try running this script with sudo.${NC}"
exit 1
fi
echo -e "${GREEN}-> Runner restarted to apply new concurrency limit.${NC}"
else
echo -e "\n${YELLOW}[WARNING] config.toml not found at $CONFIG_FILE.${NC}"
echo -e "${YELLOW}-> Could not set concurrency automatically.${NC}"
fi
echo -e "\n${GREEN}==========================================${NC}"
echo -e "${GREEN} SETUP FULLY COMPLETE! ${NC}"
echo -e "${GREEN}==========================================${NC}\n"
else
echo -e "\n${RED}==========================================${NC}"
echo -e "${RED} ERROR! Failed to register the runner. ${NC}"
echo -e "${RED} Please check your URL and Token. ${NC}"
echo -e "${RED}==========================================${NC}\n"
exit 1
fi