Setting up CI/CD

Having a local project is great, but if it's going to be any good, I need to get it deployed onto a server.  I use Linode servers mostly - they are cheap to start, and easily expanded into larger capacity as needed.  Plus with the magic of nginx, I can serve multiple projects from the same server as distinct instances very easily.  So let's get into putting our boilerplate project onto a server as boilerplate.pawpawshouse.com

Create github actions yml file

My repo is in github, and with the help of the @appleboy/ssh-action library, it's really easy to set this up.  Let's go through the sections

This is the top of the file - setting up the job:

  • Names the workflow - in this case 'CI'

  • Sets the triggers - in this case, I want to trigger the workflow anytime there is a new push to the main branch, and also to allow me to trigger the workflow from the github UI.

  • #Basic workflow to deploy our boilerplate project

  • name: CI

  • # Controls when the action will run.

  • on:

  • # Triggers the workflow on push or pull request events but only for the main branch

    • push:

      • branches: [ main ]

  • # Allows you to run this workflow manually from the Actions tab

  • workflow_dispatch:

Here's where the work gets done...  Something to note is that github allows you to define "secrets" in your workflow - more on that in a second, but where you see ${{ secrets.USERNAME }} - that means the username is being stored securely as a secret, so we can commit the yml file to a public repo without exposing any credentials.

  • Define the OS for the VM that will run the deployment - ubuntu-latest this time

  • Setup the script for the ssh-action/master library to use

    • Change to the target folder ${{ secrets.PATH }}

    • Sets some ID for the apostrophe release - this needs to be present for the deployment to work

    • Pulls the repo - note if you are pulling a private repo, then you'll need to include a Personal Access Token in this string, like git pull https://${{ secrets.PAT }}:[email protected]/bulldoguk/a3-bulldoguk-boilerplate.git main

    • Installs libraries and runs the build task

    • (Re)starts the instance with PM2

    • Saves the running PM2 list to make sure this instance gets restarted if the server reboots

  • # A workflow run is made up of one or more jobs that can run sequentially or in parallel

  • jobs:

    • deploy:

      • runs-on: ubuntu-latest

      • steps:

      • - name: SSH and deploy node app

        • uses: appleboy/ssh-action@master

        • with:

If you check your github actions for this repo, you'll see that the deployment failed - but that's OK - we need to setup our secrets!  So go to settings>secrets in github and add your settings for HOSTIP, USERNAME and PASSWORD

For the first time, you will also need to clone the repo to your server.  That's the first and only time you should ever need to use the CLI in your server for this.

While you are connected to the CLI - go ahead and create the data/local.js file with your MongoDB connection details in it - remember this is NOT held in your repo.