Skip to content

Instantly share code, notes, and snippets.

@osbre
Last active December 8, 2023 10:39
Show Gist options
  • Save osbre/dc38f40adcfd0c800db312feb2998db4 to your computer and use it in GitHub Desktop.
Save osbre/dc38f40adcfd0c800db312feb2998db4 to your computer and use it in GitHub Desktop.
# Set default values
repository="git@github.com:user/project.git"
branch="main"
new_release=$(date '+%s')
set -e
mkdir -p releases
cd releases || exit 1
function deploy() {
prepare_release
activate_release
cleanup_old_releases
}
function ensure_directory_is_releases() {
if [ "$(basename "$(pwd)")" != "releases" ]; then
echo "❌ The current working directory must be 'releases'."
exit 1
fi
}
function rollback() {
echo "βͺ Rolling back to the previous release..."
ensure_directory_is_releases
ln -sfn "$(ls -1d */ | tail -n 2 | head -n 1)" current
}
function prepare_release() {
echo "πŸš€ Preparing release $new_release..."
ensure_directory_is_releases
git clone --branch "$branch" "$repository" "$new_release"
cd "$new_release" || exit 1
[ -n "$commit" ] && git checkout --detach "$commit"
composer install --no-dev --optimize-autoloader --no-interaction
pnpm install --frozen-lockfile
pnpm run build
ln -s ../../.env .env
cd ../ || exit 1
}
function activate_release() {
echo "πŸ”„ Activating release $new_release..."
ensure_directory_is_releases
ln -sfn "$new_release" current
cd current || exit 1
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan queue:restart
cd ../ || exit 1
}
function cleanup_old_releases() {
echo "πŸ—‘οΈ Cleaning up old releases..."
ensure_directory_is_releases
ls -1d */ | awk 'NR <= (NF-5) { print }' | xargs rm -rf
}
# Check if any arguments are provided
if [ $# -eq 0 ]; then
echo "Commands:"
echo " πŸš€ deploy Deploy the latest commit."
echo " πŸš€ deploy <commit> <branch> Deploy a specific commit. Branch defaults to $branch."
echo " βͺ rollback Rollback to the previous release."
exit 1
fi
# Determine the action to perform
case $1 in
"deploy")
if [ $# -ge 2 ]; then
commit=$2
branch=${3:-$branch}
echo "πŸš€ Deploying specific commit $commit on branch $branch..."
deploy
else
echo "πŸš€ Deploying latest commit on branch $branch..."
deploy
fi
;;
"rollback")
rollback
;;
*)
echo "❌ Invalid action. Please use 'deploy' or 'rollback'."
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment