Skip to content

Instantly share code, notes, and snippets.

@rawilk
Last active July 7, 2022 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rawilk/95ef8d438e4a38e8ccb39d8844247539 to your computer and use it in GitHub Desktop.
Save rawilk/95ef8d438e4a38e8ccb39d8844247539 to your computer and use it in GitHub Desktop.
Laravel Forge "Zero-Downtime" Deployment
cd /home/forge
PROJECT_NAME="site-name"
PROJECT_REPO="git@github.com:github-username/repo-name.git"
RELEASES_KEPT=3
RELEASE=$(date +"%Y-%m-%d-%H-%M-%S")
# Reload PHP-FPM.
( flock -w 10 9 || exit 1
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
# Create our master directory for .env and storage if they don't exist.
# This should only need to be done the first time we deploy with this method.
# We will symlink to these in each release to avoid having to copy them each time.
if [ ! -d $PROJECT_NAME"-data" ]
then
mkdir $PROJECT_NAME"-data"
cp $PROJECT_NAME"/.env" $PROJECT_NAME"-data/.env"
cp -r $PROJECT_NAME"/storage" $PROJECT_NAME"-data/storage"
fi
# Create a project-name-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 -b $FORGE_SITE_BRANCH --depth 1 $PROJECT_REPO $RELEASE
cd $RELEASE
# Install dependencies
$FORGE_COMPOSER install --no-interaction --prefer-dist --optimize-autoloader
# Symlink our .env and storage.
ln -sfn "../../"$PROJECT_NAME"-data/.env" .env
rm -rf storage
ln -sfn "../../"$PROJECT_NAME"-data/storage" storage
if [ -f artisan ]
then
$FORGE_COMPOSER update-permissions # Seed any new permissions
$FORGE_PHP artisan route:cache
$FORGE_PHP artisan view:cache
$FORGE_PHP artisan event:cache
$FORGE_PHP artisan horizon:terminate
$FORGE_PHP artisan storage:link
$FORGE_PHP artisan config:cache
fi
# Compile our assets.
npm install
npm run build
# Now that our scripts are compiled, we can safely remove the node directory.
rm -rf node_modules
# Clean up old releases.
cd ..
rm -rf `ls -1 | sort -r | tail -n +$((RELEASES_KEPT+1))`
cd /home/forge
# Remove the project directory if it exists.
if [ -d $PROJECT_NAME ]
then
rm -rf $PROJECT_NAME
fi
# Re-link our new release.
ln -sfn $PROJECT_NAME"-releases/"$RELEASE $PROJECT_NAME
@rawilk
Copy link
Author

rawilk commented Jul 6, 2022

Replace "github-username" and "repo-name" with the correct path to the repo before running deployments with this script.

Replace "site-name" with your app's domain name.

@rawilk
Copy link
Author

rawilk commented Jul 6, 2022

Also modify the artisan commands that are run depending on the project's needs and requirements.

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