Update cron-job.sh
Browse files- cron-job.sh +22 -7
cron-job.sh
CHANGED
|
@@ -1,30 +1,45 @@
|
|
| 1 |
#!/usr/bin/env bash
|
| 2 |
|
| 3 |
# Git configuration
|
| 4 |
-
REPO_URL="https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/${GIT_REPO}"
|
| 5 |
TARGET_DIR="/app/backend/data"
|
| 6 |
LOG_FILE="/var/log/cron.log"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Log start time
|
| 9 |
-
|
| 10 |
|
| 11 |
# Pull the latest changes from the repository
|
| 12 |
-
|
| 13 |
cd $TARGET_DIR
|
|
|
|
| 14 |
git config --global --add safe.directory $TARGET_DIR >> $LOG_FILE 2>&1
|
|
|
|
|
|
|
| 15 |
git init >> $LOG_FILE 2>&1
|
| 16 |
git remote add origin $REPO_URL >> $LOG_FILE 2>&1
|
|
|
|
| 17 |
git -C $TARGET_DIR pull $REPO_URL >> $LOG_FILE 2>&1
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
while true; do
|
| 20 |
# Check if there are changes to commit
|
| 21 |
if [[ -n $(git -C $TARGET_DIR status -s) ]]; then
|
| 22 |
-
|
| 23 |
-
git -C $TARGET_DIR add . >> $LOG_FILE 2>&1
|
| 24 |
git -C $TARGET_DIR commit -m "Automated commit of changes" >> $LOG_FILE 2>&1
|
| 25 |
git -C $TARGET_DIR push $REPO_URL >> $LOG_FILE 2>&1
|
|
|
|
|
|
|
|
|
|
| 26 |
else
|
| 27 |
-
|
| 28 |
fi
|
| 29 |
sleep 60
|
| 30 |
-
done
|
|
|
|
| 1 |
#!/usr/bin/env bash
|
| 2 |
|
| 3 |
# Git configuration
|
| 4 |
+
REPO_URL="https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/${GITHUB_USERNAME}/${GIT_REPO}"
|
| 5 |
TARGET_DIR="/app/backend/data"
|
| 6 |
LOG_FILE="/var/log/cron.log"
|
| 7 |
|
| 8 |
+
# Function to log messages to both the console and the log file
|
| 9 |
+
log_message() {
|
| 10 |
+
echo "$1" | tee -a $LOG_FILE
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
# Log start time
|
| 14 |
+
log_message "$(date +'%Y-%m-%d %H:%M:%S') - INFO - Starting cron job"
|
| 15 |
|
| 16 |
# Pull the latest changes from the repository
|
| 17 |
+
log_message "$(date +'%Y-%m-%d %H:%M:%S') - INFO - Pulling latest changes from repository"
|
| 18 |
cd $TARGET_DIR
|
| 19 |
+
rm webui.db
|
| 20 |
git config --global --add safe.directory $TARGET_DIR >> $LOG_FILE 2>&1
|
| 21 |
+
git config --global user.email "you@example.com"
|
| 22 |
+
git config --global user.name "Your Name"
|
| 23 |
git init >> $LOG_FILE 2>&1
|
| 24 |
git remote add origin $REPO_URL >> $LOG_FILE 2>&1
|
| 25 |
+
git branch -m main
|
| 26 |
git -C $TARGET_DIR pull $REPO_URL >> $LOG_FILE 2>&1
|
| 27 |
+
if [ $? -ne 0 ]; then
|
| 28 |
+
log_message "$(date +'%Y-%m-%d %H:%M:%S') - ERROR - Failed to pull changes"
|
| 29 |
+
fi
|
| 30 |
|
| 31 |
while true; do
|
| 32 |
# Check if there are changes to commit
|
| 33 |
if [[ -n $(git -C $TARGET_DIR status -s) ]]; then
|
| 34 |
+
log_message "$(date +'%Y-%m-%d %H:%M:%S') - INFO - Changes detected, committing changes"
|
| 35 |
+
git -C $TARGET_DIR add webui.db >> $LOG_FILE 2>&1
|
| 36 |
git -C $TARGET_DIR commit -m "Automated commit of changes" >> $LOG_FILE 2>&1
|
| 37 |
git -C $TARGET_DIR push $REPO_URL >> $LOG_FILE 2>&1
|
| 38 |
+
if [ $? -ne 0 ]; then
|
| 39 |
+
log_message "$(date +'%Y-%m-%d %H:%M:%S') - ERROR - Failed to push changes"
|
| 40 |
+
fi
|
| 41 |
else
|
| 42 |
+
log_message "$(date +'%Y-%m-%d %H:%M:%S') - INFO - No changes detected"
|
| 43 |
fi
|
| 44 |
sleep 60
|
| 45 |
+
done
|