Add main branch sync automation
This commit is contained in:
7
scripts/install_devone_sync_hooks.sh
Executable file
7
scripts/install_devone_sync_hooks.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
|
||||
git -C "$ROOT_DIR" config core.hooksPath .githooks
|
||||
printf 'Installed git hooks path: %s\n' "$ROOT_DIR/.githooks"
|
||||
73
scripts/sync_devone_changed_files.sh
Executable file
73
scripts/sync_devone_changed_files.sh
Executable file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
|
||||
TARGET_HOST="${SYNC_TARGET_HOST:-devone.aplikasi.web.id}"
|
||||
REMOTE_USER="${SYNC_REMOTE_USER:-one}"
|
||||
REMOTE_PATH="${SYNC_REMOTE_PATH:-/home/one/project/one/one-api-lab/}"
|
||||
PRIVATE_KEY_PATH="${SYNC_PRIVATE_KEY_PATH:-/Users/fajrihardhitamurti/id_rsa}"
|
||||
STATE_FILE="${SYNC_STATE_FILE:-$ROOT_DIR/.git/devone-last-synced-head}"
|
||||
MODE="${1:-commit}"
|
||||
|
||||
log() {
|
||||
printf '[devone-sync] %s\n' "$*"
|
||||
}
|
||||
|
||||
fail() {
|
||||
printf '[devone-sync] %s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ! git -C "$ROOT_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
branch="$(git -C "$ROOT_DIR" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
|
||||
if [[ "$branch" != "main" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
[[ -f "$PRIVATE_KEY_PATH" ]] || fail "Private key not found: $PRIVATE_KEY_PATH"
|
||||
|
||||
current_head="$(git -C "$ROOT_DIR" rev-parse HEAD)"
|
||||
if [[ -f "$STATE_FILE" ]] && [[ "$(cat "$STATE_FILE")" == "$current_head" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
tmpdir="$(mktemp -d)"
|
||||
trap 'rm -rf "$tmpdir"' EXIT
|
||||
changed_list="$tmpdir/changed-files.z"
|
||||
|
||||
case "$MODE" in
|
||||
commit)
|
||||
git -C "$ROOT_DIR" diff-tree --no-commit-id --root --diff-filter=ACMR -r -z HEAD > "$changed_list"
|
||||
;;
|
||||
merge)
|
||||
if git -C "$ROOT_DIR" rev-parse -q --verify ORIG_HEAD >/dev/null 2>&1; then
|
||||
git -C "$ROOT_DIR" diff --diff-filter=ACMR -z --name-only ORIG_HEAD HEAD > "$changed_list"
|
||||
else
|
||||
git -C "$ROOT_DIR" diff-tree --no-commit-id --root --diff-filter=ACMR -r -z HEAD > "$changed_list"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
fail "Unknown mode: $MODE"
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ ! -s "$changed_list" ]]; then
|
||||
printf '%s\n' "$current_head" > "$STATE_FILE"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
SSH_CMD="ssh -i $PRIVATE_KEY_PATH -o BatchMode=yes -o StrictHostKeyChecking=accept-new"
|
||||
destination="${REMOTE_USER}@${TARGET_HOST}:${REMOTE_PATH%/}/"
|
||||
|
||||
log "Syncing changed files to $destination"
|
||||
if rsync -az --from0 --files-from="$changed_list" --relative --info=progress2 --human-readable --exclude='.git/' --exclude='.DS_Store' -e "$SSH_CMD" "$ROOT_DIR/" "$destination"; then
|
||||
printf '%s\n' "$current_head" > "$STATE_FILE"
|
||||
log "Done: $current_head"
|
||||
else
|
||||
fail "Sync failed"
|
||||
fi
|
||||
Reference in New Issue
Block a user