|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# HRM GitHub Actions Runner - Interactive Configuration Script |
| 4 | +# This script helps create a .env.runner file with interactive prompts |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Change to the directory where this script is located |
| 9 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 10 | +cd "$SCRIPT_DIR" |
| 11 | + |
| 12 | +# Colors for better UX |
| 13 | +if [ -t 1 ]; then |
| 14 | + BLUE='\033[0;34m' |
| 15 | + GREEN='\033[0;32m' |
| 16 | + YELLOW='\033[1;33m' |
| 17 | + NC='\033[0m' # No Color |
| 18 | +else |
| 19 | + BLUE='' |
| 20 | + GREEN='' |
| 21 | + YELLOW='' |
| 22 | + NC='' |
| 23 | +fi |
| 24 | + |
| 25 | +echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}" |
| 26 | +echo -e "${BLUE}║ HRM GitHub Actions Runner - Configuration Setup ║${NC}" |
| 27 | +echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}" |
| 28 | +echo "" |
| 29 | + |
| 30 | +# Check if .env.runner already exists |
| 31 | +if [ -f .env.runner ]; then |
| 32 | + echo -e "${YELLOW}Warning: .env.runner already exists.${NC}" |
| 33 | + read -p "Do you want to overwrite it? [y/N] " -n 1 -r |
| 34 | + echo |
| 35 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 36 | + echo "Configuration cancelled. Existing .env.runner preserved." |
| 37 | + exit 0 |
| 38 | + fi |
| 39 | + echo "" |
| 40 | +fi |
| 41 | + |
| 42 | +# Function to prompt for input with default value |
| 43 | +prompt_with_default() { |
| 44 | + local prompt="$1" |
| 45 | + local default="$2" |
| 46 | + local var_name="$3" |
| 47 | + local is_secret="${4:-false}" |
| 48 | + |
| 49 | + if [ -n "$default" ]; then |
| 50 | + echo -e "${BLUE}$prompt${NC}" |
| 51 | + echo -e " ${GREEN}(default: $default)${NC}" |
| 52 | + else |
| 53 | + echo -e "${BLUE}$prompt${NC}" |
| 54 | + fi |
| 55 | + |
| 56 | + if [ "$is_secret" = "true" ]; then |
| 57 | + read -s -p "> " value |
| 58 | + echo "" |
| 59 | + else |
| 60 | + read -p "> " value |
| 61 | + fi |
| 62 | + |
| 63 | + if [ -z "$value" ]; then |
| 64 | + eval "$var_name='$default'" |
| 65 | + else |
| 66 | + eval "$var_name='$value'" |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +# Function to prompt yes/no |
| 71 | +prompt_yes_no() { |
| 72 | + local prompt="$1" |
| 73 | + local default="$2" |
| 74 | + |
| 75 | + if [ "$default" = "y" ]; then |
| 76 | + read -p "$prompt [Y/n] " -n 1 -r |
| 77 | + else |
| 78 | + read -p "$prompt [y/N] " -n 1 -r |
| 79 | + fi |
| 80 | + echo |
| 81 | + |
| 82 | + if [ -z "$REPLY" ]; then |
| 83 | + [ "$default" = "y" ] |
| 84 | + else |
| 85 | + [[ $REPLY =~ ^[Yy]$ ]] |
| 86 | + fi |
| 87 | +} |
| 88 | + |
| 89 | +echo "═══════════════════════════════════════════════════════════" |
| 90 | +echo " Required Configuration" |
| 91 | +echo "═══════════════════════════════════════════════════════════" |
| 92 | +echo "" |
| 93 | + |
| 94 | +# Repository URL |
| 95 | +prompt_with_default \ |
| 96 | + "GitHub repository URL:" \ |
| 97 | + "https://github.com/arii/hrm" \ |
| 98 | + "REPO_URL" |
| 99 | + |
| 100 | +echo "" |
| 101 | +echo "═══════════════════════════════════════════════════════════" |
| 102 | +echo " Token Configuration (Optional)" |
| 103 | +echo "═══════════════════════════════════════════════════════════" |
| 104 | +echo "" |
| 105 | +echo "The runner needs a registration token. You have three options:" |
| 106 | +echo " 1. Auto-generate using GitHub CLI (gh) - Recommended" |
| 107 | +echo " 2. Auto-generate using Personal Access Token (PAT)" |
| 108 | +echo " 3. Manually provide a runner token (expires in 1 hour)" |
| 109 | +echo "" |
| 110 | + |
| 111 | +# Check if gh CLI is available |
| 112 | +GH_AVAILABLE=false |
| 113 | +if command -v gh &> /dev/null; then |
| 114 | + if gh auth status &> /dev/null; then |
| 115 | + echo -e "${GREEN}✓ GitHub CLI (gh) is installed and authenticated${NC}" |
| 116 | + GH_AVAILABLE=true |
| 117 | + else |
| 118 | + echo -e "${YELLOW}! GitHub CLI (gh) is installed but not authenticated${NC}" |
| 119 | + echo " Run 'gh auth login' to enable automatic token generation" |
| 120 | + fi |
| 121 | +else |
| 122 | + echo " GitHub CLI (gh) is not installed" |
| 123 | +fi |
| 124 | +echo "" |
| 125 | + |
| 126 | +RUNNER_TOKEN="" |
| 127 | +GITHUB_PAT="" |
| 128 | + |
| 129 | +if [ "$GH_AVAILABLE" = true ]; then |
| 130 | + if prompt_yes_no "Use GitHub CLI for automatic token generation?" "y"; then |
| 131 | + echo -e "${GREEN}✓ Will use GitHub CLI to auto-generate tokens${NC}" |
| 132 | + RUNNER_TOKEN="" |
| 133 | + GITHUB_PAT="" |
| 134 | + else |
| 135 | + if prompt_yes_no "Do you want to provide a Personal Access Token (PAT)?" "n"; then |
| 136 | + echo "" |
| 137 | + echo "Generate a PAT at: https://github.com/settings/tokens" |
| 138 | + echo "Required scopes: 'repo' (or 'public_repo' for public repos)" |
| 139 | + echo "" |
| 140 | + prompt_with_default \ |
| 141 | + "Personal Access Token:" \ |
| 142 | + "" \ |
| 143 | + "GITHUB_PAT" \ |
| 144 | + "true" |
| 145 | + else |
| 146 | + if prompt_yes_no "Do you want to provide a manual runner token?" "n"; then |
| 147 | + echo "" |
| 148 | + echo "Get token from: GitHub Repo → Settings → Actions → Runners → New self-hosted runner" |
| 149 | + echo "Note: Tokens expire after 1 hour" |
| 150 | + echo "" |
| 151 | + prompt_with_default \ |
| 152 | + "Runner token:" \ |
| 153 | + "" \ |
| 154 | + "RUNNER_TOKEN" \ |
| 155 | + "true" |
| 156 | + fi |
| 157 | + fi |
| 158 | + fi |
| 159 | +else |
| 160 | + if prompt_yes_no "Do you want to provide a Personal Access Token (PAT)?" "n"; then |
| 161 | + echo "" |
| 162 | + echo "Generate a PAT at: https://github.com/settings/tokens" |
| 163 | + echo "Required scopes: 'repo' (or 'public_repo' for public repos)" |
| 164 | + echo "" |
| 165 | + prompt_with_default \ |
| 166 | + "Personal Access Token:" \ |
| 167 | + "" \ |
| 168 | + "GITHUB_PAT" \ |
| 169 | + "true" |
| 170 | + else |
| 171 | + if prompt_yes_no "Do you want to provide a manual runner token?" "n"; then |
| 172 | + echo "" |
| 173 | + echo "Get token from: GitHub Repo → Settings → Actions → Runners → New self-hosted runner" |
| 174 | + echo "Note: Tokens expire after 1 hour" |
| 175 | + echo "" |
| 176 | + prompt_with_default \ |
| 177 | + "Runner token:" \ |
| 178 | + "" \ |
| 179 | + "RUNNER_TOKEN" \ |
| 180 | + "true" |
| 181 | + fi |
| 182 | + fi |
| 183 | +fi |
| 184 | + |
| 185 | +echo "" |
| 186 | +echo "═══════════════════════════════════════════════════════════" |
| 187 | +echo " Resource Limits (Optional)" |
| 188 | +echo "═══════════════════════════════════════════════════════════" |
| 189 | +echo "" |
| 190 | +echo "Recommended for production to prevent resource exhaustion" |
| 191 | +echo "" |
| 192 | + |
| 193 | +RUNNER_MEMORY_LIMIT="" |
| 194 | +RUNNER_CPU_LIMIT="" |
| 195 | + |
| 196 | +if prompt_yes_no "Configure resource limits?" "n"; then |
| 197 | + echo "" |
| 198 | + prompt_with_default \ |
| 199 | + "Memory limit (e.g., 2g for 2GB, 512m for 512MB, leave empty for no limit):" \ |
| 200 | + "" \ |
| 201 | + "RUNNER_MEMORY_LIMIT" |
| 202 | + |
| 203 | + echo "" |
| 204 | + prompt_with_default \ |
| 205 | + "CPU limit (e.g., 2 for 2 CPUs, 0.5 for half a CPU, leave empty for no limit):" \ |
| 206 | + "" \ |
| 207 | + "RUNNER_CPU_LIMIT" |
| 208 | +fi |
| 209 | + |
| 210 | +echo "" |
| 211 | +echo "═══════════════════════════════════════════════════════════" |
| 212 | +echo " Summary" |
| 213 | +echo "═══════════════════════════════════════════════════════════" |
| 214 | +echo "" |
| 215 | +echo "Configuration summary:" |
| 216 | +echo " Repository: $REPO_URL" |
| 217 | +if [ -n "$RUNNER_TOKEN" ]; then |
| 218 | + echo " Runner token: [provided manually]" |
| 219 | +elif [ -n "$GITHUB_PAT" ]; then |
| 220 | + echo " GitHub PAT: [provided - will auto-generate tokens]" |
| 221 | +else |
| 222 | + echo " Token: [will auto-generate using GitHub CLI]" |
| 223 | +fi |
| 224 | +if [ -n "$RUNNER_MEMORY_LIMIT" ]; then |
| 225 | + echo " Memory limit: $RUNNER_MEMORY_LIMIT" |
| 226 | +fi |
| 227 | +if [ -n "$RUNNER_CPU_LIMIT" ]; then |
| 228 | + echo " CPU limit: $RUNNER_CPU_LIMIT" |
| 229 | +fi |
| 230 | +echo "" |
| 231 | + |
| 232 | +if ! prompt_yes_no "Create .env.runner with this configuration?" "y"; then |
| 233 | + echo "Configuration cancelled." |
| 234 | + exit 0 |
| 235 | +fi |
| 236 | + |
| 237 | +# Create .env.runner file |
| 238 | +cat > .env.runner << EOF |
| 239 | +# GitHub Actions Runner Configuration |
| 240 | +# Generated by configure.sh on $(date) |
| 241 | +
|
| 242 | +# Required: The GitHub repository URL |
| 243 | +REPO_URL=$REPO_URL |
| 244 | +
|
| 245 | +# Optional: Runner registration token (auto-generated if not provided) |
| 246 | +# If not set, the script will attempt to generate it automatically using: |
| 247 | +# 1. GitHub CLI (gh) if authenticated |
| 248 | +# 2. GITHUB_PAT if provided below |
| 249 | +# Get manually from: GitHub Repo Settings → Actions → Runners → New self-hosted runner |
| 250 | +# Note: Tokens are short-lived (valid for 1 hour) |
| 251 | +RUNNER_TOKEN=$RUNNER_TOKEN |
| 252 | +
|
| 253 | +# Optional: GitHub Personal Access Token for auto-generating runner token |
| 254 | +# Required if gh CLI is not authenticated and RUNNER_TOKEN is not provided |
| 255 | +# Must have 'repo' scope (or 'public_repo' for public repositories) |
| 256 | +# Generate at: https://github.com/settings/tokens |
| 257 | +GITHUB_PAT=$GITHUB_PAT |
| 258 | +
|
| 259 | +# Optional: Resource limits for the Docker container |
| 260 | +# Recommended for production deployments to prevent resource exhaustion |
| 261 | +# Memory limit (e.g., 2g for 2 gigabytes, 512m for 512 megabytes) |
| 262 | +RUNNER_MEMORY_LIMIT=$RUNNER_MEMORY_LIMIT |
| 263 | +
|
| 264 | +# CPU limit (e.g., 2 for 2 CPUs, 0.5 for half a CPU) |
| 265 | +RUNNER_CPU_LIMIT=$RUNNER_CPU_LIMIT |
| 266 | +EOF |
| 267 | + |
| 268 | +# Set restrictive permissions |
| 269 | +chmod 600 .env.runner |
| 270 | + |
| 271 | +echo "" |
| 272 | +echo -e "${GREEN}✓ Configuration file created successfully!${NC}" |
| 273 | +echo "" |
| 274 | +echo "File: $(pwd)/.env.runner" |
| 275 | +echo "Permissions: 600 (read/write for owner only)" |
| 276 | +echo "" |
| 277 | +echo "Next steps:" |
| 278 | +echo " 1. Review the configuration: cat .env.runner" |
| 279 | +echo " 2. Deploy the runner: ./deploy-runner.sh" |
| 280 | +echo "" |
0 commit comments