11#! /usr/bin/env bash
22set -euo pipefail
33
4- # n8n workflow import + owner setup + Postgres credential
4+ # n8n workflow import + owner setup
55# Idempotent: skips if already configured
6- # Google Sheets credential must be set up manually in n8n UI
6+ # Credentials (Postgres, Google Sheets) must be set up manually in n8n UI
77
88COMPOSE=" sudo docker compose -p bcsd-app --env-file .env -f infra/docker/docker-compose.yml"
99MAX_RETRIES=15
@@ -28,17 +28,6 @@ workflow_exists() {
2828 [ " $count " -gt 0 ]
2929}
3030
31- import_workflow () {
32- local file=" $1 "
33- local name=" $2 "
34- if workflow_exists " $name " ; then
35- echo " Workflow '$name ' already exists — skipping"
36- return 0
37- fi
38- echo " Importing '$name '..."
39- $COMPOSE exec -T n8n n8n import:workflow --input=" $file " 2>&1 || true
40- }
41-
4231delete_workflow () {
4332 local name=" $1 "
4433 if ! workflow_exists " $name " ; then
@@ -76,36 +65,6 @@ print('setup' if d.get('userManagement',{}).get('showSetupOnFirstLoad') else 'do
7665 echo " Owner created: ${N8N_AUTH_USER} @bcsdlab.com"
7766}
7867
79- setup_pg_credential () {
80- local existing
81- existing=$( $COMPOSE exec -T n8n n8n list:credential 2>&1 | grep -c " BCSD PostgreSQL" || true)
82- if [ " $existing " -gt 0 ]; then
83- echo " Postgres credential already exists — skipping"
84- return 0
85- fi
86- echo " Creating Postgres credential from .env..."
87- set -a; source .env; set +a
88- local cred_json
89- cred_json=$( python3 -c "
90- import json, sys, uuid
91- cred = [{
92- 'id': str(uuid.uuid4()),
93- 'name': 'BCSD PostgreSQL',
94- 'type': 'postgres',
95- 'data': {
96- 'host': '${POSTGRES_HOST:- postgres} ',
97- 'port': ${POSTGRES_PORT:- 5432} ,
98- 'database': '${POSTGRES_DB} ',
99- 'user': '${POSTGRES_USER} ',
100- 'password': '${POSTGRES_PASSWORD} ',
101- 'ssl': 'disable'
102- }
103- }]
104- json.dump(cred, sys.stdout)
105- " )
106- echo " $cred_json " | $COMPOSE exec -T n8n n8n import:credentials --input=/dev/stdin
107- }
108-
10968echo " === n8n Init ==="
11069
11170echo " 1. Starting n8n..."
12180echo " 3. Setting up owner account..."
12281setup_owner
12382
124- echo " 4. Setting up Postgres credential..."
125- setup_pg_credential
126-
127- echo " 5. Replacing workflows..."
83+ echo " 4. Replacing workflows..."
12884delete_workflow " Link Auto-Expiration (hourly)"
12985delete_workflow " Link Auto-Expiration (1min)"
13086delete_workflow " PG → Sheets Sync (5min)"
13187
132- echo " 6 . Importing workflows..."
88+ echo " 5 . Importing workflows..."
13389$COMPOSE exec -T n8n n8n import:workflow --input=" /workflows/pg_sheets_sync.json" 2>&1
13490$COMPOSE exec -T n8n n8n import:workflow --input=" /workflows/link_auto_expire.json" 2>&1
13591
13692echo " === n8n Init complete ==="
137- echo " NOTE: Set up Google Sheets credential in n8n UI if first deploy"
93+ echo " NOTE: Set up Postgres + Google Sheets credentials in n8n UI if first deploy"
0 commit comments