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 |
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.
It's not a 'noob' comment 😄! What you do (or at least I did) is the following:
- Create a folder in your
/home/forge
directory named.bin
, so the absolute path:/home/forge/.bin
- Put the file there, so the path is:
/home/forge/.bin/deploy_site
- Add execute rights to the file:
chmod +x /home/forge/.bin/deploy_site
(you might need sudo for this) - Add the folder to your path to execute the command from everywhere:
export PATH=$PATH:/home/forge/.bin
. - 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
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.