-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcron.sh
More file actions
executable file
·68 lines (57 loc) · 2.14 KB
/
Copy pathcron.sh
File metadata and controls
executable file
·68 lines (57 loc) · 2.14 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
#!/bin/sh
# Auto-deploy script for warp-charger.com and docs.warp-charger.com
# Called via cron (as root). Git operations and builds run as the service user.
#
# Both sites live in the same git checkout, so a single fetch determines what
# changed; each site is only rebuilt when files below its directory changed.
#
# Branch selection based on hostname:
# stagingwww -> stagingwww.warp-charger.com branch
# otherwise -> master branch
#
# Initial setup on the server (run once as service user):
#
# cd /home/www
# git clone --depth=1 --filter=blob:none --sparse \
# git@github.com:Tinkerforge/warp-charger.git warp-charger
# cd warp-charger
# git sparse-checkout set warp-charger.com firmwares documents docs.warp-charger.com
set -e
# Determine branch and user based on hostname
HOSTNAME=$(hostname)
if [ "$HOSTNAME" = "stagingwww" ]; then
BRANCH="stagingwww.warp-charger.com"
SERVICE_USER="stagingwww"
else
BRANCH="master"
SERVICE_USER="www"
fi
REPO_DIR="/home/$SERVICE_USER/warp-charger"
SITE_DIR="$REPO_DIR/warp-charger.com"
DOCS_DIR="$REPO_DIR/docs.warp-charger.com"
run_as_user() {
su -s /bin/sh "$SERVICE_USER" -c "$1"
}
cd "$REPO_DIR"
run_as_user "/usr/bin/git fetch --depth=1 origin $BRANCH"
LOCAL_HASH=$(run_as_user "/usr/bin/git rev-parse HEAD")
REMOTE_HASH=$(run_as_user "/usr/bin/git rev-parse origin/$BRANCH")
# Exit silently if nothing changed
[ "$LOCAL_HASH" = "$REMOTE_HASH" ] && exit 0
# Determine which paths changed before updating the working tree
CHANGED=$(run_as_user "/usr/bin/git diff --name-only $LOCAL_HASH origin/$BRANCH")
# Update working tree
run_as_user "/usr/bin/git reset --hard origin/$BRANCH"
# Rebuild warp-charger.com when its inputs changed
if echo "$CHANGED" | grep -qE '^(warp-charger\.com|firmwares|documents)/'; then
cd "$SITE_DIR"
run_as_user "./compile.sh"
/usr/bin/systemctl restart warp-charger.com
fi
# Rebuild docs.warp-charger.com when its inputs changed
if echo "$CHANGED" | grep -qE '^docs\.warp-charger\.com/'; then
cd "$DOCS_DIR"
run_as_user "/usr/bin/npm ci"
run_as_user "/usr/bin/npm run build"
run_as_user "/usr/bin/rsync -a --delete build/ build_release/"
fi