Last active
August 19, 2020 09:04
-
-
Save mamsoudi/93407495730d28b7f14ae9b2cdb7a1b3 to your computer and use it in GitHub Desktop.
Here's the configuration I came up with for my PM2 API instances on my own server, hopefully it'll be useful.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
apps : [{ | |
name: 'Sample API', | |
exec_mode: 'cluster', | |
instances: 4, | |
watch: true, | |
// I had my database under `./cinema` so make sure anything changing inside the project's root is ignored | |
// if you have watch turned on above | |
ignore_watch: ['node_modules', 'cinema'], | |
script: './dist/index.js', | |
// I later use these to run different instances, notice that there is an option to add env names to instance names that | |
// will probably make it easier for you to deal with instances and work with them via PM2 cli. You can find it in the docs. | |
env: { | |
'NODE_ENV': 'test' | |
}, | |
env_dev: { | |
'NODE_ENV': 'dev' | |
}, | |
env_prod: { | |
'NODE_ENV': 'production' | |
} | |
}], | |
deploy : { | |
prod : { | |
user : 'USERNAME', | |
host : 'SERVER_ADDRESS/IP_ADDRESS', | |
ref : 'origin/master', | |
repo : 'git@github.com:YOUR_USERNAME/REPO_NAME.git', | |
// Pay attention that i'm specifing which env I'm targeting for this deployment environement. It should match your env | |
path : '/home/USERNAME_ON_SERVER/{PATH_TO_PROJECT_ROOT}/prod/', | |
// Here I'm telling it to use my exisiting SSH key on my local to connect to Github to fetch latest changes, You can | |
// create a new one on your server and tell PM2 to use that one. | |
'ssh_options': 'ForwardAgent=yes', | |
'pre-deploy-local': 'echo "Started Deploying Production Build! :)"', | |
// Here's where magic happens. You can gracefully reload instances of `prod` clusters you started. | |
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env prod', | |
'pre-setup': 'echo "Started setting-up Deployment for Production"', | |
'post-setup': 'echo "Setup Finished!"' | |
}, | |
dev : { | |
user : 'USERNAME', | |
host : 'SERVER_ADDRESS/IP_ADDRESS', | |
ref : 'origin/master', | |
repo : 'git@github.com:YOUR_USERNAME/REPO_NAME.git', | |
path : '/home/USERNAME_ON_SERVER/{PATH_TO_PROJECT_ROOT}/dev/', | |
'ssh_options': 'ForwardAgent=yes', | |
'pre-deploy-local': 'echo "Started Deploying Development! :)"', | |
'post-deploy' : 'yarn && yarn compile && pm2 startOrReload ecosystem.config.js --env dev', | |
'pre-setup': 'echo "Started setting-up Deployment for Development"', | |
'post-setup': 'echo "Setup Finished!"' | |
}, | |
test: { | |
user : 'USERNAME', | |
host : 'SERVER_ADDRESS/IP_ADDRESS', | |
// This line is a bit tricky, sometimes when you're testing a specific branch of your code, you might want to | |
// tell github to fetch that branch on server to stage it, here, by doing so, I can later tell PM2 in command line | |
// that I need the ref to be, say, `masoud/test-something` and on the server, it'd fetch that branch and run `test` | |
// with the branch I told it to. Pretty handy for testing and staging. | |
ref : process.env.BRANCH || 'dev', | |
repo : 'git@github.com:YOUR_USERNAME/REPO_NAME.git', | |
path : '/home/USERNAME_ON_SERVER/{PATH_TO_PROJECT_ROOT}/test/', | |
'ssh_options': 'ForwardAgent=yes', | |
'pre-deploy-local': `echo "Started Deploying ${process.env.BRANCH || 'dev'} branch! :)"`, | |
'post-deploy' : 'ls -la && yarn && yarn compile && pm2 startOrReload ecosystem.config.js --env test', | |
'pre-setup': 'echo "Started setting-up Deployment"', | |
'post-setup': 'echo "Setup Finished!"' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment