Skip to content

Instantly share code, notes, and snippets.

@dees040
Last active August 22, 2023 19:47
Show Gist options
  • Save dees040/ac0d5fa33f7ceddc84b95a12719fab03 to your computer and use it in GitHub Desktop.
Save dees040/ac0d5fa33f7ceddc84b95a12719fab03 to your computer and use it in GitHub Desktop.
Deploy a Vue.JS app on Laravel Forge with zero downtime
#!/bin/bash
set -e
SITE=$1
HOME="/home/forge"
DESTINATION="$HOME/$SITE"
TARGET="$HOME/deployments/$SITE/current"
function display_usage() {
echo "deploy_site: missing destination source";
exit 1;
}
function check_if_dir_exists() {
if [ -d "$DESTINATION" ]; then
echo "Directory $DESTINATION found, starting deployment."
else
echo "$DESTINATION not found."
exit;
fi
}
function mkdir_if_needed() {
if [ ! -d "$HOME/deployments" ]; then
mkdir "$HOME/deployments"
fi
if [ ! -d "$HOME/deployments/$SITE" ]; then
mkdir "$HOME/deployments/$SITE"
fi
if [ -d "$TARGET" ]; then
rm "$TARGET" -rf
fi
mkdir "$TARGET"
if [ ! -d "$HOME/deployments/$SITE/build" ]; then
mkdir "$HOME/deployments/$SITE/build"
fi
}
function run_deployment_proccess() {
cp -a "$DESTINATION/." $TARGET -R
cd $TARGET
# Put your project specify code here. The code below is for a Vue.JS app.
if [ -e yarn.lock ]; then
yarn
yarn build || rollback_build
else
npm install
npm run build || rollback_build
fi
}
function serve_build() {
rm "$HOME/deployments/$SITE/build" -rf
mv "$DESTINATION" "$HOME/deployments/$SITE/build"
mv "$TARGET" "$DESTINATION"
echo "Served build with success"
}
function rollback_build() {
if [ ! -d "$HOME/deployments/$SITE/build" ]; then
echo "Build failed. Couldn't find a backup to rollback to."
else
rm $DESTINATION -rf
mv "$HOME/deployments/$SITE/build" $DESTINATION
echo "Build failed. Rollback to latest backup."
fi
exit 1
}
function start_deployment() {
mkdir_if_needed
run_deployment_proccess
serve_build
}
if [ -z "$SITE" ]; then
display_usage
fi
check_if_dir_exists
start_deployment
@dees040
Copy link
Author

dees040 commented Jan 13, 2018

Deploy Script on Forge:

cd /home/forge/{site}
git pull origin develop

deploy_script {site}

You could use this script for Laravel apps as well. Just add your commands on line 52.

@flyingcoder
Copy link

sorry for the noob comment but I wanted to ask how to use this. I tried to command deploy_script but it says command not found.

@dees040
Copy link
Author

dees040 commented Sep 27, 2018

It's not a 'noob' comment 😄! What you do (or at least I did) is the following:

  1. Create a folder in your /home/forge directory named .bin, so the absolute path: /home/forge/.bin
  2. Put the file there, so the path is: /home/forge/.bin/deploy_site
  3. Add execute rights to the file: chmod +x /home/forge/.bin/deploy_site (you might need sudo for this)
  4. Add the folder to your path to execute the command from everywhere: export PATH=$PATH:/home/forge/.bin.
  5. Change your deployment script in forge:
cd /home/forge/domain.com
git pull origin master

deploy_script domain.com

I hope this helps you out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment