Last active
December 8, 2023 10:39
-
-
Save osbre/dc38f40adcfd0c800db312feb2998db4 to your computer and use it in GitHub Desktop.
This file contains 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
# 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