Skip to content

Instantly share code, notes, and snippets.

@gagregrog
Last active September 4, 2022 20:15
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gagregrog/c6963a09962adf22c91e7c1370dffc13 to your computer and use it in GitHub Desktop.
Save gagregrog/c6963a09962adf22c91e7c1370dffc13 to your computer and use it in GitHub Desktop.
Using GitHub Deploy Keys with PM2

PM2, EC2, and Github

Set Up Deploy Key on Remote Server

  1. SSH into the target machine
  2. Make a deploy_keys folder and change into it
    1. mkdir ~/.ssh/deploy_keys
    2. cd ~/.ssh/deploy_keys
  3. Create a new key pair
    1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
      • NOTE Set a custom path to save the keys in the current directory
    2. sudo chmod 400 id_rsa
  4. Add key to ssh agent
    1. eval "$(ssh-agent -s)"
    2. ssh-add ./id_rsa
  5. Add the deploy key to your project on GitHub
    1. Go to GitHub
    2. Go to the desired repo, navigate to settings, and find deploy keys
    3. Create a new deploy key and copy your public key from the server
      • cat id_rsa.pub
      • Copy all of the contents and create the deploy key
  6. Clone the project on the server and add GitHub to known hosts
    1. Switch back to root with cd
    2. git clone git@github.com:<username_or_orgname>/<project_name>.git
    3. On successful clone, remove the project:
      • rm -rf <project_name></project_name>
  7. Add an ssh config to help PM2
    1. nano ~/.ssh/config
    2. Add the following config with your parameters:
      Host github.com
        HostName github.com
        User <username_or_orgname>
        IdentityFile ~/.ssh/deploy_keys/id_rsa
      
    3. Save with ctrl+x and y and ENTER
  8. From your local machine, run the pm2 init script (npm run pm2-init below)

Ensure PM2 is properly set up

Using NVM

If you use NVM to manage your node installation, ensure that your .bashrc NVM activation script is at the top of the file, otherwise pm2 won't be able to access npm.

More info here.

Managing Logs

If your log files get too big, you can risk bricking your server. To avoid this, use a module to rotate your logs.

From the server, run pm2 install pm2-logrotate.

More info here.

Ecosystem File

// sample ecosystem.config.js file for managing deployments

require('dotenv').config();

module.exports = {
apps: [
  {
    name: 'Your App',
    script: './index.js',
  },
],
deploy: {
  production: { //
    user: 'ubuntu',
    host: process.env.HOST,
    key: process.env.KEY_PATH,
    ref: 'origin/master',
    repo: 'git@github.com:<username_or_orgname>/<project_name>.git',
    path: '/home/ubuntu/<project_name>',
    'post-deploy':
      "cp ~/.env ~/<project_name>/source/.env && npm install && pm2 startOrRestart ecosystem.config.js",
  },
},
};

NPM Scripts

Scripts to run from your local machine to interface with the remote pm2 instance.

"pm2-restart": "pm2 startOrRestart ecosystem.config.js",
"pm2-deploy": "pm2 deploy ecosystem.config.js production",
"pm2-logs": "pm2 deploy ecosystem.config.js production exec \"pm2 logs\"",
"pm2-ls": "pm2 deploy ecosystem.config.js production exec \"pm2 ls\"",
"pm2-flush": "pm2 deploy ecosystem.config.js production exec \"pm2 flush\"",
"pm2-init": "pm2 deploy ecosystem.config.js production setup" // run this first to initialize the install

Resources

Unitech/pm2-deploy#41

https://github.com/keymetrics/pm2-logrotate

https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key

https://developer.github.com/v3/guides/managing-deploy-keys/#deploy-keys

https://pm2.keymetrics.io/docs/usage/deployment/

https://forum.magicmirror.builders/topic/2715/pm2-disable-logs/2

@Nyamkhuub
Copy link

Thank you very much!, it's save my day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment