fix: restore invoking user's ownership after sudo deploy scripts

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Abhishek Kumar 2026-07-14 18:05:13 +05:30
parent 0ea3e34653
commit d84f0c1af8
4 changed files with 45 additions and 0 deletions

View file

@ -22,6 +22,16 @@ cleanup() {
if [[ -n "$BOOTSTRAP_LIB" ]]; then
rm -f "$BOOTSTRAP_LIB"
fi
# When run via sudo (the common case: docker access, root-owned installs),
# the refreshed deployment files and the rewritten .env become root-owned,
# breaking later sudo-less edits. Hand the install back to the user who
# invoked sudo; a no-op for unprivileged runs and real root, where SUDO_UID
# is unset. Runs from the EXIT trap so a mid-update failure also leaves
# ownership fixed.
if [[ -n "${SUDO_UID:-}" && -n "${SUDO_GID:-}" && -n "${DOGRAH_DEPLOY_PROJECT_DIR:-}" && -d "$DOGRAH_DEPLOY_PROJECT_DIR" ]]; then
echo -e "${BLUE}Restoring ownership of $DOGRAH_DEPLOY_PROJECT_DIR to ${SUDO_USER:-uid $SUDO_UID}...${NC}"
chown -R "$SUDO_UID:$SUDO_GID" "$DOGRAH_DEPLOY_PROJECT_DIR" || true
fi
}
trap cleanup EXIT