Logo

dev-resources.site

for different kinds of informations.

Automatic deploys with PM2, Caddy, and GitHub Actions

Published at
9/6/2022
Categories
devops
github
pm2
Author
aaronblondeau
Categories
3 categories in total
devops
open
github
open
pm2
open
Author
13 person written this
aaronblondeau
open
Automatic deploys with PM2, Caddy, and GitHub Actions

Back in the olden days I used to "deploy" applications by using FTP to transfer PHP code files on the server. Now, I love being able to push code to my Git repo and then see the app get deployed automatically. What I still miss though is how fast old school FTP deployments were. These days there is usually a container build step involved with deployments. Then there is the time it takes to transfer the containers, not to mention the extra costs involved with hosting a container repo, managing a cluster, running a load balancer, and so on.

Since containers solve problems that very few of my projects have, I wanted to see if I could cook up a relatively simple and low cost application hosting and deployment rig.

Ingredients:

Instructions:

  1. If you don't have one already, create an SSH key on your local machine that you can use to connect to your VM

  2. Create a Debian 11 VM on Vultr. I setup mine for only $3.50 per month. Add your SSH key during the config process.

  3. Once the VM is ready, install Caddy : https://caddyserver.com/docs/install

  4. Open the firewall to accept connections on ports 80, and 443

    sudo ufw allow 80
    sudo ufw allow 443
    
  5. Install node.js

    sudo apt update
    curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
    sudo apt -y install nodejs
    
  6. Install PM2 and Yarn

    npm install -g yarn pm2
    
  7. Configure PM2 to run on startup

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

    pm2 startup
    
  8. Install Git and setup SSH Key

    Since PM2 deploys will be using git to pull new code to the server, you'll need to make it easy for the server to access your repository. If the repo is public then you can skip this step.

    Begin by creating a new ssh key on the server

    ssh-keygen -t ed25519 -C "pm2 on server"
    

    Then copy contents of ~/.ssh/id_ed25519.pub into a new SSH key in your GitHub user account settings.

    You'll also need to make sure that GitHub is added to your list of known hosts on the server. The quickest way to do this is to do a quick checkout:

    cd
    git clone [email protected]:aaronblondeau/mpg2co2.git
    rm -Rf mpg2co2
    

    On this first checkout on the server you should be prompted to add github to the list of known hosts.

  9. Configure environment variables.

    I chose to simply add app environment variables to the .bashrc file on the server:

    Add to .bashrc

    export PGHOST=db.bit.io
    export PGUSER=HIDDEN
    export PGPASSWORD=HIDDEN
    export PGPORT=5432
    export PGDATABASE=HIDDEN
    export PGSSL=yes
    
  10. Create a PM2 config file

    I created two separate config files - one for staging and one for production. One file with both configs will work too:

    Info on PM2 configs is here : https://pm2.keymetrics.io/docs/usage/application-declaration/

  11. Create a directory for the app.

    This directory needs to match where you told PM2 to operate from when setting up the config file above : https://github.com/aaronblondeau/mpg2co2/blob/main/ecosystem.staging.config.js#L18

    mkdir /var/www
    
  12. Run PM2 setup and deploy

    These commands are run from your local development environment in order to setup the application and do an initial deploy.

    Note, if on Windows, you may get a "spawn sh ENOENT" error. You can resolve this by making sure "sh" is in your path : https://github.com/Unitech/pm2/issues/3839#issuecomment-484347776

    pm2 deploy ecosystem.production.config.js production setup
    pm2 deploy ecosystem.production.config.js production
    

    Your application should be running on the server at this point.

  13. Setup DNS records

    Make sure you've setup a DNS record that points your domain at the server. Note that Caddy is going to provide completely automatic SSL certificates!

  14. Configure Caddyfile reverse proxy

    Back on the server, add your domain to /etc/caddy/Caddyfile

    mpg2co2.com {
            reverse_proxy localhost:3001
    }
    
    www.mpg2co2.com {
            redir https://mpg2co2.com{uri}
    }
    

    Then tell caddy to reload the config (run this in /etc/caddy)

    sudo caddy reload
    

    At this point your application should be up and running on the server with its own domain name. You can also manually deploy updates from your development machine by running

    pm2 deploy ecosystem.production.config.js production
    
  15. Setup GitHub deploy action

    Like the PM2 files I setup a separate GitHub action file for each environment:

    I based these actions off this article : https://dev.to/goodidea/setting-up-pm2-ci-deployments-with-github-actions-1494

    The action:

    • Checks out the code
    • Configures ssh so the server can be accessed
    • Runs PM2 deploy
  16. Setup SSH config for GitHub actions

    Before you can use the GitHub actions you have to provide a way for GitHub to access your server.

    Run this command on your local dev box to create a new ssh key (choose to put the file in ./github_action_key and don't provide a passphrase):

    ssh-keygen -t ed25519 -C "github_deploy_action"
    

    Then, ssh into your server and add the contents of github_action_key.pub as new line in ~/.ssh/authorized_keys.

    Then, in GitHub, go to Repo Settings > Secrets > Actions > New Repository Secret, put github_action_key file's contents into a repository secret named SSH_PRIVATE_KEY

    Next, from your local dev box, connect to server with new key (ssh -i .\github_action_key [email protected]). You should be prompted to add an entry to your known_hosts file when you do this. If you don't you'll need to remove all existing entries for the address and try again. After you've been prompted to add the connection to known_hosts, open the file (~/.ssh/known_hosts) grab the entry and add it to a new repository secret in GitHub called SSH_KNOWN_HOSTS.

  17. Enjoy

    Once everything is setup you'll be able to push your code to staging/main branches in git and your code will automatically get deployed!

pm2 Article's
30 articles in total
Favicon
Deploy NestJS and NextJS application in same server using pm2 and Nginx
Favicon
Guia de Comandos PM2
Favicon
๐Ÿš€ Deploying Node.js Application with PM2, NGINX, and SSL Configuration ๐Ÿš€
Favicon
Monitoring PM2 in production
Favicon
Mastering PM2: Optimizing Node.js and Next.js Applications for Performance and Scalability
Favicon
Setting Up PM2 for Multi-User Access on Ubuntu Instance
Favicon
Manual deployment of NestJS and Angular applications on a dedicated server via "Docker Compose" and "PM2"
Favicon
Build applications on NestJS and Angular and run them in two versions: via PM2 and via Docker Compose
Favicon
An example of a simple update of NestJS-mod libraries
Favicon
Using pm2 to Manage Node.js Applications
Favicon
Using Screen and PM2 for Deploying, Debugging, and Running NestJS in Production
Favicon
Host Multiple Node Apps with nginx, pm2 with SSL certificate
Favicon
Node.js PM2 Orchestration Explained
Favicon
Managing Logs with PM2 and pm2-logrotate
Favicon
Streamlining PM2 Startup for Node.js Applications: A Comprehensive Guide
Favicon
Managing Next.js and NestJS Applications in Production with PM2
Favicon
Deploy a Full Stack Web App to VPS Server with NGINX andย PM2!
Favicon
Como utilizar o PM2 para gerenciar aplicaรงรตes
Favicon
Guide to Running a Node Server in Nx Monorepo using PM2 Process Manager
Favicon
How to run old nodejs Project into new nodejs project
Favicon
CentOS 7 NodeJS Kurulumu
Favicon
How to start node.js app with pm2
Favicon
How to start node.js app with pm2
Favicon
Deploying Multiple NodeJS Servers on a Single DigitalOcean Droplet; Managed by PM2, Without Using an ecosystem.config.js file
Favicon
Automatic deploys with PM2, Caddy, and GitHub Actions
Favicon
Utilizando PM2 (Basico)
Favicon
Experience on PM2 Tricks to manage your NodeJs processes
Favicon
Deploy Nest JS App using PM2 on Linux (Ubuntu) Server
Favicon
Install PM2 (Process Manager 2)
Favicon
Generate server block (virtual hosts) for nginx dynamically

Featured ones: