-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_git.sh
More file actions
executable file
·81 lines (67 loc) · 2.95 KB
/
push_git.sh
File metadata and controls
executable file
·81 lines (67 loc) · 2.95 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
#!/usr/bin/env bash
# ==============================================================================
# push_git.sh — Sync /opt to GitHub (rogerworkman1972/media-server, branch: main)
#
# Usage:
# ./push_git.sh # auto commit message
# ./push_git.sh -m "my commit message" # custom message
# ==============================================================================
set -euo pipefail
# ------------------------------------------------------------------------------
# Config
# ------------------------------------------------------------------------------
REPO_URL="git@github.com:rogerworkman1972/media-server.git"
BRANCH="main"
SOURCE_DIR="/opt"
WORK_DIR="/tmp/media_server_sync_$$"
COMMIT_MSG="chore: sync /opt — $(date '+%Y-%m-%d %H:%M:%S')"
while getopts "m:" opt; do
case $opt in
m) COMMIT_MSG="$OPTARG" ;;
*) echo "Usage: $0 [-m 'commit message']"; exit 1 ;;
esac
done
# ------------------------------------------------------------------------------
# Colors / logging
# ------------------------------------------------------------------------------
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
die() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; }
# ------------------------------------------------------------------------------
# Pre-flight checks & Cleanup
# ------------------------------------------------------------------------------
command -v git >/dev/null 2>&1 || die "git is not installed."
command -v rsync >/dev/null 2>&1 || die "rsync is not installed."
[[ -d "$SOURCE_DIR" ]] || die "Source directory '$SOURCE_DIR' does not exist."
cleanup() { [[ -d "$WORK_DIR" ]] && rm -rf "$WORK_DIR"; }
trap cleanup EXIT
# ------------------------------------------------------------------------------
# Clone → sync → push
# ------------------------------------------------------------------------------
info "Cloning rogerworkman1972/media-server (branch: $BRANCH)…"
git clone --depth=1 --branch "$BRANCH" "$REPO_URL" "$WORK_DIR"
info "Clearing existing tracked content…"
find "$WORK_DIR" -mindepth 1 -maxdepth 1 ! -name '.git' -exec rm -rf {} +
info "Copying $SOURCE_DIR → repo (excluding secrets and one-off scripts)…"
# CRITICAL: Exclude .env, any Python scripts (may contain API keys),
# and any files that should not be in version control.
rsync -a \
--exclude='.env' \
--exclude='.git' \
--exclude='*.pyc' \
--exclude='__pycache__' \
--exclude='radarr.py' \
--exclude='sonarr.py' \
"$SOURCE_DIR/" "$WORK_DIR/"
git -C "$WORK_DIR" add -A
if git -C "$WORK_DIR" diff --cached --quiet; then
info "No changes detected — nothing to push."
exit 0
fi
info "Changed files:"
git -C "$WORK_DIR" diff --cached --stat
git -C "$WORK_DIR" commit -m "$COMMIT_MSG"
info "Pushing to GitHub…"
git -C "$WORK_DIR" push --force origin "$BRANCH"
info "✅ /opt successfully pushed to rogerworkman1972/media-server."