Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build/
.clasp.json
.clasp-dev.json
.clasp-prod.json
.clasp-sandbox.json
*.tgz
.vscode/
coverage/
Expand Down
2 changes: 2 additions & 0 deletions .templatesyncignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

# Scripts
:!scripts/init.sh
:!scripts/init-sandbox.sh
:!scripts/sandbox-push.sh
:!scripts/set-properties.sh
:!scripts/gcp-get-secret.sh
:!scripts/fetch-clasp-credentials.sh
Expand Down
20 changes: 20 additions & 0 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ pnpm run check # lint + 型チェック + テスト

clasp の認証と dev へのデプロイは上記セットアップガイドを参照してください。

#### 個人 sandbox(CI-only モード)

共有デプロイ ID を CI/CD 専用に制限する組織(「CI-only モード」)では、開発者は自分の Google アカウント配下の **個人 sandbox** プロジェクトで手元開発を行います:

```bash
pnpm exec clasp login # 自分のアカウントで認証
./scripts/init-sandbox.sh # sandbox プロジェクトを作成(.clasp-sandbox.json、gitignore 済み)
pnpm run sandbox # check + build + 自分の sandbox に push
```

`clasp` のサブコマンドは `-P` で sandbox に対して実行できます:

```bash
pnpm exec clasp -P .clasp-sandbox.json open-script
pnpm exec clasp -P .clasp-sandbox.json run <fn>
pnpm exec clasp -P .clasp-sandbox.json logs
```

sandbox は個人の権限で実行されます。デプロイ ID との権限差は再現しないため、最終確認は dev 環境(`dev` への merge)で行ってください。環境変数 `FLEET_DEPLOY_ACCOUNT=<共有アカウントの email>` を設定すると、clasp が共有デプロイアカウントで認証されている間は sandbox コマンドが停止します。

## CI/CD パイプライン

GitHub Actions と GitLab CI の両方の設定が含まれています。push 先のプラットフォームで同じパイプラインが動きます。CI/CD 変数の設定以外の追加セットアップは不要です。
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ pnpm run check # lint + typecheck + test

For clasp authentication and deploying to dev, see the setup guides above.

#### Personal Sandbox (CI-only mode)

In organizations where the shared deploy identity is restricted to CI/CD ("CI-only mode"), developers iterate locally against a **personal sandbox** — an Apps Script project under their own Google account:

```bash
pnpm exec clasp login # authenticate with YOUR account
./scripts/init-sandbox.sh # create the sandbox project (.clasp-sandbox.json, gitignored)
pnpm run sandbox # check + build + push to YOUR sandbox
```

`clasp` subcommands work against the sandbox via `-P`:

```bash
pnpm exec clasp -P .clasp-sandbox.json open-script
pnpm exec clasp -P .clasp-sandbox.json run <fn>
pnpm exec clasp -P .clasp-sandbox.json logs
```

The sandbox runs with your personal permissions — it does not reproduce the deploy identity's access, so final verification still happens in the dev environment (merge to `dev`). Optionally set `FLEET_DEPLOY_ACCOUNT=<shared-account email>` so sandbox commands refuse to run while clasp is authenticated as the shared deploy account.

## CI/CD Pipeline

Both GitHub Actions and GitLab CI configurations are included. The same pipeline runs on whichever platform you push to — no additional setup needed beyond CI/CD variables.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"lint:css": "stylelint '**/*.{css,scss,html}' --allow-empty-input",
"lint:html": "htmlhint --ignore 'coverage/**,dist/**' '**/*.html' || true",
"prepare": "husky",
"sandbox": "./scripts/sandbox-push.sh",
"test": "jest test/ --passWithNoTests --detectOpenHandles --coverage",
"typecheck": "tsc --noEmit"
},
Expand Down
133 changes: 133 additions & 0 deletions scripts/init-sandbox.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env bash
set -euo pipefail

# Apps Script Fleet — Personal Sandbox Init
#
# Creates a personal Apps Script project under YOUR OWN Google account and
# writes .clasp-sandbox.json (gitignored). For organizations running CI-only
# mode, this is the standard way to iterate locally without the shared deploy
# identity. It does NOT set CI/CD variables and does NOT bind a GCP project.
#
# Prerequisites:
# - clasp authenticated with your PERSONAL account:
# pnpm exec clasp login
# - Node.js + pnpm installed
#
# Usage:
# ./scripts/init-sandbox.sh [--title "Project Name"] [--type standalone|sheets|docs|slides|forms]
#
# Optional guard:
# FLEET_DEPLOY_ACCOUNT=<shared-deploy-account email>
# When set, this script refuses to run while clasp is authenticated as the
# shared deploy account (prevents sandbox projects being created under the
# shared identity).

TITLE=""
SCRIPT_TYPE="standalone"
VALID_TYPES="standalone sheets docs slides forms"

while [[ $# -gt 0 ]]; do
case $1 in
--title)
TITLE="$2"
shift 2
;;
--type)
SCRIPT_TYPE="$2"
shift 2
;;
*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
done

die() {
echo "Error: $*" >&2
exit 1
}

require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "$1 is required but not installed."
}

validate_type() {
local type="$1"
for valid in $VALID_TYPES; do
[[ "$type" == "$valid" ]] && return 0
done
die "Invalid --type '${type}'. Must be one of: ${VALID_TYPES}"
}

json_value() {
node -e "process.stdout.write(String(JSON.parse(require('fs').readFileSync(process.argv[1],'utf8'))[process.argv[2]]))" "$1" "$2"
}

lower() {
tr '[:upper:]' '[:lower:]'
}

guard_not_shared_account() {
[[ -n "${FLEET_DEPLOY_ACCOUNT:-}" ]] || return 0

local info email
if ! info=$(pnpm exec clasp show-authorized-user --json 2>/dev/null); then
echo "Warning: could not determine the current clasp account; FLEET_DEPLOY_ACCOUNT guard skipped." >&2
return 0
fi
email=$(printf '%s' "$info" | node -e "const d=JSON.parse(require('fs').readFileSync(0,'utf8'));process.stdout.write(String(d.email||''))")
if [[ -z "$email" ]]; then
echo "Warning: clasp did not report an account email; FLEET_DEPLOY_ACCOUNT guard skipped." >&2
return 0
fi
if [[ "$(printf '%s' "$email" | lower)" == "$(printf '%s' "$FLEET_DEPLOY_ACCOUNT" | lower)" ]]; then
die "clasp is authenticated as the shared deploy account (${email}).
Sandbox commands must use your PERSONAL account. Re-authenticate with:
pnpm exec clasp login"
fi
}

echo "=== Apps Script Fleet — Personal Sandbox Init ==="
echo ""

require_cmd pnpm
require_cmd node
validate_type "$SCRIPT_TYPE"
[[ -f "$HOME/.clasprc.json" ]] || die "$HOME/.clasprc.json not found. Authenticate with your personal account: pnpm exec clasp login"
[[ -f .clasp-sandbox.json ]] && die ".clasp-sandbox.json already exists. Remove it first if you want to create a new sandbox project."

guard_not_shared_account

if [[ -z "$TITLE" ]]; then
TITLE=$(basename "$(pwd)")
fi

echo "Title: ${TITLE} (sandbox)"
echo "Type: ${SCRIPT_TYPE}"
echo ""

echo "Creating personal Apps Script project..."
rm -f .clasp.json
pnpm exec clasp create --title "${TITLE} (sandbox)" --type "$SCRIPT_TYPE" --rootDir dist 2>&1

[[ -f .clasp.json ]] || die "clasp create failed — .clasp.json not generated."
mv .clasp.json .clasp-sandbox.json

SCRIPT_ID=$(json_value .clasp-sandbox.json scriptId)
echo " Script ID: ${SCRIPT_ID}"

mkdir -p dist
cp appsscript.json dist/appsscript.json
echo " Pushing initial manifest..."
pnpm exec clasp -P .clasp-sandbox.json push -f 2>&1

echo ""
echo "Done! Personal sandbox is ready (.clasp-sandbox.json, gitignored)."
echo ""
echo " sandbox script: https://script.google.com/d/${SCRIPT_ID}/edit"
echo ""
echo " Push your code: pnpm run sandbox"
echo " Open the editor: pnpm exec clasp -P .clasp-sandbox.json open-script"
echo " Run a function: pnpm exec clasp -P .clasp-sandbox.json run <fn>"
echo " Tail logs: pnpm exec clasp -P .clasp-sandbox.json logs"
54 changes: 54 additions & 0 deletions scripts/sandbox-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
set -euo pipefail

# Apps Script Fleet — Sandbox Push
#
# Builds and pushes to YOUR personal sandbox project (.clasp-sandbox.json,
# created by ./scripts/init-sandbox.sh). Never touches the shared dev/prod
# projects and never reads .clasp-dev.json / .clasp-prod.json.
#
# Usage:
# pnpm run sandbox (wired in package.json)
# ./scripts/sandbox-push.sh
#
# Optional guard:
# FLEET_DEPLOY_ACCOUNT=<shared-deploy-account email>
# When set, the push stops if clasp is authenticated as the shared deploy
# account, so experimental code never runs with the shared identity.

die() {
echo "Error: $*" >&2
exit 1
}

lower() {
tr '[:upper:]' '[:lower:]'
}

guard_not_shared_account() {
[[ -n "${FLEET_DEPLOY_ACCOUNT:-}" ]] || return 0

local info email
if ! info=$(pnpm exec clasp show-authorized-user --json 2>/dev/null); then
echo "Warning: could not determine the current clasp account; FLEET_DEPLOY_ACCOUNT guard skipped." >&2
return 0
fi
email=$(printf '%s' "$info" | node -e "const d=JSON.parse(require('fs').readFileSync(0,'utf8'));process.stdout.write(String(d.email||''))")
if [[ -z "$email" ]]; then
echo "Warning: clasp did not report an account email; FLEET_DEPLOY_ACCOUNT guard skipped." >&2
return 0
fi
if [[ "$(printf '%s' "$email" | lower)" == "$(printf '%s' "$FLEET_DEPLOY_ACCOUNT" | lower)" ]]; then
die "clasp is authenticated as the shared deploy account (${email}).
Sandbox commands must use your PERSONAL account. Re-authenticate with:
pnpm exec clasp login"
fi
}

[[ -f .clasp-sandbox.json ]] || die ".clasp-sandbox.json not found. Create your sandbox first: ./scripts/init-sandbox.sh"

guard_not_shared_account

pnpm run check
pnpm run build
pnpm exec clasp -P .clasp-sandbox.json push -f
Loading