diff --git a/deploy/dalidou/deploy.sh b/deploy/dalidou/deploy.sh index 9e01545..68ab572 100644 --- a/deploy/dalidou/deploy.sh +++ b/deploy/dalidou/deploy.sh @@ -90,6 +90,57 @@ log " branch: $BRANCH" log " health url: $HEALTH_URL" log " dry run: $DRY_RUN" +# --------------------------------------------------------------------- +# Step 0: pre-flight permission check +# --------------------------------------------------------------------- +# +# If $APP_DIR exists but the current user cannot write to it (because +# a previous manual deploy left it root-owned, for example), the git +# fetch / reset in step 1 will fail with cryptic errors. Detect this +# up front and give the operator a clean remediation command instead +# of letting git produce half-state on partial failure. This was the +# exact workaround the 2026-04-08 Dalidou redeploy needed — pre- +# existing root ownership from the pre-phase9 manual schema fix. + +if [ -d "$APP_DIR" ] && [ "$DRY_RUN" != "1" ]; then + if [ ! -w "$APP_DIR" ] || [ ! -r "$APP_DIR/.git" ] 2>/dev/null; then + log "WARNING: app dir exists but may not be writable by current user" + fi + current_owner="$(stat -c '%U:%G' "$APP_DIR" 2>/dev/null || echo unknown)" + current_user="$(id -un 2>/dev/null || echo unknown)" + current_uid_gid="$(id -u 2>/dev/null):$(id -g 2>/dev/null)" + log "Step 0: permission check" + log " app dir owner: $current_owner" + log " current user: $current_user ($current_uid_gid)" + # Try to write a tiny marker file. If it fails, surface a clean + # remediation message and exit before git produces confusing + # half-state. + marker="$APP_DIR/.deploy-permission-check" + if ! ( : > "$marker" ) 2>/dev/null; then + log "FATAL: cannot write to $APP_DIR as $current_user" + log "" + log "The app dir is owned by $current_owner and the current user" + log "doesn't have write permission. This usually happens after a" + log "manual workaround deploy that ran as root." + log "" + log "Remediation (pick the one that matches your setup):" + log "" + log " # If you have passwordless sudo and gitea runs as UID 1000:" + log " sudo chown -R 1000:1000 $APP_DIR" + log "" + log " # If you're running deploy.sh itself as root:" + log " sudo bash $0" + log "" + log " # If neither works, do it via a throwaway container:" + log " docker run --rm -v $APP_DIR:/app alpine \\" + log " chown -R 1000:1000 /app" + log "" + log "Then re-run deploy.sh." + exit 5 + fi + rm -f "$marker" 2>/dev/null || true +fi + # --------------------------------------------------------------------- # Step 1: make sure $APP_DIR is a proper git checkout of the branch # ---------------------------------------------------------------------