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

56 lines2,157 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
25set -a
26# shellcheck disable=SC1090
27. "$KEYS"
28set +a
29
30# The git store's shared secret: given, or the file the git store made.
31if [ -z "${GITSTORE_SECRET:-}" ] && [ -n "${GITSTORE_SECRET_FILE:-}" ]; then
32 for _ in $(seq 1 60); do [ -s "$GITSTORE_SECRET_FILE" ] && break; sleep 1; done
33 GITSTORE_SECRET="$(cat "$GITSTORE_SECRET_FILE")"
34 export GITSTORE_SECRET
35fi
36[ -n "${GITSTORE_SECRET:-}" ] || { echo "No GITSTORE_SECRET or GITSTORE_SECRET_FILE." >&2; exit 1; }
37
38node configs.mjs "$GENERATED"
39cd "$GENERATED"
40
41# Migrations: the same files D1 gets, applied to the SQLite files on the
42# volume. Applied ones are recorded, so this is safe on every start.
43for config in $(cat workers.txt); do
44 for database in $(node -e "for (const d of require('./$config').d1_databases ?? []) console.log(d.database_name)"); do
45 echo "Migrating $database"
46 "$WRANGLER" d1 migrations apply "$database" --local --persist-to "$STATE" -c "$config" >/dev/null
47 done
48done
49
50args=()
51while read -r config; do args+=(-c "$config"); done < workers.txt
52echo "g1t is starting on ${PUBLIC_URL:-http://localhost:8787}"
53exec "$WRANGLER" dev "${args[@]}" \
54 --ip 0.0.0.0 --port 8787 \
55 --persist-to "$STATE" \
56 --show-interactive-dev-session=false