Last active
February 5, 2025 16:24
-
-
Save internetztube/aeff937ed4886579a82356c773396175 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Atomic Deployment with Laravel Forge | |
# Native .env handling via Laravel Forge GUI | |
# Support for nodejs via nvm. | |
# Persistent Folders! | |
# No need for Envoyer! | |
# Installation | |
# * Update CONF_GIT_REPOSITORY to match your Git Repository. | |
# * Update persistent folders as needed. | |
# * Add this Script as Deploy Script into your Laravel Forge Application. | |
# * Set Web Directory on your Laravel Forge Application to "/current/YOUR_PUBLIC_PATH" | |
[email protected]:user/repo.git | |
CONF_KEEP_RELEASES=3 | |
FOLDER_CLONE_TEMP="$FORGE_SITE_PATH/clone-temp" | |
FOLDER_CLONE_TEMP_GIT="$FOLDER_CLONE_TEMP/.git" | |
FOLDER_REPOSITORY="$FORGE_SITE_PATH/repository" | |
FOLDER_REPOSITORY_GIT="$FOLDER_REPOSITORY/.git" | |
FOLDER_PERSISTENT="$FORGE_SITE_PATH/persistent" | |
FOLDER_RELEASES="$FORGE_SITE_PATH/releases" | |
FOLDER_RELEASE_NOW_BUILDING="$FOLDER_RELEASES/$FORGE_DEPLOYMENT_ID" | |
FOLDER_RELEASE_LIVE="$FORGE_SITE_PATH/current" | |
FILE_ENV_LIVE="$FOLDER_RELEASE_LIVE/.env" | |
cd $FORGE_SITE_PATH | |
# Reinitialize repository for first deployment. | |
if [ ! -d "$FOLDER_REPOSITORY_GIT" ]; then | |
rm -rf $FORGE_SITE_PATH | |
mkdir -p $FORGE_SITE_PATH | |
mkdir -p $FOLDER_REPOSITORY | |
cd $FORGE_SITE_PATH | |
git clone $CONF_GIT_REPOSITORY $FOLDER_CLONE_TEMP | |
mv $FOLDER_CLONE_TEMP_GIT $FOLDER_REPOSITORY | |
rm -rf $FOLDER_CLONE_TEMP | |
fi | |
# Create new release folder. | |
mkdir -p $FOLDER_RELEASE_NOW_BUILDING | |
# Copy .git from repository | |
cp -r $FOLDER_REPOSITORY_GIT $FOLDER_RELEASE_NOW_BUILDING | |
# Pull git repository. | |
cd $FOLDER_RELEASE_NOW_BUILDING | |
git pull origin $FORGE_SITE_BRANCH | |
git reset --hard | |
cd $FORGE_SITE_PATH | |
# Composer! | |
cd $FOLDER_RELEASE_NOW_BUILDING | |
$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader | |
cd $FORGE_SITE_PATH | |
# Make sure all persistent folders exist. | |
mkdir -p $FOLDER_PERSISTENT/storage | |
mkdir -p $FOLDER_PERSISTENT/storage/app | |
mkdir -p $FOLDER_PERSISTENT/storage/app/public | |
mkdir -p $FOLDER_PERSISTENT/storage/logs | |
mkdir -p $FOLDER_PERSISTENT/storage/framework | |
mkdir -p $FOLDER_PERSISTENT/storage/framework/cache | |
mkdir -p $FOLDER_PERSISTENT/storage/framework/sessions | |
mkdir -p $FOLDER_PERSISTENT/storage/framework/testing | |
mkdir -p $FOLDER_PERSISTENT/storage/framework/views | |
# Link persistent folders. | |
rm -rf $FOLDER_RELEASE_NOW_BUILDING/storage | |
ln -s $FOLDER_PERSISTENT/storage $FOLDER_RELEASE_NOW_BUILDING/storage | |
rm -rf $FOLDER_RELEASE_NOW_BUILDING/public/storage | |
ln -s $FOLDER_PERSISTENT/storage/app/public $FOLDER_RELEASE_NOW_BUILDING/public/storage | |
# Build Frontend (requires https://forgerecipes.com/recipes/266) | |
# DO NOT source nvm.sh in folder with .nvmrc file! https://github.com/nvm-sh/nvm/issues/1985 | |
cd $FORGE_SITE_PATH | |
source /home/forge/.nvm/nvm.sh | |
cd $FOLDER_RELEASE_NOW_BUILDING | |
nvm install && nvm use | |
node -v | |
npm install | |
npm run production | |
cd $FORGE_SITE_PATH | |
# Copy over .env from current release. | |
if [ -f "$FILE_ENV_LIVE" ]; then | |
cp $FILE_ENV_LIVE $FOLDER_RELEASE_NOW_BUILDING | |
else | |
echo "WARN! $FILE_ENV_LIVE does not exist!" | |
# Recommended: Uncomment this exit 1 after you ran your first deployment! | |
# exit 1 | |
fi | |
# Switch out folders. | |
touch $FOLDER_RELEASE_LIVE | |
rm $FOLDER_RELEASE_LIVE | |
ln -s $FOLDER_RELEASE_NOW_BUILDING $FOLDER_RELEASE_LIVE | |
# Cleanup | |
cd $FOLDER_RELEASES | |
ls -t | tail -n +$((CONF_KEEP_RELEASES + 1)) | xargs rm -rf | |
cd $FORGE_SITE_PATH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment