Skip to content

Main "prod.yml" CI‐CD

holadmex edited this page Jul 22, 2024 · 6 revisions

Welcome to the hng_boilerplate_expressjs wiki!

Hello, in this guide, we will walk you through setting up the production CI/CD workflow for our project. Before heading to production, certain prerequisites need to be completed under local development testing.

Pre-requisites

Installation prerequisite on the server

  • Nodejs/Npm installation Link Text
  • Nginx installation

Steps

Here are the steps that needs to be carried out during local development testing:

Clone the Project Repository

First, we need to clone our project repository in a directory/path accessible by everyone on the server (e.g, /var/www/aihomework/dev).

Note: Create the directory explicitly using mkdir aihomework if it does not exist.

cd /var/www/
sudo mkdir aihomework
cd /var/www/aihomework
sudo git clone <project-url> dev
cd /var/www/aihomework/dev

Install Dependencies

Navigate to the project repository and install the necessary dependencies.

cd /var/www/aihomework/dev
sudo yarn global add pnpm
yarn -v
sudo yarn install
sudo yarn start (execution only needed for testing on dev)
sudo yarn build
sudo yarn prod

On successful installation of the above commands, your application should be up and running and exposed to the default port 8080 using the server's IP address. (e.g http://ipaddress:8080).

While our local development application test is running successfully,

NB: Repeated step function above for dev will be executed for prod using /var/www/aihomework/ path.

sudo git clone <project-url> prod
cd /var/www/aihomework/prod

Moving on, will proceed to write the prod.yml CI/CD workflow, and also create our aihomeworkprod.service file, startappprod.sh file which will be created under server-script directory in our hng_boilerplate_expressjs root directory.

aihomeworkprod.service

[Unit]
Description=AIHomework-Prod
After=network.target

[Service]
WorkingDirectory=/var/www/aihomework/prod
ExecStart=/bin/bash /var/www/aihomework/prod/server-script/startappprod.sh
#Restart=on-failure
#RestartSec=20s
StartLimitInterval=0

[Install]
WantedBy=multi-user.target

startappprod.sh

#!/bin/bash

cd /var/www/aihomework/prod/
/usr/bin/yarn start

Create and configure your GitHub Actions Workflow for prod.yml on push to Dev Branch

The below prod.yml CI/CD workflow should be written in the dev branch and configured to trigger no push to the dev branch.

name: Build, Test, and Deploy for Prod Branch

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: self-hosted
    defaults:
      run:
        working-directory: /var/www/aihomework/prod

    steps:
      - name: Pull from GitHub
        run: |
          git fetch origin
          git reset --hard origin/main

      - name: Install dependencies
        run: yarn install

      - name: Build the dist
        run: yarn build

      - name: Setup service file
        run: sudo cp server-script/aihomeworkprod.service /etc/systemd/system

      - name: Start the app
        run: |
          sudo systemctl daemon-reload
          sudo systemctl restart aihomeworkprod

On execution of the above production server-script and prod.yml workflow setup, an open request has to be created for a deployment merge request to the main branch.

Additional NB:

When working with team Collaboration, it's important to follow these guidelines below:

Any code merge that takes place should be synchronized by the next teammate who will be pushing a new code change. This ensures that everyone is working with the most recent codebase. Always perform a git pull to sync with remote upstream code changes before pushing local commit changes.

Challenges encountered in the course of running this project include below comment:

New commit merges from the backend team, which require recurrent code changes in the package.json file to run our CI/CD for both development and production workflows on GitHub Actions.

Conclusion:

These are all the required installation and steps carried for the successful deployment of this project, to run a smooth and efficient CI/CD workflow.