Files
myoffice_public/proxmox/sync-public.sh
T

184 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Sync full source from the private repo to the public Gitea repo (manual build + Proxmox scripts).
# Usage:
# export GITEA_TOKEN='<token>'
# ./proxmox/sync-public.sh --gitea-url https://gitea.example.com --owner org --repo myoffice-public --push
#
# Copies tracked git files (plus generates public README.md). Skips private CI, secrets, build junk.
set -euo pipefail
script_dir="$(cd "$(dirname "$0")" && pwd)"
repo_root="$(cd "$script_dir/.." && pwd)"
cd "$repo_root"
gitea_url="${GITEA_URL:-}"
owner="${GITEA_OWNER:-}"
repo="${GITEA_REPO:-myoffice-public}"
branch="${BRANCH:-master}"
token="${GITEA_TOKEN:-}"
work_dir="${WORK_DIR:-}"
do_push=false
usage() {
echo "Usage: $0 --gitea-url URL --owner OWNER [--repo REPO] [--branch BRANCH] [--push]" >&2
exit 1
}
while [[ $# -gt 0 ]]; do
case "$1" in
--gitea-url)
gitea_url="$2"
shift 2
;;
--owner)
owner="$2"
shift 2
;;
--repo)
repo="$2"
shift 2
;;
--branch)
branch="$2"
shift 2
;;
--work-dir)
work_dir="$2"
shift 2
;;
--push)
do_push=true
shift
;;
-h | --help)
usage
;;
*)
echo "Unknown argument: $1" >&2
usage
;;
esac
done
if [[ -z "$gitea_url" || -z "$owner" ]]; then
usage
fi
gitea_url="${gitea_url%/}"
if [[ -z "$work_dir" ]]; then
work_dir="${TMPDIR:-/tmp}/myoffice-public-sync"
fi
# Paths never published (private CI, secrets, local junk).
should_skip() {
local f="$1"
case "$f" in
.gitea | .gitea/*) return 0 ;;
_Published | _Published/*) return 0 ;;
myoffice-publish.tar.gz) return 0 ;;
Docker/data | Docker/data/*) return 0 ;;
.env | .env.* | env.local) return 0 ;;
*.user | *.suo) return 0 ;;
proxmox/README.public.md) return 0 ;; # baked into public README.md
*) return 1 ;;
esac
}
if [[ -n "$token" ]]; then
host_part="${gitea_url#https://}"
host_part="${host_part#http://}"
if [[ "$gitea_url" == http://* ]]; then
scheme=http
else
scheme=https
fi
remote="${scheme}://oauth2:${token}@${host_part}/${owner}/${repo}.git"
else
remote="${gitea_url}/${owner}/${repo}.git"
echo "No token — using remote as-is (SSH URL recommended): $remote"
fi
echo "-----------------------------------------------"
echo "Sync full source -> $owner/$repo ($branch)"
echo "WorkDir: $work_dir"
echo "-----------------------------------------------"
rm -rf "$work_dir"
mkdir -p "$work_dir"
if git clone --depth 1 --branch "$branch" "$remote" "$work_dir" 2>/dev/null; then
:
else
echo "Clone failed or empty repo — initializing new git repo..."
git -C "$work_dir" init -b "$branch" 2>/dev/null || {
git -C "$work_dir" init
git -C "$work_dir" checkout -b "$branch" 2>/dev/null || true
}
git -C "$work_dir" remote remove origin 2>/dev/null || true
git -C "$work_dir" remote add origin "$remote"
fi
find "$work_dir" -mindepth 1 -maxdepth 1 ! -name '.git' -exec rm -rf {} +
copied=0
skipped=0
while IFS= read -r -d '' f; do
if should_skip "$f"; then
skipped=$((skipped + 1))
continue
fi
src="$repo_root/$f"
if [[ ! -e "$src" ]]; then
continue
fi
dest="$work_dir/$f"
mkdir -p "$(dirname "$dest")"
cp -a "$src" "$dest"
copied=$((copied + 1))
done < <(git -C "$repo_root" ls-files -z)
echo "Copied $copied tracked files (skipped $skipped)."
repo_raw_base="${gitea_url}/${owner}/${repo}/raw/branch/${branch}"
readme_template="$script_dir/README.public.md"
if [[ ! -f "$readme_template" ]]; then
echo "Missing $readme_template" >&2
exit 1
fi
sed \
-e "s|__GITEA_URL__|${gitea_url}|g" \
-e "s|__GITEA_OWNER__|${owner}|g" \
-e "s|__GITEA_REPO__|${repo}|g" \
-e "s|__GITEA_BRANCH__|${branch}|g" \
-e "s|__REPO_RAW_BASE__|${repo_raw_base}|g" \
"$readme_template" >"$work_dir/README.md"
echo "Wrote public README.md"
# Drop private-only docs that confuse public consumers (optional keep docs/)
# Keep docs/ — useful for auth notes.
git -C "$work_dir" add -A
if [[ -z "$(git -C "$work_dir" status --porcelain)" ]]; then
echo "No changes to commit."
else
git -C "$work_dir" -c user.email='myoffice-sync@local' -c user.name='myoffice-sync' \
commit -m "sync full source from private myoffice"
echo "Committed public snapshot."
fi
if [[ "$do_push" == true ]]; then
echo "Pushing to origin $branch..."
git -C "$work_dir" push -u origin "HEAD:$branch"
echo "Pushed."
else
echo "Dry run (no push). Re-run with --push to publish."
echo "Work tree left at: $work_dir"
fi
echo ""
echo "Proxmox one-liner:"
echo " bash -c \"\$(curl -fsSL ${repo_raw_base}/proxmox/myoffice.sh)\""
echo ""