Last active
August 22, 2023 19:47
-
-
Save dees040/ac0d5fa33f7ceddc84b95a12719fab03 to your computer and use it in GitHub Desktop.
Deploy a Vue.JS app on Laravel Forge with zero downtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's not a 'noob' comment 😄! What you do (or at least I did) is the following:
/home/forge
directory named.bin
, so the absolute path:/home/forge/.bin
/home/forge/.bin/deploy_site
chmod +x /home/forge/.bin/deploy_site
(you might need sudo for this)export PATH=$PATH:/home/forge/.bin
.I hope this helps you out!