Skip to content

Commit

Permalink
add temp build for zero downtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ibastawisi committed Aug 14, 2024
1 parent 15a0c94 commit 6578bc8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 86 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/oci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,4 @@ jobs:
# execute commands
script: |
cd ~/matheditor
git fetch
git reset --hard origin/master
npm ci
npx prisma migrate deploy
npm run build
pm2 reload "matheditor"
./deploy.sh
94 changes: 14 additions & 80 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,81 +1,15 @@
#!/bin/bash

#both app folders start with 'matheditor'
LIVE_DIR_PATH=~/matheditor

#add the suffixes to the app folders
BLUE_DIR=$LIVE_DIR_PATH/blue
GREEN_DIR=$LIVE_DIR_PATH/green

# set up variables to store the current live and deploying to directories
CURRENT_LIVE_NAME=false
CURRENT_LIVE_DIR=false

DEPLOYING_TO_NAME=false
DEPLOYING_TO_DIR=false

# this checks if the green and blue apps are running
GREEN_ONLINE=$(pm2 jlist | jq -r '.[] | select(.name == "matheditor-green") | .name, .pm2_env.status' | tr -d '\n\r')
BLUE_ONLINE=$(pm2 jlist | jq -r '.[] | select(.name == "matheditor-blue") | .name, .pm2_env.status' | tr -d '\n\r')

# if blue is running, set the current live to blue and 'deploying to' is green
if [ "$BLUE_ONLINE" == "matheditor-blueonline" ]; then
echo "Blue is running"
CURRENT_LIVE_NAME="matheditor-blue"
CURRENT_LIVE_DIR=$BLUE_DIR

DEPLOYING_TO_NAME="matheditor-green"
DEPLOYING_TO_DIR=$GREEN_DIR
echo "Deploy starting..."
git fetch
git reset --hard origin/master
npm ci
npx prisma migrate deploy
npm install || exit
BUILD_DIR=temp npm run build || exit
if [ ! -d "temp" ]; then
echo '\033[31m temp Directory not exists!\033[0m'
exit 1;
fi

# if green is running, set the current live to green and 'deploying to' is blue
if [ "$GREEN_ONLINE" == "matheditor-greenonline" ]; then
echo "Green is running"
CURRENT_LIVE_NAME="matheditor-green"
CURRENT_LIVE_DIR=$GREEN_DIR

DEPLOYING_TO_NAME="matheditor-blue"
DEPLOYING_TO_DIR=$BLUE_DIR
fi

# display the current live and deploying to directories
echo "Current live: $CURRENT_LIVE_NAME"
echo "Deploying to: $DEPLOYING_TO_NAME"

echo "Current live dir: $CURRENT_LIVE_DIR"
echo "Deploying to dir: $DEPLOYING_TO_DIR"

# Navigate to the deploying to directory
cd $DEPLOYING_TO_DIR || { echo 'Could not access deployment directory.' ; exit 1; }

# Pull the latest changes
# clear any changes to avoid conflicts (usually permission based)
git fetch || { echo 'Git fetch command failed' ; exit 1; }
git reset --hard origin/master || { echo 'Git reset command failed' ; exit 1; }

# install the dependencies
npm ci || { echo 'npm clean install failed' ; exit 1; }
# run migrations
npx prisma migrate deploy || { echo 'Prisma migrate deploy failed' ; exit 1; }
# Build the project
npm run build || { echo 'Build failed' ; exit 1; }

# start the pm2 process
pm2 start npm --name $DEPLOYING_TO_NAME -- start || { echo 'pm2 start failed' ; exit 1; }

# add a delay to allow the server to start
sleep 5

# check if the server is running
DEPLOYMENT_ONLINE=$(pm2 jlist | jq -r '.[] | select(.name == "$DEPLOYING_TO_NAME") | .name, .pm2_env.status' | tr -d '\n\r')

if [ "$DEPLOYMENT_ONLINE" == "$DEPLOYING_TO_NAMEonline" ]; then
echo "Deployment successful"
else
echo "Deployment failed"
exit 1
fi

# update process list
pm2 delete $CURRENT_LIVE_NAME
pm2 save
rm -rf .next
mv temp .next
pm2 reload matheditor --update-env
echo "Deploy done."
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const withPWA = require("./next-pwa")({
/** @type {import('next').NextConfig} */
const config = {
reactStrictMode: false,
distDir: process.env.BUILD_DIR || '.next',
/** @param { import('webpack').Configuration } config */
webpack: (config, { isServer }) => {
if (isServer) {
Expand Down

0 comments on commit 6578bc8

Please sign in to comment.