#!/bin/sh # ============================================================================= # Gitea Initialization Script # ============================================================================= # Replaces the default CMD in the Gitea Docker container. # Runs database migrations, creates the admin user (if credentials are provided # and the user doesn't already exist), then starts the Gitea web server. # # The Gitea entrypoint (/usr/bin/entrypoint) has already: # - Set up UID/GID, created directories, generated app.ini from GITEA__* env vars # But it exec's our CMD still as root — Gitea refuses to run as root. # We must drop to the 'git' user before running any gitea commands. # ============================================================================= set -e PREFIX="[gitea-init]" log() { echo "$PREFIX $1"; } # Drop privileges: Gitea refuses to run as root. The Docker entrypoint # sets up directories as root, then exec's the CMD (us) — still as root. # Re-exec this script as the 'git' user via su-exec. if [ "$(id -u)" = "0" ]; then log "Dropping to git user..." exec su-exec git "$0" "$@" fi # --- Step 1: Run database migrations --- log "Running database migrations..." MIGRATE_OK=false for i in $(seq 1 10); do if gitea migrate 2>&1; then MIGRATE_OK=true log "Migrations complete" break fi log "Waiting for database... (attempt $i/10)" sleep 3 done if [ "$MIGRATE_OK" = false ]; then log "WARNING: Migrations may not have completed — starting anyway" fi # --- Step 2: Create admin user if credentials provided --- if [ -n "$GITEA_ADMIN_USER" ] && [ -n "$GITEA_ADMIN_PASSWORD" ] && [ -n "$GITEA_ADMIN_EMAIL" ]; then log "Creating admin user '${GITEA_ADMIN_USER}'..." if gitea admin user create --admin \ --username "$GITEA_ADMIN_USER" \ --password "$GITEA_ADMIN_PASSWORD" \ --email "$GITEA_ADMIN_EMAIL" \ --must-change-password false 2>&1; then log "Admin user created successfully" else log "Admin user already exists (or creation skipped)" fi else log "No GITEA_ADMIN_USER/PASSWORD/EMAIL set — skipping admin creation" fi # --- Step 3: Start Gitea web server --- log "Starting Gitea web server..." exec gitea web