Skip to content

Instantly share code, notes, and snippets.

@mrl22
Created November 15, 2024 17:43
Show Gist options
  • Save mrl22/678478468e39bcdb79dde288b83eca6b to your computer and use it in GitHub Desktop.
Save mrl22/678478468e39bcdb79dde288b83eca6b to your computer and use it in GitHub Desktop.
Deploy script for custom php application
#!/usr/bin/bash
BASE_DIR="/home/user/sites/site.com"
GIT_SSH=""
BRANCH="master"
SHARED=("failed" "temp" "uploads" "working") # root directories that will be moved into shared storage and symlinked on each deploy
# Dont forget to setup your SSH key in Github
##############################
echo "Starting deployment..."
# Define paths using Forge variables
RELEASES_DIR="$BASE_DIR/releases"
CURRENT_DIR="$BASE_DIR/current"
STORAGE_DIR="$BASE_DIR/shared"
TIMESTAMP=$(date +%Y%m%d%H%M%S)
RELEASE_DIR="$RELEASES_DIR/$TIMESTAMP"
GIT=$(command -v git)
COMPOSER=$(command -v composer)
# Validation checks
if [ -z "$GIT" ]; then
echo "Git not found. Please install it or add it to your PATH."
exit 1
fi
if [ -z "$COMPOSER" ]; then
echo "Composer not found. Please install it or add it to your PATH."
exit 1
fi
# Lets get started with a clone
if ! $GIT clone -b "$BRANCH" --single-branch "$GIT_SSH" "$RELEASE_DIR"; then
echo "Git clone failed. Exiting..."
exit 1
fi
# Move shared directories if they do not exist
for i in ${!SHARED[@]}; do
SRC_DIR="$RELEASE_DIR/${SHARED[$i]}"
DEST_DIR="$STORAGE_DIR/${SHARED[$i]}"
if [ ! -d "$DEST_DIR" ]; then
echo "Moving $SRC_DIR to $DEST_DIR"
mv "$SRC_DIR" "$DEST_DIR"
if [ ! -d "$DEST_DIR" ]; then
mkdir "$DEST_DIR"
fi
else
echo "Directory $DEST_DIR already exists. Skipping move."
rm -rf "$SRC_DIR"
fi
echo "Linking $SRC_DIR to $DEST_DIR"
ln -sf "$DEST_DIR" "$SRC_DIR"
done
echo "Installing Composer dependencies..."
if ! $COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader -d "$RELEASE_DIR"; then
echo "Composer install failed. Exiting..."
exit 1
fi
# Clean up
rm -rf "$RELEASE_DIR/.git"
# Switch symlink to new release
echo "Activating new release..."
ln -sfn "$RELEASE_DIR" "$CURRENT_DIR"
# Clean up old releases, keeping the latest 5
echo "Cleaning up old releases..."
ls -1tr "$RELEASES_DIR" | head -n -5 | xargs -d '\n' -I{} rm -rf "$RELEASES_DIR/{}"
echo "Deployment completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment