Skip to content

Commit 38d7782

Browse files
committed
test: complete dotenvx example and add docs
1 parent 4e5fd17 commit 38d7782

File tree

6 files changed

+53
-27
lines changed

6 files changed

+53
-27
lines changed

examples/dotenvx-pm2/.env

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

examples/dotenvx-pm2/.env.initial

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DE=dotenv_initial
2+
SH_DE=dotenv_initial
3+
DE_PM=dotenv_initial
4+
SH_DE_PM=dotenv_initial

examples/dotenvx-pm2/.env.updated

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DE=dotenv_updated
2+
SH_DE=dotenv_updated
3+
DE_PM=dotenv_updated
4+
SH_DE_PM=dotenv_updated

examples/dotenvx-pm2/README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
This is an example of using [dotenvx](https://dotenvx.com/) with pm2.
12

2-
To start http application in cluster mode:
3+
It demonstrates how to update the environment for forked and clustered apps using dotenvx.
4+
5+
In this example, the environment variables come from three sources and are applied in the following order (dotenv files take precedence because of the `--overload` option used):
6+
- shell environment
7+
- dotenv files `.env.initial` or `.env.updated`.
8+
- pm2 ecosystem config `ecosystem.config.cjs`
9+
10+
See `package.json` scripts for the actual commands.
311

412
```bash
5-
$ pm2 start ecosystem.config.js
6-
# OR
7-
$ pm2 start http.js -i max
13+
# install dotenvx
14+
npm install
15+
# start apps with `initial` environment and `.env.initial` dotenv file
16+
npm run start:json
17+
# reload apps with `updated` environment and `.env.updated` dotenv file
18+
npm run reload:json
19+
# delete apps
20+
npm run delete:json
821
```

examples/dotenvx-pm2/ecosystem.config.cjs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,39 @@ module.exports = {
33
{
44
name: 'forked_app',
55
script: './index.js',
6-
env: {
6+
env_initial: {
77
PORT: 8001,
8-
PM: 'pm2',
9-
SH_PM: 'pm2',
10-
DE_PM: 'pm2',
11-
SH_DE_PM: 'pm2',
8+
PM: 'pm2_initial',
9+
SH_PM: 'pm2_initial',
10+
DE_PM: 'pm2_initial',
11+
SH_DE_PM: 'pm2_initial',
12+
},
13+
env_updated: {
14+
PORT: 8001,
15+
PM: 'pm2_updated',
16+
SH_PM: 'pm2_updated',
17+
DE_PM: 'pm2_updated',
18+
SH_DE_PM: 'pm2_updated',
1219
},
1320
},
1421
{
1522
name: 'clustered_app',
1623
script: './index.js',
1724
instances: 2,
1825
exec_mode: 'cluster',
19-
env: {
26+
env_initial: {
27+
PORT: 8002,
28+
PM: 'pm2_initial',
29+
SH_PM: 'pm2_initial',
30+
DE_PM: 'pm2_initial',
31+
SH_DE_PM: 'pm2_initial',
32+
},
33+
env_updated: {
2034
PORT: 8002,
21-
PM: 'pm2',
22-
SH_PM: 'pm2',
23-
DE_PM: 'pm2',
24-
SH_DE_PM: 'pm2',
35+
PM: 'pm2_updated',
36+
SH_PM: 'pm2_updated',
37+
DE_PM: 'pm2_updated',
38+
SH_DE_PM: 'pm2_updated',
2539
},
2640
},
2741
],

examples/dotenvx-pm2/package.json

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
"author": "Michael Kalygin",
88
"license": "MIT",
99
"scripts": {
10-
"start:json:simple": "SH=shell SH_DE=shell SH_PM=shell SH_DE_PM=shell dotenvx run --env-file .env -- ../../bin/pm2 start ecosystem.config.cjs",
11-
"start:json:overload": "SH=shell SH_DE=shell SH_PM=shell SH_DE_PM=shell dotenvx run --env-file .env --overload -- ../../bin/pm2 start ecosystem.config.cjs",
12-
"start:pid:simple": "SH=shell SH_DE=shell SH_PM=shell SH_DE_PM=shell dotenvx run --env-file .env -- ../../bin/pm2 start forked_app clustered_app",
13-
"start:pid:overload": "SH=shell SH_DE=shell SH_PM=shell SH_DE_PM=shell dotenvx run --env-file .env --overload -- ../../bin/pm2 start forked_app clustered_app",
14-
"reload:json:simple": "SH=shell SH_DE=shell SH_PM=shell SH_DE_PM=shell dotenvx run --env-file .env -- ../../bin/pm2 reload ecosystem.config.cjs --update-env",
15-
"reload:json:overload": "SH=shell SH_DE=shell SH_PM=shell SH_DE_PM=shell dotenvx run --env-file .env --overload -- ../../bin/pm2 reload ecosystem.config.cjs --update-env",
16-
"reload:pid:simple": "SH=shell SH_DE=shell SH_PM=shell SH_DE_PM=shell dotenvx run --env-file .env -- ../../bin/pm2 reload forked_app clustered_app --update-env",
17-
"reload:pid:overload": "SH=shell SH_DE=shell SH_PM=shell SH_DE_PM=shell dotenvx run --env-file .env --overload -- ../../bin/pm2 reload forked_app clustered_app --update-env",
18-
"delete": "../../bin/pm2 delete ecosystem.config.cjs"
10+
"start:json": "SH=shell_initial SH_DE=shell_initial SH_PM=shell_initial SH_DE_PM=shell_initial dotenvx run --env-file .env.initial --overload -- ../../bin/pm2 start ecosystem.config.cjs --env initial",
11+
"reload:json": "SH=shell_updated SH_DE=shell_updated SH_PM=shell_updated SH_DE_PM=shell_updated dotenvx run --env-file .env.updated --overload -- ../../bin/pm2 reload ecosystem.config.cjs --env updated --update-env",
12+
"delete:json": "../../bin/pm2 delete ecosystem.config.cjs",
13+
"test:json": "../../bin/pm2 update && npm run delete:json && npm run start:json && npm run reload:json"
1914
},
2015
"dependencies": {
2116
"@dotenvx/dotenvx": "^1.10.2"

0 commit comments

Comments
 (0)