11#! /usr/bin/env bash
22set -euo pipefail
33
4- # n8n workflow import + Google Sheets credential setup
5- # Idempotent: skips if workflows already exist
4+ # n8n workflow import + owner setup
5+ # Idempotent: skips if already configured
6+ # Credentials (Postgres, Google Sheets) must be set up manually in n8n UI
67
78COMPOSE=" sudo docker compose -p bcsd-app --env-file .env -f infra/docker/docker-compose.yml"
8- N8N_CONTAINER=" bcsd-app-n8n-1"
99MAX_RETRIES=15
1010
1111wait_n8n () {
@@ -24,7 +24,7 @@ wait_n8n() {
2424workflow_exists () {
2525 local name=" $1 "
2626 local count
27- count=$( $COMPOSE exec -T n8n n8n list:workflow 2> /dev/null | grep -c " $name " || true)
27+ count=$( $COMPOSE exec -T n8n n8n list:workflow 2>&1 | grep -c " $name " || true)
2828 [ " $count " -gt 0 ]
2929}
3030
@@ -39,45 +39,6 @@ import_workflow() {
3939 $COMPOSE exec -T n8n n8n import:workflow --input=" $file "
4040}
4141
42- activate_workflow () {
43- local name=" $1 "
44- local wf_id
45- wf_id=$( $COMPOSE exec -T n8n n8n list:workflow 2> /dev/null | grep " $name " | awk -F' |' ' {print $1}' )
46- if [ -z " $wf_id " ]; then
47- echo " WARNING: Could not find workflow '$name ' to activate"
48- return 1
49- fi
50- $COMPOSE exec -T n8n n8n publish:workflow --id=" $wf_id "
51- echo " Activated '$name ' (id: $wf_id )"
52- }
53-
54- setup_credential () {
55- local existing
56- existing=$( $COMPOSE exec -T n8n n8n list:credential 2>&1 | grep -c " Google Sheets SA" || true)
57- if [ " $existing " -gt 0 ]; then
58- echo " Google Sheets credential already exists ($existing found) — skipping"
59- return 0
60- fi
61- echo " Creating Google Sheets service account credential..."
62- local cred_json
63- cred_json=$( python3 -c "
64- import json, sys
65- sa = json.load(open('$GOOGLE_SERVICE_ACCOUNT_FILE '))
66- cred = [{
67- 'id': str(__import__('uuid').uuid4()),
68- 'name': 'Google Sheets SA',
69- 'type': 'googleApi',
70- 'data': {
71- 'email': sa['client_email'],
72- 'privateKey': sa['private_key'],
73- 'impersonateUser': ''
74- }
75- }]
76- json.dump(cred, sys.stdout)
77- " )
78- echo " $cred_json " | $COMPOSE exec -T n8n n8n import:credentials --input=/dev/stdin
79- }
80-
8142setup_owner () {
8243 set -a; source .env; set +a
8344 local port
@@ -123,12 +84,5 @@ echo "4. Importing workflows..."
12384import_workflow " /workflows/pg_sheets_sync.json" " PG → Sheets Sync (5min)"
12485import_workflow " /workflows/link_auto_expire.json" " Link Auto-Expiration (hourly)"
12586
126- echo " 5. Setting up Google Sheets credential..."
127- set -a; source .env; set +a
128- setup_credential
129-
130- echo " 6. Activating workflows..."
131- activate_workflow " PG → Sheets Sync"
132- activate_workflow " Link Auto-Expiration"
133-
13487echo " === n8n Init complete ==="
88+ echo " NOTE: Set up Postgres + Google Sheets credentials in n8n UI if first deploy"
0 commit comments