From 45d8a93ddd5d7718baa1a38d1ca0f396deaba422 Mon Sep 17 00:00:00 2001 From: wuyanwanwu <104009119+wuyanwanwu@users.noreply.github.com.> Date: Sun, 9 Aug 2026 20:07:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0n8n=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +++ n8n/.env.example | 14 +++++++++ n8n/README.md | 48 +++++++++++++++++++++++++++++ n8n/docker-compose.yml | 9 ++++++ n8n/workflows/01-test-postgres.json | 46 +++++++++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 n8n/.env.example create mode 100644 n8n/README.md create mode 100644 n8n/docker-compose.yml create mode 100644 n8n/workflows/01-test-postgres.json diff --git a/.gitignore b/.gitignore index 9b3fd48..d6e1bd0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,7 @@ *.log data/ backups/ + +# n8n runtime secret and persistent data +n8n/.env +n8n/data/ diff --git a/n8n/.env.example b/n8n/.env.example new file mode 100644 index 0000000..1e46f31 --- /dev/null +++ b/n8n/.env.example @@ -0,0 +1,14 @@ +# n8n instance secret. Generate once with: openssl rand -hex 32 +# Do not change this value after n8n has stored credentials. +N8N_ENCRYPTION_KEY= + +# Initial access is through an SSH tunnel, so keep n8n on localhost. +N8N_HOST=127.0.0.1 +N8N_PORT=5678 +N8N_LISTEN_ADDRESS=127.0.0.1 +N8N_PROTOCOL=http +N8N_EDITOR_BASE_URL=http://127.0.0.1:5678 +N8N_SECURE_COOKIE=false + +TZ=Asia/Shanghai +GENERIC_TIMEZONE=Asia/Shanghai diff --git a/n8n/README.md b/n8n/README.md new file mode 100644 index 0000000..e13bc81 --- /dev/null +++ b/n8n/README.md @@ -0,0 +1,48 @@ +# n8n deployment + +This directory runs n8n separately from the news web service. The n8n data +directory and `.env` stay on the server and must not be committed to Gitea. + +## First deployment on the server + +```bash +cd /projects/daily-news-digest/n8n +cp .env.example .env +openssl rand -hex 32 +nano .env +chmod 600 .env +mkdir -p data +sudo chown -R 1000:1000 data +docker compose up -d +docker compose ps +docker compose logs --tail=50 n8n +``` + +The generated value must be copied to `N8N_ENCRYPTION_KEY` and must not be +changed later. n8n listens only on the server's loopback address. From a local +computer, create an SSH tunnel and open `http://127.0.0.1:5678`: + +```powershell +ssh -L 5678:127.0.0.1:5678 root@YOUR_SERVER_IP +``` + +## Updating from Gitea + +```bash +cd /projects/daily-news-digest +git pull +cd n8n +docker compose up -d +``` + +Do not overwrite `n8n/.env` or `n8n/data` during updates. + +## PostgreSQL credential + +In n8n, create a PostgreSQL credential for the existing application database. +Because n8n uses host networking and PostgreSQL is on the same host, use the +host's local PostgreSQL address (`127.0.0.1`), port `5432`, your database/user/ +password, and disable SSL when the server reports that it does not support SSL. + +The first workflow in `workflows/01-test-postgres.json` only checks this +connection. It does not fetch news or send email. diff --git a/n8n/docker-compose.yml b/n8n/docker-compose.yml new file mode 100644 index 0000000..f8ba42e --- /dev/null +++ b/n8n/docker-compose.yml @@ -0,0 +1,9 @@ +services: + n8n: + image: docker.n8n.io/n8nio/n8n:latest + restart: unless-stopped + network_mode: host + env_file: + - .env + volumes: + - ./data:/home/node/.n8n diff --git a/n8n/workflows/01-test-postgres.json b/n8n/workflows/01-test-postgres.json new file mode 100644 index 0000000..f5113b7 --- /dev/null +++ b/n8n/workflows/01-test-postgres.json @@ -0,0 +1,46 @@ +{ + "name": "Daily News Digest - Test PostgreSQL", + "nodes": [ + { + "parameters": {}, + "id": "manual-trigger", + "name": "手动执行", + "type": "n8n-nodes-base.manualTrigger", + "typeVersion": 1, + "position": [260, 300] + }, + { + "parameters": { + "operation": "executeQuery", + "query": "SELECT current_database() AS database_name, NOW() AS checked_at, (SELECT COUNT(*) FROM news_sources) AS source_count, (SELECT COUNT(*) FROM news_articles) AS article_count;", + "options": {} + }, + "id": "postgres-check", + "name": "检查新闻数据库", + "type": "n8n-nodes-base.postgres", + "typeVersion": 2.5, + "position": [520, 300] + } + ], + "connections": { + "手动执行": { + "main": [ + [ + { + "node": "检查新闻数据库", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": false, + "settings": {}, + "versionId": "daily-news-digest-test-postgres-v1", + "meta": { + "templateCredsSetupCompleted": false + }, + "pinData": {}, + "tags": [] +}