PM2 Deployment via Gitlab CI

Matt Chaffe
2 min readNov 7, 2017
https://unsplash.com/photos/NneNzn0vs44

If you have ever used Heroku and loved how smooth the experience is with their git based deployments. I recently set out to create a similar experience with Gitlab CI and pm2. Here is how I configured Gitlab CI to deploy a Node.js app using pm2 to my GCE instance.

Local PM2

I configured pm2 using the pm2 ecosystem command which creates a local config file, I then edited it to look like the following:

This sets out a production configuration. Which will ssh into your remote server which you should specify by changing USERNAME HOST and REPO_ADDRESS. Once connected it will update the remote git repo, npm install to ensure all dependencies are up to date and then will restart the pm2 instance.

Important

To get setup on your remote server run this command:

pm2 deploy ecosystem.config.js production setup

Gitlab

I then created my gitlab-ci.yml to look like this, I have skipped over the other stages to only show the deploy stage:

--

--