Files
alist-serve-resource/n8n/SETUP.md
T

71 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# n8n Setup — Environment Variables
W1/W2 通过**一个 in-flow 的 "Load Env" Code 节点**(webhook 之后第 2 步)读取全部 env,
下游所有节点引用 `$('Load Env').item.json.VAR`。secrets 不硬编码进 workflow。
## 读取方式(已实测确认)
- **Code 节点内**:`$env.VAR` 可用(例 `$env.NOCODB_URL`)。✔
- **HTTP 节点表达式字段**(url / header / body expression):`$env` 和 `process.env` **都不可用**。✘
- 所以用**一个 Code 节点 "Load Env"** 在 `$env` 里调取全部 env 输出到 `$json`,
下游 HTTP 节点用 `{{ $('Load Env').item.json.VAR }}` 取值。
> 关键点:
> - Load Env **必须接在 webhook flow 里(第 2 步)**,不在 flow 里的 Code 节点不会执行。
> - Load Env **必须透传传入的字段**(`customerId` / `customerProductId`),否则下游丢数据。
### Load Env 模板(实测可用)
```js
// Step 2: read ALL env via $env (works in Code node), and pass through
// the webhook-derived fields so downstream keeps customerId.
const prev = $input.item.json;
return { json: {
customerId: prev.customerId,
eventType: prev.eventType,
NOCODB_URL: $env.NOCODB_URL,
NOCODB_TOKEN: $env.NOCODB_TOKEN,
T_CUSTOMERS: $env.NOCODB_TABLE_CUSTOMERS,
ALIST_URL: $env.ALIST_URL,
ALIST_ADMIN_USERNAME: $env.ALIST_ADMIN_USERNAME,
ALIST_ADMIN_PASSWORD: $env.ALIST_ADMIN_PASSWORD,
ALIST_ADMIN_OTP: $env.ALIST_ADMIN_OTP || '',
} };
```
## 需要配置的 env(n8n 容器环境变量)
```
NOCODB_URL=https://nocodb.hoelee.com
NOCODB_TOKEN=<workspace token>
NOCODB_TABLE_CUSTOMERS=maqb9zgvza2te3k
NOCODB_TABLE_PRODUCTS=molyb3pxv81l75g
NOCODB_TABLE_RESOURCES=m6cm7tkxzd8m18c
NOCODB_TABLE_CUSTOMERPRODUCTS=mvyqbv8ao7u6bct
NOCODB_TABLE_ACCESSGRANTS=mxdx3yhfcyej3ao
NOCODB_LINK_PROD_RESOURCES=c4ko4x8kwi16swg
ALIST_URL=http://alist-serve-resource:5244
ALIST_ADMIN_USERNAME=hoelee
ALIST_ADMIN_PASSWORD=<admin 密码>
```
改完 env 必须**重建 n8n 容器**(不是 restart)。
## 导入步骤
1. n8n → Import from File → `W1-customer-provision.json` / `W2-customerproduct-sync.json`。
2. Webhook 节点 → Copy Webhook URL。
3. NocoDB → 对应表 → 建 Webhook(After Insert + After Update)粘 URL:
- Customers 表 → W1;CustomerProducts 表 → W2。
## AList user 创建顺序(重要)
`user/create` 传空 `role` 会失败(`admin or guest user can not be created`,因 AList 把空 role
自动填成 guest 后拒绝)。**必须先 `role/create` 建客户专属 role,再 `user/create` 传 `role:[新roleId]`。**
详见 `docs/API_INTEGRATION.md` §9.7。
## 验证状态
- ✅ W1 已实测跑通(create user 自动化成功)。
- W2 待测(同 Load Env 模式已套用)。