使用 Pm2 将 .env 文件复制到多台主机
Copying .env files To Mutliple Host Machines w/ Pm2
我正在尝试使用 Pm2 的部署过程将我的 Node.js 脚本部署到多个主机。
使用单个主机工作正常,具有以下 ecosystem.config.js
文件:
require("dotenv").config({ path: `./envs/.production.env` });
const path = require("path");
module.exports = {
apps: [
{
name: process.env.APP_NAME,
interpreter: process.env.NODE_PATH,
cwd: process.env.PROJECT_PATH
script: "dist/index.js",
instances: process.env.INSTANCES || 0,
exec_mode: "cluster",
env: {
...process.env,
},
},
],
deploy: {
production: {
user: "harrison",
host: process.env.HOST,
key: "~/.ssh/id_rsa",
ref: "origin/master",
repo: process.env.GIT_REPO,
path: process.env.PROJECT_PATH,
// Copy keys to server
"pre-deploy-local": `scp -Cr envs harrison@${process.env.HOST}:${process.env.PROJECT_PATH}/current`,
// Build app and restart PM2 processes
"post-deploy": `yarn install --ignore-engines && \
pwd && \
yarn prod:build && \
yarn prod:serve`,
},
},
};
为了将其部署到多个主机,PM2 文档非常简单:只需添加多个主机名即可。好的,很简单。在我的 .env 文件中,我使用了一系列以逗号分隔的 IP 地址,然后将它们拆分到我的配置文件中的一个数组中,如下所示:
host: process.env.HOST.split(",");
然而,将我的 .env 文件复制到多个主机并不是那么容易。
如何将此生态系统文件的 "pre-deploy-local" 部分配置为 scp
我的 .env
文件到每台主机?
完成了 运行 一个 bash 脚本。 HOSTS的列表在我的.env.production
文件里,用逗号隔开,像这样
HOSTS=123.12.134.122,134.135.134.134
要将它们传递给 Pm2 主机,请用 space 替换每个逗号并将它们作为参数传递给 bash 脚本。然后,在部署上执行 bash 脚本。
我的生态系统文件现在看起来像这样:
// PM2 CONFIGURATION FOR PRODUCTION BUILDS
require("dotenv").config({ path: `./envs/.production.env` });
const path = require("path");
const hosts = process.env.HOSTS.replace(/,/g, " ");
module.exports = {
apps: [
{
name: process.env.APP_NAME,
args: ["--color"],
interpreter: process.env.NODE_PATH,
cwd: path.resolve(process.env.PROJECT_PATH, "current"), // Path holding the current version of our app (where post-deploy runs)
script: "dist/index.js", // Location of
instances: process.env.INSTANCES || 0,
exec_mode: "cluster",
env: {
...process.env,
},
},
],
deploy: {
production: {
user: "harrison",
host: process.env.HOSTS.split(","),
key: "~/.ssh/id_rsa2",
ref: "origin/master",
repo: process.env.GIT_REPO,
// Where to deploy on the server
path: process.env.PROJECT_PATH,
// Pass hosts as arguments to .env copy script
"pre-deploy-local": `./deployEnvs.sh ${
process.env.PROJECT_PATH
} ${hosts}`,
"post-deploy": `yarn install --ignore-engines && \
yarn prod:build && \
yarn prod:serve`,
},
},
};
而名为 deployEnvs.sh
的 bash 脚本如下所示:
#!/bin/bash
PROJECT_PATH=""
for HOST in "${@:2}"
do
scp -Cr envs "harrison@${HOST}:${PROJECT_PATH}/current"
done
我正在尝试使用 Pm2 的部署过程将我的 Node.js 脚本部署到多个主机。
使用单个主机工作正常,具有以下 ecosystem.config.js
文件:
require("dotenv").config({ path: `./envs/.production.env` });
const path = require("path");
module.exports = {
apps: [
{
name: process.env.APP_NAME,
interpreter: process.env.NODE_PATH,
cwd: process.env.PROJECT_PATH
script: "dist/index.js",
instances: process.env.INSTANCES || 0,
exec_mode: "cluster",
env: {
...process.env,
},
},
],
deploy: {
production: {
user: "harrison",
host: process.env.HOST,
key: "~/.ssh/id_rsa",
ref: "origin/master",
repo: process.env.GIT_REPO,
path: process.env.PROJECT_PATH,
// Copy keys to server
"pre-deploy-local": `scp -Cr envs harrison@${process.env.HOST}:${process.env.PROJECT_PATH}/current`,
// Build app and restart PM2 processes
"post-deploy": `yarn install --ignore-engines && \
pwd && \
yarn prod:build && \
yarn prod:serve`,
},
},
};
为了将其部署到多个主机,PM2 文档非常简单:只需添加多个主机名即可。好的,很简单。在我的 .env 文件中,我使用了一系列以逗号分隔的 IP 地址,然后将它们拆分到我的配置文件中的一个数组中,如下所示:
host: process.env.HOST.split(",");
然而,将我的 .env 文件复制到多个主机并不是那么容易。
如何将此生态系统文件的 "pre-deploy-local" 部分配置为 scp
我的 .env
文件到每台主机?
完成了 运行 一个 bash 脚本。 HOSTS的列表在我的.env.production
文件里,用逗号隔开,像这样
HOSTS=123.12.134.122,134.135.134.134
要将它们传递给 Pm2 主机,请用 space 替换每个逗号并将它们作为参数传递给 bash 脚本。然后,在部署上执行 bash 脚本。
我的生态系统文件现在看起来像这样:
// PM2 CONFIGURATION FOR PRODUCTION BUILDS
require("dotenv").config({ path: `./envs/.production.env` });
const path = require("path");
const hosts = process.env.HOSTS.replace(/,/g, " ");
module.exports = {
apps: [
{
name: process.env.APP_NAME,
args: ["--color"],
interpreter: process.env.NODE_PATH,
cwd: path.resolve(process.env.PROJECT_PATH, "current"), // Path holding the current version of our app (where post-deploy runs)
script: "dist/index.js", // Location of
instances: process.env.INSTANCES || 0,
exec_mode: "cluster",
env: {
...process.env,
},
},
],
deploy: {
production: {
user: "harrison",
host: process.env.HOSTS.split(","),
key: "~/.ssh/id_rsa2",
ref: "origin/master",
repo: process.env.GIT_REPO,
// Where to deploy on the server
path: process.env.PROJECT_PATH,
// Pass hosts as arguments to .env copy script
"pre-deploy-local": `./deployEnvs.sh ${
process.env.PROJECT_PATH
} ${hosts}`,
"post-deploy": `yarn install --ignore-engines && \
yarn prod:build && \
yarn prod:serve`,
},
},
};
而名为 deployEnvs.sh
的 bash 脚本如下所示:
#!/bin/bash
PROJECT_PATH=""
for HOST in "${@:2}"
do
scp -Cr envs "harrison@${HOST}:${PROJECT_PATH}/current"
done