forked from rikgale/LocalAircraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAUTOUPDATE
More file actions
executable file
·101 lines (87 loc) · 3.01 KB
/
Copy pathAUTOUPDATE
File metadata and controls
executable file
·101 lines (87 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
#
# ─────────────────────────────────────────────────────────────────────────────
# IMPORTANT – ONE-TIME SETUP REQUIRED (PER USER)
#
# Each user that runs this script (manually or via cron) must explicitly
# mark the repository directory as trusted, due to Git security protections
# introduced in Git 2.35+ ("dubious ownership" checks).
#
# Run ONCE as the user that will execute this script:
#
# git config --global --add safe.directory /path/to/repo
#
# Example:
# git config --global --add safe.directory /home/$USER/repo_name
#
# ─────────────────────────────────────────────────────────────────────────────
#
set -e
# -----------------------------
# Detect user and home directory dynamically
# -----------------------------
RUN_USER="$(id -un)"
RUN_HOME="$(getent passwd "$RUN_USER" | cut -d: -f6)"
: "${RUN_HOME:=$HOME}"
# -----------------------------
# Log file setup
# -----------------------------
LOGFILE="$RUN_HOME/logs/cron-jobs.log"
mkdir -p "$(dirname "$LOGFILE")"
# -----------------------------
# TTY-aware logging
# -----------------------------
if [[ -t 1 ]]; then
# Interactive: show output + log
exec > >(tee -a "$LOGFILE") 2>&1
else
# Cron: log only
exec >>"$LOGFILE" 2>&1
fi
# -----------------------------
# Git safety for cron
# -----------------------------
export HOME="$RUN_HOME"
export USER="$RUN_USER"
export GIT_PAGER=cat
# -----------------------------
# Directory name for log prefix
# -----------------------------
SCRIPT_DIR_NAME="$(basename "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)")"
log() {
echo "$SCRIPT_DIR_NAME [$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
# -----------------------------
# Prevent overlapping runs (per user + repo)
# -----------------------------
LOCKFILE="/tmp/${SCRIPT_DIR_NAME}-${RUN_USER}.lock"
exec 9>"$LOCKFILE" || exit 1
flock -n 9 || exit 0
# -----------------------------
# Change to repo directory
# -----------------------------
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
# -----------------------------
# Prefix Git output reliably
# -----------------------------
git_prefixed() {
# Usage: git_prefixed git <args>
"$@" 2>&1 | while IFS= read -r line; do
echo "$SCRIPT_DIR_NAME [$(date '+%Y-%m-%d %H:%M:%S')] $line"
done
}
# -----------------------------
# Begin auto-update
# -----------------------------
log "AUTOUPDATE started (user=$RUN_USER)"
git_prefixed git pull --ff-only
if [[ -n $(git status --porcelain) ]]; then
log "Changes found. Committing and pushing..."
git add -A
git_prefixed git commit -m "Updated $(date +%Y-%m-%d)"
git_prefixed git push
log "Changes pushed successfully"
else
log "No changes found. Skip pushing."
fi
log "AUTOUPDATE finished"