Skip to content

Commit 1695094

Browse files
committed
Working n8n with Oauth
1 parent 0f0ce0c commit 1695094

File tree

8 files changed

+120
-73
lines changed

8 files changed

+120
-73
lines changed

samples/n8n/.env

Lines changed: 0 additions & 6 deletions
This file was deleted.

samples/n8n/.github/workflows/deploy.yaml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ on:
77

88
jobs:
99
deploy:
10-
environment: playground
1110
runs-on: ubuntu-latest
1211
permissions:
1312
contents: read
1413
id-token: write
1514

1615
steps:
17-
- name: Checkout Repo
18-
uses: actions/checkout@v4
16+
- name: Checkout Repo
17+
uses: actions/checkout@v4
1918

20-
- name: Deploy
21-
uses: DefangLabs/[email protected]
19+
- name: Deploy
20+
uses: DefangLabs/[email protected]
21+
with:
22+
config-env-vars: POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB POSTGRES_NON_ROOT_USER POSTGRES_NON_ROOT_PASSWORD
23+
env:
24+
POSTGRES_USER: ${{ secrets.POSTGRES_USER }}
25+
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
26+
POSTGRES_DB: ${{ secrets.POSTGRES_DB }}
27+
POSTGRES_NON_ROOT_USER: ${{ secrets.POSTGRES_NON_ROOT_USER }}
28+
POSTGRES_NON_ROOT_PASSWORD: ${{ secrets.POSTGRES_NON_ROOT_PASSWORD }}

samples/n8n/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ The name of your Postgres database. You need to set this before deploying for th
4747

4848
### `POSTGRES_NON_ROOT_USER`
4949

50-
The non-root user for your Postgres database, it will be used in the service release to setup non-root access for your Postgres database. It is also used in the script called `init-data.sh`. You need to set this before deploying for the first time.
50+
The non-root user for your Postgres database, it will be used in the service setup to setup non-root access for your Postgres database. It is also used in the script called `init-data.sh`. You need to set this before deploying for the first time.
5151

5252
### `POSTGRES_NON_ROOT_PASSWORD`
5353

54-
The POSTGRES_NON_ROOT_USER's password for your Postgres database, it will be used in the service release to setup non-root access for your Postgres database. It is also used in the script called `init-data.sh`. You need to set this before deploying for the first time.
54+
The POSTGRES_NON_ROOT_USER's password for your Postgres database, it will be used in the service setup to setup non-root access for your Postgres database. It is also used in the script called `init-data.sh`. You need to set this before deploying for the first time.
5555

5656
## Deployment
5757

@@ -75,7 +75,7 @@ This will use `compose.yaml` which extends `compose.dev.yaml` but:
7575

7676
- Removes volume mounts (not supported by Defang)
7777
- Removes PostgreSQL port exposure for security
78-
- Adds a `release` service to initialize the database with proper user permissions
78+
- Adds a `setup` service to initialize the database with proper user permissions
7979
- Optimizes environment variables for cloud deployment
8080

8181
### BYOC (AWS)

samples/n8n/compose.dev.yaml

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
1-
version: '3.8'
2-
3-
# Defang does not support volumes, therefore these volumes are only used for local development and will be ignored in production.
4-
# Consider using s3 buckets instead: https://docs.n8n.io/hosting/scaling/external-storage/
51
volumes:
62
db_storage:
73
n8n_storage:
84

95
services:
106
postgres:
11-
image: postgres:16
12-
restart: always
7+
extends:
8+
file: compose.yaml
9+
service: postgres
1310
environment:
14-
- POSTGRES_USER=${POSTGRES_USER}
15-
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
16-
- POSTGRES_DB=${POSTGRES_DB}
17-
- POSTGRES_NON_ROOT_USER=${POSTGRES_NON_ROOT_USER}
18-
- POSTGRES_NON_ROOT_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
11+
- POSTGRES_USER=postgres
12+
- POSTGRES_PASSWORD=defang12345
13+
- POSTGRES_DB=n8n
14+
- POSTGRES_NON_ROOT_USER=defang_user
15+
- POSTGRES_NON_ROOT_PASSWORD=defang12345
1916
volumes:
2017
- db_storage:/var/lib/postgresql/data
2118
- ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
2219
healthcheck:
23-
test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}']
24-
interval: 5s
25-
timeout: 5s
26-
retries: 10
20+
test: ["CMD-SHELL", "pg_isready -h localhost -U postgres -d n8n"]
2721

2822
n8n:
2923
image: docker.n8n.io/n8nio/n8n
@@ -32,9 +26,9 @@ services:
3226
- DB_TYPE=postgresdb
3327
- DB_POSTGRESDB_HOST=postgres
3428
- DB_POSTGRESDB_PORT=5432
35-
- DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
36-
- DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
37-
- DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
29+
- DB_POSTGRESDB_DATABASE=n8n
30+
- DB_POSTGRESDB_USER=defang_user
31+
- DB_POSTGRESDB_PASSWORD=defang12345
3832
ports:
3933
- 5678:5678
4034
links:
@@ -43,4 +37,4 @@ services:
4337
- n8n_storage:/home/node/.n8n
4438
depends_on:
4539
postgres:
46-
condition: service_healthy
40+
condition: service_healthy

samples/n8n/compose.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
services:
2+
setup:
3+
# This is a one-off job to initialize the database with a non-root user
4+
build:
5+
dockerfile: setup.Dockerfile
6+
command: "/init-data.sh"
7+
environment:
8+
- POSTGRES_USER
9+
- POSTGRES_HOST=${POSTGRES_USER}
10+
# Need to pass PGPASSWORD so the password can be used by psql in the script init-data.sh
11+
- PGPASSWORD=${POSTGRES_PASSWORD}
12+
- POSTGRES_DB
13+
- POSTGRES_NON_ROOT_USER
14+
- POSTGRES_NON_ROOT_PASSWORD
15+
restart: "no"
16+
depends_on:
17+
postgres:
18+
condition: service_healthy
19+
20+
postgres:
21+
image: postgres:16
22+
restart: always
23+
environment:
24+
- POSTGRES_USER
25+
- POSTGRES_PASSWORD
26+
- POSTGRES_DB
27+
- POSTGRES_NON_ROOT_USER
28+
- POSTGRES_NON_ROOT_PASSWORD
29+
healthcheck:
30+
test:
31+
[
32+
"CMD-SHELL",
33+
"pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}",
34+
]
35+
interval: 5s
36+
timeout: 5s
37+
retries: 10
38+
39+
n8n:
40+
image: docker.n8n.io/n8nio/n8n
41+
restart: always
42+
environment:
43+
- DB_TYPE=postgresdb
44+
- DB_POSTGRESDB_HOST=postgres
45+
- DB_POSTGRESDB_PORT=5432
46+
- DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
47+
- DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
48+
- DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
49+
# https://community.n8n.io/t/how-to-change-localhost-5678-from-webhook-url/27033/8
50+
- N8N_HOST=n8n--5678.n8n.kevyvo.defang.app
51+
- N8N_PORT=5678
52+
- N8N_PROTOCOL=https
53+
- NODE_ENV=production
54+
- WEBHOOK_URL=https://n8n--5678.n8n.kevyvo.defang.app
55+
ports:
56+
- 5678:5678
57+
links:
58+
- postgres
59+
depends_on:
60+
setup:
61+
condition: service_completed_successfully
62+
postgres:
63+
condition: service_healthy

samples/n8n/compose.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.

samples/n8n/init-data.sh

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,35 @@ set -e;
33

44

55
if [ -n "${POSTGRES_NON_ROOT_USER:-}" ] && [ -n "${POSTGRES_NON_ROOT_PASSWORD:-}" ]; then
6-
# Need to add --host because it is run in a separate container
7-
psql -v ON_ERROR_STOP=1 --host="$POSTGRES_HOST" --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
8-
CREATE USER ${POSTGRES_NON_ROOT_USER} WITH PASSWORD '${POSTGRES_NON_ROOT_PASSWORD}';
9-
GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_NON_ROOT_USER};
10-
GRANT CREATE ON SCHEMA public TO ${POSTGRES_NON_ROOT_USER};
11-
EOSQL
6+
# Retry logic with maximum 20 attempts and 3-second delays
7+
max_attempts=20
8+
attempt=1
9+
10+
while [ $attempt -le $max_attempts ]; do
11+
echo "Attempt $attempt of $max_attempts to initialize database..."
12+
13+
# Need to add --host because it is run in a separate container
14+
if psql -v ON_ERROR_STOP=1 --host="$POSTGRES_HOST" --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
15+
CREATE USER ${POSTGRES_NON_ROOT_USER} WITH PASSWORD '${POSTGRES_NON_ROOT_PASSWORD}';
16+
GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_NON_ROOT_USER};
17+
GRANT CREATE ON SCHEMA public TO ${POSTGRES_NON_ROOT_USER};
18+
EOSQL
19+
then
20+
echo "Database initialization successful on attempt $attempt"
21+
break
22+
else
23+
echo "Database initialization failed on attempt $attempt"
24+
if [ $attempt -lt $max_attempts ]; then
25+
echo "Waiting 3 seconds before retry..."
26+
sleep 3
27+
else
28+
echo "All $max_attempts attempts failed. Exiting."
29+
exit 1
30+
fi
31+
fi
32+
33+
attempt=$((attempt + 1))
34+
done
1235
else
1336
echo "SETUP INFO: No Environment variables given!"
1437
fi

0 commit comments

Comments
 (0)