Files
Virtnet.bond/gitsync.sh

187 lines
5.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
<<<<<<< HEAD
BAR_WIDTH=50
SLEEP_INTERVAL=0.05
CLEAR_LINE="\033[K"
LOG_FILE="/var/log/laravel_deploy.log"
GREEN="\033[32m"
RESET="\033[0m"
STEPS=(
"Pull latest code"
"Maintenance mode"
"Restart PHP-FPM"
"Clear caches"
"Optimize app"
"Set permissions"
"Ensure queue running"
"Reset OPcache"
"Bring app online"
"Check php-fpm"
"Check paymenter"
)
CMDS=(
"git pull --rebase --autostash --quiet"
"php artisan down --render='errors::503' --retry=60 --quiet || true"
"systemctl restart php8.3-fpm.service"
"php artisan optimize:clear --quiet"
"php artisan optimize --quiet"
"find storage bootstrap/cache -not -perm 775 -type d -exec chmod 775 {} + -o -not -perm 664 -type f -exec chmod 664 {} +; chown -R www-data:www-data storage bootstrap/cache"
"if ! pgrep -f 'artisan queue:work' >/dev/null; then nohup php artisan queue:work --daemon --quiet >> /dev/null 2>&1 & fi"
"php -r 'if(function_exists(\"opcache_reset\")) opcache_reset();' 2>/dev/null || true"
"php artisan up --quiet"
"systemctl is-active --quiet php8.3-fpm || systemctl restart php8.3-fpm"
"systemctl is-active --quiet paymenter.service || systemctl restart paymenter.service"
)
rollback(){
echo -e "\n\033[31m✘ Deployment failed. Rolling back...\033[0m"
git reset --hard HEAD@{1} >> "$LOG_FILE" 2>&1
php artisan up || true
exit 1
}
trap rollback ERR
draw_bar() {
local percent=$1
local width=$2
local text="$3"
# Strip color codes for length calculation
local clean_text=$(echo -ne "$text" | sed 's/\x1b\[[0-9;]*m//g')
[ ${#clean_text} -gt $width ] && clean_text="${clean_text:0:$width}"
local filled=$(( percent * width / 100 ))
[ $filled -gt $width ] && filled=$width
local text_start=$(( (width - ${#clean_text}) / 2 ))
local text_end=$(( text_start + ${#clean_text} ))
local bar=""
for ((i=0;i<width;i++)); do
if (( i >= text_start && i < text_end )); then
bar+="${clean_text:i-text_start:1}"
else
if (( i < filled )); then
bar+="#"
else
bar+="-"
fi
fi
done
printf "[%s] %3d%%" "$bar" "$percent"
}
run_deployment() {
local total_steps=${#STEPS[@]}
for i in "${!STEPS[@]}"; do
step_name="${STEPS[$i]}"
cmd="${CMDS[$i]}"
step_percent_start=$(( i * 100 / total_steps ))
step_percent_end=$(( (i+1) * 100 / total_steps ))
bash -c "$cmd" > >(tee -a "$LOG_FILE") 2> >(tee -a "$LOG_FILE" >&2) &
pid=$!
percent=$step_percent_start
while kill -0 "$pid" 2>/dev/null; do
[ "$percent" -lt "$step_percent_end" ] && percent=$((percent+1))
echo -ne "\r$(draw_bar $percent $BAR_WIDTH "$step_name")$CLEAR_LINE"
sleep "$SLEEP_INTERVAL"
done
wait "$pid"
# Fill bar to end of step
echo -ne "\r$(draw_bar $step_percent_end $BAR_WIDTH "$step_name")$CLEAR_LINE"
done
# Final green completion bar
final_text="Deployment Complete"
# Construct bar normally
bar=$(draw_bar 100 $BAR_WIDTH "$final_text")
# Replace the visible text in the middle with green text
clean_text_len=${#final_text}
start=$(( (BAR_WIDTH - clean_text_len) / 2 + 1 )) # +1 for the [
end=$(( start + clean_text_len - 1 ))
colored_bar="${bar:0:start-1}${GREEN}${final_text}${RESET}${bar:end}"
echo -e "\r$colored_bar$CLEAR_LINE"
# echo "Log: $LOG_FILE"
}
run_deployment
=======
LOGFILE="/var/log/laravel_deploy.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOGFILE"
}
rollback() {
log "⚠️ Deployment failed — attempting rollback!"
git reset --hard HEAD@{1}
php artisan up || true
log "Rollback complete."
exit 1
}
trap rollback ERR
log "=== 🚀 Laravel Deployment Started ==="
log "Checking for uncommitted changes..."
if ! git diff --quiet || ! git diff --cached --quiet; then
log "❌ Uncommitted changes detected. Aborting."
exit 1
fi
log "Putting application into maintenance mode..."
php artisan down --render="errors::503" --retry=60 || true
log "Pulling latest code..."
git pull --rebase --autostash
log "Restarting PHP-FPM..."
systemctl restart php8.3-fpm.service
log "Clearing old caches..."
php artisan optimize:clear
log "Rebuilding caches..."
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
php artisan package:discover --ansi
php artisan optimize
log "Setting permissions safely..."
find storage -type d -exec chmod 775 {} \;
find storage -type f -exec chmod 664 {} \;
find bootstrap/cache -type d -exec chmod 775 {} \;
find bootstrap/cache -type f -exec chmod 664 {} \;
chown -R www-data:www-data storage bootstrap/cache
log "Restarting queue workers..."
if ! pgrep -f "artisan queue:work" > /dev/null; then
nohup php artisan queue:work --daemon --quiet >> /dev/null 2>&1 &
log "Queue worker started."
else
log "Queue worker already running."
fi
log "Resetting OPcache if available..."
php -r "if (function_exists('opcache_reset')) opcache_reset();" || true
log "Bringing application back online..."
php artisan up
log "=== ✅ Deployment completed successfully ==="
>>>>>>> 9b78ad7da723c83d8b82b08e07fb245a28cf5879