|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -euo pipefail |
3 | 3 |
|
4 | | -# n8n workflow import + owner setup |
| 4 | +# n8n workflow import + owner setup + Postgres credential |
5 | 5 | # Idempotent: skips if already configured |
6 | | -# Credentials (Postgres, Google Sheets) must be set up manually in n8n UI |
| 6 | +# Google Sheets credential must be set up manually in n8n UI |
7 | 7 |
|
8 | 8 | COMPOSE="sudo docker compose -p bcsd-app --env-file .env -f infra/docker/docker-compose.yml" |
9 | 9 | MAX_RETRIES=15 |
@@ -65,6 +65,36 @@ print('setup' if d.get('userManagement',{}).get('showSetupOnFirstLoad') else 'do |
65 | 65 | echo " Owner created: ${N8N_AUTH_USER}@bcsdlab.com" |
66 | 66 | } |
67 | 67 |
|
| 68 | +setup_pg_credential() { |
| 69 | + local existing |
| 70 | + existing=$($COMPOSE exec -T n8n n8n list:credential 2>&1 | grep -c "BCSD PostgreSQL" || true) |
| 71 | + if [ "$existing" -gt 0 ]; then |
| 72 | + echo " Postgres credential already exists — skipping" |
| 73 | + return 0 |
| 74 | + fi |
| 75 | + echo " Creating Postgres credential from .env..." |
| 76 | + set -a; source .env; set +a |
| 77 | + local cred_json |
| 78 | + cred_json=$(python3 -c " |
| 79 | +import json, sys, uuid |
| 80 | +cred = [{ |
| 81 | + 'id': str(uuid.uuid4()), |
| 82 | + 'name': 'BCSD PostgreSQL', |
| 83 | + 'type': 'postgres', |
| 84 | + 'data': { |
| 85 | + 'host': '${POSTGRES_HOST:-postgres}', |
| 86 | + 'port': ${POSTGRES_PORT:-5432}, |
| 87 | + 'database': '${POSTGRES_DB}', |
| 88 | + 'user': '${POSTGRES_USER}', |
| 89 | + 'password': '${POSTGRES_PASSWORD}', |
| 90 | + 'ssl': 'disable' |
| 91 | + } |
| 92 | +}] |
| 93 | +json.dump(cred, sys.stdout) |
| 94 | +") |
| 95 | + echo "$cred_json" | $COMPOSE exec -T n8n n8n import:credentials --input=/dev/stdin |
| 96 | +} |
| 97 | + |
68 | 98 | echo "=== n8n Init ===" |
69 | 99 |
|
70 | 100 | echo "1. Starting n8n..." |
|
80 | 110 | echo "3. Setting up owner account..." |
81 | 111 | setup_owner |
82 | 112 |
|
83 | | -echo "4. Importing workflows..." |
| 113 | +echo "4. Setting up Postgres credential..." |
| 114 | +setup_pg_credential |
| 115 | + |
| 116 | +echo "5. Importing workflows..." |
84 | 117 | import_workflow "/workflows/pg_sheets_sync.json" "PG → Sheets Sync (5min)" |
85 | 118 | import_workflow "/workflows/link_auto_expire.json" "Link Auto-Expiration (hourly)" |
86 | 119 |
|
87 | 120 | echo "=== n8n Init complete ===" |
88 | | -echo "NOTE: Set up Postgres + Google Sheets credentials in n8n UI if first deploy" |
| 121 | +echo "NOTE: Set up Google Sheets credential in n8n UI if first deploy" |
0 commit comments