flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/deploy/self-host/start.sh

61 lines2,414 bytesCodeBlame
1#!/usr/bin/env bash
2# Starts self-hosted g1t inside its container: makes the keys it needs once,
3# writes the Wrangler configs, brings every database up to date, and runs
4# every Worker in one workerd on :8787.
5set -euo pipefail
6
7cd "$(dirname "$0")"
8DATA="${G1T_DATA:-/data}"
9STATE="$DATA/state"
10KEYS="$DATA/keys.env"
11GENERATED="$DATA/generated"
12WRANGLER="$(cd ../.. && pwd)/node_modules/.bin/wrangler"
13mkdir -p "$STATE"
14
15# Keys that seal secrets at rest (actions, integrations, webhooks). Made on
16# first start and kept on the volume: losing them loses those secrets.
17if [ ! -f "$KEYS" ]; then
18 umask 077
19 {
20 echo "ACTIONS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')"
21 echo "INTEGRATIONS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')"
22 echo "WEBHOOKS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')"
23 } > "$KEYS"
24fi
25# Identity's key seals the GitHub tokens of linked accounts; added to keys
26# made before it existed.
27if ! grep -q '^IDENTITY_KEY=' "$KEYS"; then
28 echo "IDENTITY_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" >> "$KEYS"
29fi
30set -a
31# shellcheck disable=SC1090
32. "$KEYS"
33set +a
34
35# The git store's shared secret: given, or the file the git store made.
36if [ -z "${GITSTORE_SECRET:-}" ] && [ -n "${GITSTORE_SECRET_FILE:-}" ]; then
37 for _ in $(seq 1 60); do [ -s "$GITSTORE_SECRET_FILE" ] && break; sleep 1; done
38 GITSTORE_SECRET="$(cat "$GITSTORE_SECRET_FILE")"
39 export GITSTORE_SECRET
40fi
41[ -n "${GITSTORE_SECRET:-}" ] || { echo "No GITSTORE_SECRET or GITSTORE_SECRET_FILE." >&2; exit 1; }
42
43node configs.mjs "$GENERATED"
44cd "$GENERATED"
45
46# Migrations: the same files D1 gets, applied to the SQLite files on the
47# volume. Applied ones are recorded, so this is safe on every start.
48for config in $(cat workers.txt); do
49 for database in $(node -e "for (const d of require('./$config').d1_databases ?? []) console.log(d.database_name)"); do
50 echo "Migrating $database"
51 "$WRANGLER" d1 migrations apply "$database" --local --persist-to "$STATE" -c "$config" >/dev/null
52 done
53done
54
55args=()
56while read -r config; do args+=(-c "$config"); done < workers.txt
57echo "g1t is starting on ${PUBLIC_URL:-http://localhost:8787}"
58exec "$WRANGLER" dev "${args[@]}" \
59 --ip 0.0.0.0 --port 8787 \
60 --persist-to "$STATE" \
61 --show-interactive-dev-session=false