#!/usr/bin/env bash # deploy-gas.sh — dipanggil n8n via SSH untuk deploy satu target GAS. # 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:-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) EXPECT_SID="1xGxSyu8wqExX_nOCvW_xZ9llRU3CqWZZN6DiOrRjXIYhWfg4QJ4DLSy6" ;; bdn) EXPECT_SID="1IJ4N4CO6SjDoDXvKvs_Pi0uVbQztdG9NDULIbKuGdF0-jkTUlH8lfrTl" ;; mts) EXPECT_SID="1GYZyk68o2dU2KLqzNzjMQYtn6tFjVGgqQiqd1duZqJao8kl7nCpZ0AgC" ;; esac "$(dirname "$0")/use-clasp.sh" "$PROFILE" # --- 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: pull gagal, lanjut dengan tree existing" 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=${SUBDIR:-}"