pm2 zero downtime with remote deploy

Viewed 277

I've been trying to set up a small pipeline with PM2 using its deploy-functionality (from my ubuntu desktop to my remote ubuntu server). It pulls from my github & builds remotely and restarts it. PM2 has some nice features (as described here https://pm2.io/docs/runtime/guide/easy-deploy-with-ssh/) but I don't understand how I would prevent the downtime during the build-process of my somewhat massive Nuxt application. It's around 5-10 minutes.

I followed the steps in this article with the caveat that I also needed to SCP over my own .env-file. https://warrenlee.tech/blog/deploy-your-nuxt-app-using-pm2

This is my ecosystem.config.js that I've added locally in my project folder:

module.exports = {
  apps: [
    {
      name: 'my-frontend',
      exec_mode: 'cluster',
      instances: 'max', // Or a number of instances
      script: './node_modules/nuxt/bin/nuxt.js',
      args: 'start',
    }
  ],
  deploy : {
    // "production" is the environment name
    production : {
      "key": "/home/tobias/.ssh/apis.pem",
      "user": 'ubuntu',
      "host": ['myipaddresshere'],
      "ref": 'origin/master',
      "repo": 'git@github.com:myusername/my-frontend.git',
      "ssh_options": ['ForwardAgent=yes'],
      "path": '/home/ubuntu/frontend',
      "pre-deploy-local": `scp -i /home/tobias/.ssh/apis.pem /home/tobias/projects/my-frontend/.env ubuntu@random-ip-address9.eu-north-1.compute.amazonaws.com:/home/ubuntu/frontend/current`,
      "post-deploy" : 'npm install && npm run build && pm2 startOrRestart ecosystem.config.js'
    }
  }
}

I'm thinking that I either:

  1. Somehow try to build the project locally instead and scp it over
  2. Find a more elegant way of using the way it's done right now but possibly copy/move around folders on the remote server, not really sure. The problem is that once the build starts it replaces the files that are being used for the live server. PM2 does have it's reload function so maybe that's better than to use startOrRestart.

Any advice on how to prevent the downtime from the build would be appreciated.

0 Answers
Related