-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployer.sh
149 lines (130 loc) · 3.09 KB
/
deployer.sh
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
set -e # Exit on any error
APP_DIR="/html"
DEPLOY_DIR="$APP_DIR/deployer"
TAR_FILE="$DEPLOY_DIR/repository.tar.gz"
DEPLOY_REPO_DIR="$DEPLOY_DIR/repository"
BACKUP_DIR="$DEPLOY_DIR/backup-$(date +%Y%m%d%H%M%S)"
LOG_FILE="$APP_DIR/deployment-$(date +%Y%m%d%H%M%S).log"
LOCKFILE="/tmp/deployment.lock"
EXCLUDES=(
".git"
".env"
".github"
".gitignore"
".htaccess"
"Thumbs.db"
"readme.md"
"platform/"
"wp-admin/"
"wp-includes/"
"wp-content/mu-plugins/"
"wp-content/themes/twenty*/"
"wp-content/uploads/"
"*.log"
"deployer/"
"index.php"
"plat-cron.php"
"wp-activate.php"
"wp-blog-header.php"
"wp-comments-post.php"
"wp-cron.php"
"wp-links-opml.php"
"wp-load.php"
"wp-login.php"
"wp-mail.php"
"wp-settings.php"
"wp-signup.php"
"wp-trackback.php"
"xmlrpc.php"
)
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
acquire_lock() {
if [ -e "$LOCKFILE" ]; then
log "Another instance of the deployment script is already running."
exit 1
fi
# lock released on script exit
trap 'release_lock' EXIT
touch "$LOCKFILE"
}
release_lock() {
rm -f "$LOCKFILE"
}
validate_prerequisites() {
if [ ! -f "$TAR_FILE" ]; then
log "Error: TAR file not found at $TAR_FILE."
exit 1
fi
if ! command -v wp &>/dev/null; then
log "Error: WordPress CLI (wp) is not installed or not in PATH."
exit 1
fi
}
# Create required directories
create_directories() {
log "Creating deployment and backup directories..."
mkdir -p "$DEPLOY_REPO_DIR"
mkdir -p "$BACKUP_DIR"
}
extract_tar() {
log "Extracting tar file to $DEPLOY_REPO_DIR..."
tar -xzf "$TAR_FILE" -C "$DEPLOY_REPO_DIR" || {
log "Error: Failed to extract tar file."
exit 1
}
}
sync_files() {
log "Synchronizing files to $APP_DIR with backup..."
RSYNC_EXCLUDES=""
for EXCLUDE in "${EXCLUDES[@]}"; do
RSYNC_EXCLUDES+="--exclude=$EXCLUDE "
done
rsync -avz --checksum --backup --backup-dir="$BACKUP_DIR" --suffix="" $RSYNC_EXCLUDES "$DEPLOY_REPO_DIR/" "$APP_DIR/" || {
log "Error: Failed to synchronize files."
exit 1
}
}
## do check health before, pass flag from workflow
check_wordpress_health() {
log "Checking WordPress health..."
if ! wp core is-installed --path="$APP_DIR"; then
log "Error: WordPress health check failed. Restoring backup..."
rsync -avz "$BACKUP_DIR/" "$APP_DIR/" || log "Error: Failed to restore backup."
log "Backup restored."
exit 1
fi
}
flush_cache() {
log "Flushing WordPress cache..."
if wp cache flush --path="$APP_DIR"; then
log "Cache flushed successfully."
else
log "Warning: Cache flush failed, but deployment was successful."
fi
}
persist_backup() {
log "Move backup to some other directory"
}
cleanup() {
log "Cleaning up temporary files and directories..."
rm -rf "$DEPLOY_REPO_DIR"
rm -f "$TAR_FILE"
log "Cleanup completed."
}
main() {
log "Starting deployment..."
validate_prerequisites
create_directories
extract_tar
sync_files
check_wordpress_health
flush_cache
persist_backup
cleanup
log "Deployment completed successfully."
}
acquire_lock
main