deploy-gas.sh: mapping enforcement + backup/restore .clasp.json + tanpa build untuk projects/*
Lint / lint (push) Has been cancelled

This commit is contained in:
player
2026-10-01 09:07:45 +07:00
parent a801f5f0e6
commit 617055c47d
+78 -19
View File
@@ -1,42 +1,101 @@
#!/usr/bin/env bash
# deploy-gas.sh — dipanggil n8n via SSH untuk deploy satu target GAS.
# Usage: ./deploy-gas.sh <dev|bdn|mts> [project-subdir]
# profile dev/bdn/mts = akun clasp + file .clasp.<profile>.json di repo root
# project-subdir = opsional, mis. projects/mts-alamin-2025 (punya .clasp.json sendiri)
# Usage: ./deploy-gas.sh [profile] [subdir]
#
# Mapping yang diizinkan (enforcement lapis-2; lapis-1 ada di workflow n8n):
# dev + "" -> root repo (template React+Vite: build + push)
# bdn + projects/danin -> push langsung (dist klasik dari clasp pull)
# mts + projects/mts-alamin-2025 -> push langsung (dist klasik dari clasp pull)
#
# Project projects/* TIDAK di-build: dist/ mereka adalah hasil clasp pull
# (file .gs/.html klasik), bukan output vite. Menjalankan yarn build untuk
# mereka tidak merusak (output ke root dist/) tapi sia-sia, jadi dilewati.
set -euo pipefail
PROFILE="${1:-}"
SUBDIR="${2:-}"
PROFILE="${1:-mts}"
SUBDIR="${2:-projects/mts-alamin-2025}"
REPO="$HOME/gas-react-sheets"
# --- Lapis-2a: allowlist pasangan profil|target ---
case "$PROFILE|$SUBDIR" in
"dev|"|"bdn|projects/danin"|"mts|projects/mts-alamin-2025") ;;
*) echo "ERROR: pasangan profil|target tidak diizinkan: $PROFILE|$SUBDIR"; exit 1 ;;
esac
# --- Lapis-2b: cegah path traversal ---
case "$SUBDIR" in
""|"projects/"*) ;;
*) echo "ERROR: subdir di luar projects/: $SUBDIR"; exit 1 ;;
esac
# ScriptId yang diharapkan per profil (deteksi salah-pasangan sejak dini)
case "$PROFILE" in
dev|bdn|mts) ;;
*) echo "Usage: $0 <dev|bdn|mts> [project-subdir]"; exit 1 ;;
dev) EXPECT_SID="1xGxSyu8wqExX_nOCvW_xZ9llRU3CqWZZN6DiOrRjXIYhWfg4QJ4DLSy6" ;;
bdn) EXPECT_SID="1IJ4N4CO6SjDoDXvKvs_Pi0uVbQztdG9NDULIbKuGdF0-jkTUlH8lfrTl" ;;
mts) EXPECT_SID="1GYZyk68o2dU2KLqzNzjMQYtn6tFjVGgqQiqd1duZqJao8kl7nCpZ0AgC" ;;
esac
"$(dirname "$0")/use-clasp.sh" "$PROFILE"
# Pilih file .clasp.json sesuai profil bila ada (pola repo ini: .clasp.<profil>.json)
if [[ -f "$REPO/.clasp.$PROFILE.json" ]]; then
cp -f "$REPO/.clasp.$PROFILE.json" "$REPO/.clasp.json"
fi
# --- Backup file .clasp.json yang akan disentuh, kembalikan saat keluar ---
# (Pelajaran 1 Okt 2026: copy tanpa restore menukar-nukar isi file antar deploy.)
declare -a RESTORE_QUEUE=()
backup_file() {
local f="$1" b
if [[ -f "$f" ]]; then
b=$(mktemp)
cp -f "$f" "$b"
RESTORE_QUEUE+=("$f|$b")
fi
}
restore_all() {
if ((${#RESTORE_QUEUE[@]})); then
local item f b
for item in "${RESTORE_QUEUE[@]}"; do
f="${item%%|*}"; b="${item##*|}"
cp -f "$b" "$f"; rm -f "$b"
done
fi
}
trap restore_all EXIT
script_id_of() { python3 -c "import json;print(json.load(open('$1'))['scriptId'])" 2>/dev/null || echo "UNREADABLE"; }
cd "$REPO"
echo "--- git pull ---"
git pull --ff-only || echo "WARN: bukan git repo / pull gagal, lanjut dengan tree existing"
echo "--- yarn install ---"
yarn install --frozen-lockfile
echo "--- build ---"
yarn build
git pull --ff-only || echo "WARN: pull gagal, lanjut dengan tree existing"
TARGET="$REPO"
if [[ -n "$SUBDIR" ]]; then
if [[ -z "$SUBDIR" ]]; then
# --- Target root (dev): build + push ---
backup_file "$REPO/.clasp.json"
if [[ -f "$REPO/.clasp.$PROFILE.json" ]]; then
if ! cmp -s "$REPO/.clasp.$PROFILE.json" "$REPO/.clasp.json"; then
cp -f "$REPO/.clasp.$PROFILE.json" "$REPO/.clasp.json"
echo "INFO: .clasp.json diset dari .clasp.$PROFILE.json (dikembalikan saat exit)"
fi
fi
if [[ "$(script_id_of "$REPO/.clasp.json")" != "$EXPECT_SID" ]]; then
echo "ERROR: scriptId root tidak cocok untuk profil $PROFILE"; exit 1
fi
echo "--- yarn install ---"
yarn install --frozen-lockfile
echo "--- build ---"
yarn build
TARGET="$REPO"
else
# --- Target projects/* : push langsung, tanpa build ---
TARGET="$REPO/$SUBDIR"
if [[ ! -d "$TARGET" ]]; then
echo "ERROR: subdir tidak ada: $TARGET"; exit 1
fi
if [[ ! -f "$TARGET/.clasp.json" ]]; then
echo "ERROR: $TARGET/.clasp.json tidak ada"; exit 1
fi
if [[ "$(script_id_of "$TARGET/.clasp.json")" != "$EXPECT_SID" ]]; then
echo "ERROR: scriptId $SUBDIR tidak cocok untuk profil $PROFILE"; exit 1
fi
fi
cd "$TARGET"
echo "--- clasp push ($TARGET, profil $PROFILE) ---"
clasp push
echo "DEPLOY OK: profil=$PROFILE target=$TARGET"
echo "DEPLOY OK: profil=$PROFILE target=${SUBDIR:-<root>}"