-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
140 lines (120 loc) · 3.63 KB
/
deploy.sh
File metadata and controls
140 lines (120 loc) · 3.63 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
136
137
138
139
140
#!/bin/bash
# Configuration
REPO_PATH="/root/nexhacks-2026"
LOG_FILE="${REPO_PATH}/deploy.log"
DISCORD_WEBHOOK="${DISCORD_WEBHOOK_URL}"
GITHUB_REPO="nexhacks-2026"
# Function to log messages
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}
# Function to send Discord webhook
send_discord() {
local status=$1
local message=$2
local color=$3
if [ -z "$DISCORD_WEBHOOK" ]; then
log "WARNING: DISCORD_WEBHOOK_URL not set, skipping Discord notification"
return
fi
local title="Deployment Successful"
[ "$status" = "failure" ] && title="Deployment Failed"
local commit=$(git rev-parse --short HEAD 2>/dev/null || echo "N/A")
local payload=$(cat <<EOF
{
"content": "$ **$title** - $GITHUB_REPO",
"embeds": [
{
"title": "$title",
"color": $color,
"fields": [
{
"name": "Repository",
"value": "$GITHUB_REPO",
"inline": true
},
{
"name": "Branch",
"value": "main",
"inline": true
},
{
"name": "Commit",
"value": "\`$commit\`",
"inline": true
},
{
"name": "Server",
"value": "$(hostname)",
"inline": true
},
{
"name": "Status",
"value": "$message",
"inline": false
},
{
"name": "Timestamp",
"value": "$(date '+%Y-%m-%d %H:%M:%S %Z')",
"inline": false
}
]
}
]
}
EOF
)
curl -X POST "$DISCORD_WEBHOOK" \
-H "Content-Type: application/json" \
-d "$payload" \
2>/dev/null || log "WARNING: Failed to send Discord notification"
}
# Check if deployment already in progress
if [ -f "${REPO_PATH}/.deploy.lock" ]; then
log "Deployment already in progress, skipping"
exit 0
fi
# Create lock file
touch "${REPO_PATH}/.deploy.lock"
trap "rm -f ${REPO_PATH}/.deploy.lock" EXIT
# Navigate to the repository
cd "$REPO_PATH" || exit 1
log "Checking git status for $REPO_PATH"
# Fetch latest changes from remote
git fetch origin main || {
log "ERROR: Failed to fetch from origin"
send_discord "failure" "Failed to fetch from origin" "15158332"
exit 1
}
# Check if local main is behind remote main
LOCAL=$(git rev-parse main)
REMOTE=$(git rev-parse origin/main)
if [ "$LOCAL" != "$REMOTE" ]; then
log "New changes detected on main branch"
# Pull the latest changes
log "Pulling latest changes..."
git pull origin main || {
log "ERROR: Failed to pull changes"
send_discord "failure" "Failed to pull changes" "15158332"
exit 1
}
# Stop and remove containers
log "Running docker compose down..."
docker compose down || {
log "ERROR: docker compose down failed"
send_discord "failure" "docker compose down failed" "15158332"
exit 1
}
# Build and start containers
log "Running docker compose up -d --build..."
docker compose up -d --build || {
log "ERROR: docker compose up -d --build failed"
send_discord "failure" "docker compose up -d --build failed" "15158332"
exit 1
}
log "Deployment completed successfully"
send_discord "success" "Docker containers rebuilt and running" "3066993"
else
log "No new changes on main branch"
fi
exit 0