Skip to content

Commit 4725410

Browse files
authored
Merge pull request #474 from DefangLabs/kevin/n8n
Added n8n to samples
2 parents bad472d + 410bcfe commit 4725410

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

.github/workflows/deploy-changed-samples.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ jobs:
106106
TEST_SSL_MODE: ${{ secrets.TEST_SSL_MODE }}
107107
TEST_TAVILY_API_KEY: ${{ secrets.TEST_TAVILY_API_KEY }}
108108
TEST_ALLOWED_HOSTS: ${{ secrets.TEST_ALLOWED_HOSTS }}
109+
TEST_N8N_ENCRYPTION_KEY: ${{ secrets.TEST_N8N_ENCRYPTION_KEY }}
110+
TEST_DB_POSTGRESDB_SSL_ENABLED: ${{ secrets.TEST_DB_POSTGRESDB_SSL_ENABLED }}
111+
TEST_DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED: ${{ secrets.TEST_DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED }}
112+
TEST_DB_POSTGRESDB_PASSWORD: ${{ secrets.TEST_POSTGRES_PASSWORD }}
109113
run: |
110114
SAMPLES=$(sed 's|^samples/||' changed_samples.txt | paste -s -d ',' -)
111115
echo "Running tests for samples: $SAMPLES"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]
22+
with:
23+
config-env-vars: POSTGRES_USER N8N_ENCRYPTION_KEY DB_POSTGRESDB_SSL_ENABLED DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED
24+
env:
25+
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
26+
N8N_ENCRYPTION_KEY: ${{ secrets.N8N_ENCRYPTION_KEY }}
27+
DB_POSTGRESDB_SSL_ENABLED: ${{ secrets.DB_POSTGRESDB_SSL_ENABLED }}
28+
DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED: ${{ secrets.DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED }}

samples/n8n/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# n8n
2+
3+
[![1-click-deploy](https://raw.githubusercontent.com/DefangLabs/defang-assets/main/Logos/Buttons/SVG/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-n8n-template%26template_owner%3DDefangSamples)
4+
5+
This sample shows how to get a [n8n](https://n8n.io/) app up using PostgreSQL as a database and running with Defang. We based it in part on the [original sample](https://github.com/n8n-io/n8n-hosting/tree/main/docker-compose/withPostgres) from the n8n team, but we have adapted it to be consistent with the way our other samples are configured.
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Development
14+
15+
To run the application locally for development, use the development compose file:
16+
17+
```bash
18+
docker compose -f compose.dev.yaml up
19+
```
20+
21+
This will:
22+
23+
- Start PostgreSQL with volume persistence for local development
24+
- Expose PostgreSQL on port 5432 for direct access if needed
25+
- Start n8n on port 5678 with volume persistence
26+
27+
You can access n8n at `http://localhost:5678` once the containers are running.
28+
29+
## Configuration
30+
31+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration). Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
32+
33+
### `POSTGRES_PASSWORD`
34+
35+
The password for your Postgres database. You need to set this before deploying for the first time.
36+
37+
*You can easily set this to a random string using `defang config set POSTGRES_PASSWORD --random`*
38+
39+
### `N8N_ENCRYPTION_KEY`
40+
41+
The encryption key for your n8n instance. This is used to encrypt sensitive data in the database.
42+
43+
*You can easily set this to a random string using `defang config set N8N_ENCRYPTION_KEY --random`*
44+
45+
### `DB_POSTGRESDB_SSL_ENABLED`
46+
47+
Set to `true` to enable SSL. Set to `false` to disable SSL.
48+
49+
### `DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED`
50+
51+
Set to `true` to reject unauthorized certificates. Set to `false` to accept unauthorized certificates. Counterintuitive, but leave this as **true** to **disable SSL** (required for local dev and playground).
52+
53+
54+
## Deployment
55+
56+
> [!NOTE]
57+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
58+
59+
### Defang Playground
60+
61+
Deploy your application to the Defang Playground by opening up your terminal and typing:
62+
63+
```bash
64+
defang compose up
65+
```
66+
67+
### BYOC
68+
69+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud).
70+
71+
---
72+
73+
Title: n8n
74+
75+
Short Description: A n8n app running on Defang.
76+
77+
Tags: n8n, PostgreSQL, Docker
78+
79+
Languages: Docker, Shell

samples/n8n/compose.dev.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
services:
2+
postgres:
3+
extends:
4+
file: compose.yaml
5+
service: postgres
6+
environment:
7+
- POSTGRES_PASSWORD=password
8+
volumes:
9+
- db_storage:/var/lib/postgresql/data
10+
healthcheck:
11+
test: ["CMD-SHELL", "pg_isready -h localhost -U n8nuser -d n8ndatabase"]
12+
13+
n8n:
14+
extends:
15+
file: compose.yaml
16+
service: n8n
17+
environment:
18+
- DB_POSTGRESDB_SSL_ENABLED=false
19+
- DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED=true # counterintuitive, but leave this as true to *disable* SSL (required for local dev)
20+
- N8N_ENCRYPTION_KEY=unsafe-encryption-key
21+
- DB_POSTGRESDB_PASSWORD=password
22+
ports:
23+
- 5678:5678
24+
links:
25+
- postgres
26+
volumes:
27+
- n8n_storage:/home/node/.n8n
28+
depends_on:
29+
postgres:
30+
condition: service_healthy
31+
32+
volumes:
33+
db_storage:
34+
n8n_storage:

samples/n8n/compose.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
services:
2+
postgres:
3+
image: postgres:16
4+
x-defang-postgres: true
5+
restart: always
6+
environment:
7+
- POSTGRES_USER=n8nuser
8+
- POSTGRES_DB=n8ndatabase
9+
# MANAGED CONFIG
10+
- POSTGRES_PASSWORD
11+
healthcheck:
12+
test: ["CMD-SHELL", "pg_isready -h localhost -U n8nuser -d n8ndatabase"]
13+
interval: 5s
14+
timeout: 5s
15+
retries: 10
16+
17+
n8n:
18+
image: docker.n8n.io/n8nio/n8n
19+
restart: always
20+
# Uncomment this if you are using a custom domain
21+
# domainname: yourappdomain.com
22+
environment:
23+
- DB_TYPE=postgresdb
24+
- DB_POSTGRESDB_HOST=postgres
25+
- DB_POSTGRESDB_PORT=5432
26+
- DB_POSTGRESDB_DATABASE=n8ndatabase
27+
- DB_POSTGRESDB_USER=n8nuser
28+
- N8N_RUNNERS_ENABLED=true
29+
- N8N_HOST=n8n
30+
- N8N_PORT=5678
31+
- N8N_PROTOCOL=https
32+
- N8N_PROXY_HOPS=1 # Required behind a load balancer
33+
- NODE_ENV=production
34+
- WEBHOOK_URL=https://n8n
35+
- DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED
36+
- DB_POSTGRESDB_SSL_ENABLED
37+
# MANAGED CONFIG
38+
- N8N_ENCRYPTION_KEY
39+
- DB_POSTGRESDB_PASSWORD
40+
ports:
41+
- 5678:5678
42+
links:
43+
- postgres
44+
depends_on:
45+
postgres:
46+
condition: service_healthy
47+
healthcheck:
48+
test:
49+
[
50+
"CMD-SHELL",
51+
'node --eval "const r = await fetch(''http://localhost:5678/healthz''); if(r.status === 200 || r.status === 201) { process.exit(0); } else { process.exit(1); };"',
52+
]
53+
interval: 10s
54+
timeout: 5s
55+
retries: 5
56+
start_period: 120s

0 commit comments

Comments
 (0)