-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzerodeploy.sh
More file actions
135 lines (109 loc) · 3.54 KB
/
zerodeploy.sh
File metadata and controls
135 lines (109 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
# Deployment configuration
APP_NAME="MaterialsCommons"
BASE_PATH="/var/www"
KEEP_RELEASES=5
REPOSITORY="your-git-repository-url"
BRANCH="main"
OPCACHE_CLEAR_URL="http://localhost/opcache-clear.php"
# Directory structure
CURRENT_RELEASE_DIR="${BASE_PATH}/current"
RELEASES_DIR="${BASE_PATH}/releases"
SHARED_DIR="${BASE_PATH}/shared"
TIMESTAMP=$(date +%Y%m%d%H%M%S)
NEW_RELEASE_DIR="${RELEASES_DIR}/${TIMESTAMP}"
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Function to log messages
log() {
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
}
error() {
echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: $1${NC}"
exit 1
}
warning() {
echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] WARNING: $1${NC}"
}
# Function to check if command succeeded
check_result() {
if [ $? -ne 0 ]; then
error "$1"
fi
}
# Verify running as appropriate user
if [ "$(whoami)" != "www-data" ]; then
error "Script must be run as www-data user"
fi
# Create required directories
log "Creating directory structure..."
mkdir -p ${RELEASES_DIR} ${SHARED_DIR}/storage ${SHARED_DIR}/vendor ${SHARED_DIR}/.env
check_result "Failed to create directory structure"
# Clone new release
log "Cloning repository..."
git clone --depth 1 -b ${BRANCH} ${REPOSITORY} ${NEW_RELEASE_DIR}
check_result "Failed to clone repository"
cd ${NEW_RELEASE_DIR}
check_result "Failed to change to release directory"
# Install composer dependencies
log "Installing Composer dependencies..."
composer install --no-dev --optimize-autoloader --no-interaction
check_result "Composer installation failed"
# Install npm dependencies and build assets
log "Installing NPM dependencies and building assets..."
npm ci
check_result "NPM installation failed"
npm run production
check_result "Asset compilation failed"
# Create symbolic links for shared resources
log "Creating symbolic links..."
ln -nfs ${SHARED_DIR}/.env ${NEW_RELEASE_DIR}/.env
ln -nfs ${SHARED_DIR}/storage ${NEW_RELEASE_DIR}/storage
ln -nfs ${SHARED_DIR}/vendor ${NEW_RELEASE_DIR}/vendor
# Clear caches
log "Clearing caches..."
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
# Run database migrations
log "Running database migrations..."
php artisan migrate --force
check_result "Database migration failed"
# Optimize Laravel
log "Optimizing Laravel..."
php artisan optimize
check_result "Laravel optimization failed"
# Make storage directory writable
chmod -R 775 ${SHARED_DIR}/storage
# Reload PHP-FPM in a non-disruptive way
log "Reloading PHP-FPM..."
sudo -u root systemctl reload php8.0-fpm
check_result "Failed to reload PHP-FPM"
# Switch current symlink to new release
log "Activating new release..."
ln -nfs ${NEW_RELEASE_DIR} ${BASE_PATH}/current_temp
mv -Tf ${BASE_PATH}/current_temp ${CURRENT_RELEASE_DIR}
check_result "Failed to activate new release"
# Clear OPcache
log "Clearing OPcache..."
curl -s -X GET "${OPCACHE_CLEAR_URL}" > /dev/null
check_result "Failed to clear OPcache"
# Queue workers restart (in background)
log "Restarting queue workers..."
sudo -u root systemctl restart laravel-worker
check_result "Failed to restart queue workers"
# Clean up old releases
log "Cleaning up old releases..."
cd ${RELEASES_DIR}
ls -t | tail -n +$((${KEEP_RELEASES} + 1)) | xargs -r rm -rf
# Verify deployment
log "Verifying deployment..."
curl -s -I "http://localhost/health-check" | grep "200 OK" > /dev/null
if [ $? -ne 0 ]; then
warning "Health check failed, consider rolling back"
fi
log "Deployment completed successfully!"