5 Commits

4 changed files with 34 additions and 6 deletions

View File

@@ -56,6 +56,17 @@ A [branch] name must be set all the time.
An optional [remote name] can be set, if not set "origin" will be used.
All sync progress will be written to the log folder inside the base folder, the log file has the name of [repository]
Sample
```log
[2025-07-04 16:06:31] [<uniq id>] [START] git merge <repository folder> <remote name>/<branch>
[2025-07-04 16:06:31] [<uniq id>] Updating <sha>..<sha>
<git inforation>
[2025-07-04 16:06:31] [<uniq id>] [END]
```
## TODO
Future versions will hold an incoming webhook handler and a polling scripts (systemd based)

View File

@@ -47,6 +47,7 @@ fi;
CLONE_BASE="clone-base/"
LOG_FOLDER="log/"
SCRIPT_FOLDER="scripts/"
SECRETS_FOLDER="secrets/"
CONFIG_FOLDER="config/"
WWW_BASE="www/"
WWW_WEBHOOK_INCOMING="${WWW_BASE}webhook-incoming";
@@ -125,6 +126,7 @@ EOF
"${GIT_WEBHOOK_BASE_FOLDER}${LOG_FOLDER}" \
"${GIT_WEBHOOK_BASE_FOLDER}${SCRIPT_FOLDER}" \
"${GIT_WEBHOOK_BASE_FOLDER}${CONFIG_FOLDER}" \
"${GIT_WEBHOOK_BASE_FOLDER}${SECRETS_FOLDER}" \
"${GIT_WEBHOOK_BASE_FOLDER}${WWW_WEBHOOK_INCOMING}" \
"${GIT_WEBHOOK_BASE_FOLDER}${WWW_ADMIN}";
# set basic folder rights, clone folder is excluded
@@ -132,6 +134,7 @@ EOF
"${GIT_WEBHOOK_BASE_FOLDER}${LOG_FOLDER}" \
"${GIT_WEBHOOK_BASE_FOLDER}${SCRIPT_FOLDER}" \
"${GIT_WEBHOOK_BASE_FOLDER}${CONFIG_FOLDER}" \
"${GIT_WEBHOOK_BASE_FOLDER}${SECRETS_FOLDER}" \
"${GIT_WEBHOOK_BASE_FOLDER}${WWW_WEBHOOK_INCOMING}" \
"${GIT_WEBHOOK_BASE_FOLDER}${WWW_ADMIN}";
# setfacl -m u:"${SUDO_USER}":rwx -R "${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}"

View File

@@ -31,7 +31,7 @@ GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "fetch" "-
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "diff" "--stat" "HEAD" "${REMOTE_NAME}/${BRANCH}")
changes=$("${GIT_COMMAND[@]}" 2>&1)
if [ -n "${changes}" ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [START] Changes" &>> "$LOG_FILE";
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [START] git merge ${GIT_REPOSITORY_FOLDER} ${REMOTE_NAME}/${BRANCH}" &>> "$LOG_FILE";
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" merge "${REMOTE_NAME}/${BRANCH}")
log_data=$("${GIT_COMMAND[@]}" 2>&1);
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] ${log_data}" &>> "$LOG_FILE";

View File

@@ -58,11 +58,25 @@ if [ $error -eq 1 ]; then
exit;
fi;
# from the repository get the last path without the .git so we have the target folder
GIT_REPOSITORY_FOLDER=$(basename "${REPOSITORY}" .git);
unique_id=$(uuidgen | tr -d '-' | head -c 8);
echo "* New clone from ${REMOTE_HOST}:${REPOSITORY}::${BRANCH} into ${GIT_REPOSITORY_FOLDER}";
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "clone" "-b" "${BRANCH}" "--single-branch" "--depth" "1" "--origin" "${REMOTE_NAME}" "${REMOTE_HOST}:${REPOSITORY}" "${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}${GIT_REPOSITORY_FOLDER}")
"${GIT_COMMAND[@]}";
# strip .git from the repository path
GIT_REPOSITORY_NAME=$(basename "${REPOSITORY}" .git);
# log folder target
LOG_FILE="${GIT_WEBHOOK_BASE_FOLDER}${LOG_FOLDER}${GIT_REPOSITORY_NAME}.log";
# from the repository get the last path without the .git so we have the target folder
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [START] New clone from ${REMOTE_HOST}:${REPOSITORY}::${BRANCH} into ${GIT_REPOSITORY_NAME}" | tee -a "$LOG_FILE";
# clone everything
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "clone" "-b" "${BRANCH}" "--single-branch" "--depth" "1" "--origin" "${REMOTE_NAME}" "${REMOTE_HOST}:${REPOSITORY}" "${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}${GIT_REPOSITORY_NAME}")
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
# set the repository folder
GIT_REPOSITORY_FOLDER="${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}${GIT_REPOSITORY_NAME}";
# show origin info
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" remote show "${REMOTE_NAME}" );
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
# get last log entry
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" log -n 1 --pretty=short --no-color);
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [FINISH] clone completed" | tee -a "$LOG_FILE";
# __END__