Make sure correct user runs the scripts

This commit is contained in:
2025-07-04 11:37:48 +09:00
parent f408d9b0b8
commit 2c1ac5c9bc
2 changed files with 28 additions and 10 deletions

View File

@@ -10,19 +10,26 @@ if [ -f "${CONFIG_BASE}webhook.cfg" ]; then
# shellcheck disable=SC1091
source <(grep "=" "${CONFIG_BASE}webhook.cfg" | sed 's/ *= */=/g')
fi;
error=0;
if [ "${USE_SUDO}" != "0" ] && ! id "${SUDO_USER}" &>/dev/null; then
echo "sudo user ${SUDO_USER} does not exist";
exit;
error=1;
fi;
# check that user exist
# check that git exists
if [ -z "$(command -v git)" ]; then
echo "git is not installed";
exit;
error=1;
fi;
GIT_COMMAND_BASE=("git");
if [ -n "${USE_SUDO}" ]; then
GIT_COMMAND_BASE=("sudo" "-u" "${SUDO_USER}" "git");.
# if we are root -> ok, else we must be SUDO USER
if [ "$(whoami)" = "root" ]; then
GIT_COMMAND_BASE=("sudo" "-u" "${SUDO_USER}" "git");.
elif [ "$(whoami)" != "${SUDO_USER}" ]; then
echo "Script must be run as root or as the ${SUDO_USER}";
error=1;
fi;
fi;
# add trailing slash if not set
@@ -33,24 +40,29 @@ LOG_FOLDER="log/"
# base folder does not exist
if [ ! -d "${GIT_REPOSITORY_FOLDER}" ]; then
echo "Base folder: ${GIT_REPOSITORY_FOLDER} not found";
exit;
error=1;
fi;
# branch name not set
if [ -n "${BRANCH}" ]; then
echo "No branch name given";
exit;
error=1;
fi;
# check that log folder exists
if [ ! -d "${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}" ]; then
echo "Log folder does not exist: ${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}";
exit;
error=1;
fi;
# check that the base clone folder exists
if [ ! -d "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}" ]; then
echo "Clone base folder does not exist: ${GIT_REPOSITORY_FOLDER}${CLONE_BASE}";
error=1;
fi;
# exit on error
if [ $error -eq 1 ]; then
exit;
fi;