Skip to content

Instantly share code, notes, and snippets.

@fawzanm
Created August 10, 2019 06:12
Show Gist options
  • Save fawzanm/2769cc00745b5a7f4895722f8e9fd241 to your computer and use it in GitHub Desktop.
Save fawzanm/2769cc00745b5a7f4895722f8e9fd241 to your computer and use it in GitHub Desktop.
ZERO downtime deployment script for Laravel Forge
# Original credit for the script goes to https://skepticalhippo.io/laravel-forge/2017/05/21/zero-downtime-deployments-with-laravel-forge/
# I just added a tweak to make sure that composer install must succeed to proceed the deployment.
# You can use the same trick to terminate the deployment from any point (if there is an error)
# by checking the exit code of the last command you've executed.
# Any suggestions? comments are open.
# Have fun artisans!
# Mohamed Fawzan
# :)
cd /home/forge
# make sure to change the project name to match your project
PROJECT_NAME="example.com"
# repo link
PROJECT_REPO="git@github.com:fawzanm/example.com"
RELEASES_KEPT=3
RELEASE=$(date +"%Y-%m-%d-%H-%M-%S")
# Copy the old .env file
cp $PROJECT_NAME"/.env" $PROJECT_NAME".env"
# Create a default-releases directory if it does not exist.
if [ ! -d $PROJECT_NAME"-releases" ]
then
mkdir $PROJECT_NAME"-releases"
fi
# Create our new release.
cd $PROJECT_NAME"-releases"
git clone $PROJECT_REPO $RELEASE
cd $RELEASE
# Install dependencies
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev
retVal=$?
if [ $retVal -ne 0 ]; then
echo "Composer install failed. Exitting..."
exit $retVal
fi
echo "Composer Installation Completed."
# Move in the old .env file and clean up.
cp -af "../../"$PROJECT_NAME".env" ".env"
rm "../../"$PROJECT_NAME".env"
if [ -f artisan ]
then
php artisan migrate --force
fi
# Clean up old releases.
cd ..
rm -rf `ls -1 | sort -r | tail -n +$((RELEASES_KEPT+1))`
cd /home/forge
# Remove the project directory if exists.
if [ -d $PROJECT_NAME ]
then
rm -rf $PROJECT_NAME
fi
# Relink our new release.
ln -sfn $PROJECT_NAME"-releases/"$RELEASE $PROJECT_NAME
# Reload PHP-FPM
echo "" | sudo -S service php7.3-fpm reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment