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.
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
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.
To get setup on your remote server run this command:
pm2 deploy ecosystem.config.js production setup
I then created my
gitlab-ci.yml to look like this, I have skipped over the other stages to only show the deploy stage:
The before script is used to ensure we have ssh, bash and git so that pm2 can use them. This will also add the ssh key to the ssh-agent so that pm2 can ssh into your server.
Ensure you have the private
ssh_key added to your Gitlab secret variables as