Skip to content

Commit b7c9e95

Browse files
ImTotemclaude
andcommitted
refactor(infra): first-deploy-only workflow import + versioning guide
- init_n8n.sh: skip import if workflow already exists in n8n volume - Remove --force-recreate (preserves n8n state + credential links) - Add workflows/README.md with versioning rules and recovery guide Workflow lifecycle: n8n UI (edit) → export → commit (version control) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a33bb1f commit b7c9e95

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

infra/scripts/init_n8n.sh

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
# n8n workflow import + owner setup
5-
# Credentials (Postgres, Google Sheets) must be set up manually in n8n UI
4+
# n8n init: owner setup + first-time workflow import only
5+
# Workflows persist in n8n volume — reimport skipped if already exist
6+
# Credentials set up manually in n8n UI on first deploy
7+
# Workflow changes: edit in n8n UI, then export to workflows/*.json for versioning
68

79
COMPOSE="sudo docker compose -p bcsd-app --env-file .env -f infra/docker/docker-compose.yml"
810
MAX_RETRIES=15
@@ -20,6 +22,11 @@ wait_n8n() {
2022
return 1
2123
}
2224

25+
workflow_exists() {
26+
local id="$1"
27+
$COMPOSE exec -T n8n n8n list:workflow 2>&1 | grep -q "$id"
28+
}
29+
2330
setup_owner() {
2431
set -a; source .env; set +a
2532
local port
@@ -46,10 +53,22 @@ print('setup' if d.get('userManagement',{}).get('showSetupOnFirstLoad') else 'do
4653
echo " Owner created: ${N8N_AUTH_USER}@bcsdlab.com"
4754
}
4855

56+
import_if_new() {
57+
local file="$1"
58+
local id="$2"
59+
if workflow_exists "$id"; then
60+
echo " $id — already exists, skipping (edit in n8n UI)"
61+
return 0
62+
fi
63+
echo " $id — first import"
64+
$COMPOSE exec -T n8n n8n import:workflow --input="$file" 2>&1
65+
$COMPOSE exec -T n8n n8n publish:workflow --id="$id" 2>&1 || true
66+
}
67+
4968
echo "=== n8n Init ==="
5069

5170
echo "1. Starting n8n..."
52-
$COMPOSE up -d --force-recreate n8n
71+
$COMPOSE up -d n8n
5372

5473
echo "2. Waiting for n8n..."
5574
if ! wait_n8n; then
@@ -61,13 +80,8 @@ fi
6180
echo "3. Setting up owner account..."
6281
setup_owner
6382

64-
echo "4. Importing workflows (overwrites by ID)..."
65-
$COMPOSE exec -T n8n n8n import:workflow --input="/workflows/pg_sheets_sync.json" 2>&1
66-
$COMPOSE exec -T n8n n8n import:workflow --input="/workflows/link_auto_expire.json" 2>&1
67-
68-
echo "5. Publishing workflows..."
69-
$COMPOSE exec -T n8n n8n publish:workflow --id="pg-sheets-sync" 2>&1 || true
70-
$COMPOSE exec -T n8n n8n publish:workflow --id="link-auto-expire" 2>&1 || true
83+
echo "4. Importing workflows (first deploy only)..."
84+
import_if_new "/workflows/pg_sheets_sync.json" "pg-sheets-sync"
85+
import_if_new "/workflows/link_auto_expire.json" "link-auto-expire"
7186

7287
echo "=== n8n Init complete ==="
73-
echo "NOTE: Set up Postgres + Google Sheets credentials in n8n UI if first deploy"

workflows/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# n8n Workflow 관리 규칙
2+
3+
## 원칙
4+
5+
- **n8n UI가 source of truth** — 실제 실행되는 워크플로우는 n8n 볼륨에 저장
6+
- **이 디렉토리는 버전 관리용 백업** — Git에서 변경 이력 추적
7+
- **배포 시 자동 import는 첫 배포만** — 이후에는 n8n 볼륨 상태 유지
8+
9+
## 워크플로우 수정 절차
10+
11+
1. **n8n UI**에서 워크플로우 수정 및 테스트
12+
2. 동작 확인 후 **n8n UI에서 export** (Workflow → `...` → Download)
13+
3. 다운받은 JSON으로 `workflows/*.json` 업데이트
14+
4. **커밋 메시지**에 변경 내용 기록: `docs(workflow): {변경 내용}`
15+
16+
## Export 시 주의사항
17+
18+
- n8n export에 credential 데이터는 포함되지 않음 (ID만 포함)
19+
- credential ID는 환경마다 다르므로 **커밋 전 ID를 빈 문자열로 교체**:
20+
```json
21+
"credentials": {"postgres": {"id": "", "name": "BCSD PostgreSQL"}}
22+
```
23+
- 이렇게 해야 다른 환경에서 첫 배포 시 credential 수동 연결만 하면 됨
24+
25+
## 파일 목록
26+
27+
| 파일 | 워크플로우 ID | 설명 |
28+
|------|--------------|------|
29+
| `pg_sheets_sync.json` | `pg-sheets-sync` | PG → Google Sheets 동기화 (5분) |
30+
| `link_auto_expire.json` | `link-auto-expire` | 만료 링크 자동 처리 (1분) |
31+
32+
## Credential 관리
33+
34+
- n8n 볼륨(`n8n-data`)에 암호화 저장, `N8N_ENCRYPTION_KEY`로 보호
35+
- 첫 배포 시 n8n UI에서 수동 생성:
36+
- **BCSD PostgreSQL**: host=`postgres`, port=`5432`, .env의 DB 정보
37+
- **Google Sheets SA**: Service Account, bcsd-credentials의 SA JSON 사용
38+
- 이후 배포에서는 볼륨에 보존되므로 재설정 불필요
39+
40+
## 볼륨 유실 시 복구
41+
42+
1. `n8n import:credentials` — bcsd-credentials에서 export한 파일로 복구
43+
2. `n8n import:workflow` — 이 디렉토리의 JSON 파일로 복구
44+
3. n8n UI에서 각 워크플로우의 credential 재연결
45+
4. 워크플로우 Publish

0 commit comments

Comments
 (0)