-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·275 lines (219 loc) · 7.4 KB
/
deploy.sh
File metadata and controls
executable file
·275 lines (219 loc) · 7.4 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Container runtime detection (Docker with Podman fallback)
detect_container_runtime() {
local docker_cmd
local podman_cmd
docker_cmd=$(which docker 2>/dev/null || echo "")
podman_cmd=$(which podman 2>/dev/null || echo "")
if [ -n "$docker_cmd" ]; then
CONTAINER_CMD="docker"
info "Using Docker for container operations"
elif [ -n "$podman_cmd" ]; then
CONTAINER_CMD="podman"
info "Using Podman for container operations"
else
error "Neither Docker nor Podman is installed. Please install one of them to verify images."
fi
}
# Helper functions
info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
success() {
echo -e "${GREEN}✅ $1${NC}"
}
warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
error() {
echo -e "${RED}❌ $1${NC}"
exit 1
}
# Check if git repo is clean (only tracked files)
check_git_status() {
# Check for modified, staged, or deleted tracked files
if [ -n "$(git status --porcelain | grep -E '^[MADR]')" ]; then
error "Git working directory has uncommitted changes to tracked files. Please commit or stash your changes."
fi
# Show untracked files as info (but don't block)
untracked=$(git status --porcelain | grep '^??' || true)
if [ -n "$untracked" ]; then
warning "Untracked files present (will be ignored):"
echo "$untracked" | sed 's/^?? / - /'
echo ""
fi
}
# Check if we're on main branch
check_main_branch() {
current_branch=$(git branch --show-current)
if [ "$current_branch" != "main" ]; then
error "You must be on the main branch to deploy. Current branch: $current_branch"
fi
}
# Get the latest git tag
get_latest_tag() {
local latest_tag
latest_tag=$(git describe --tags --abbrev=0 2>/dev/null)
if [ -z "$latest_tag" ]; then
error "No git tags found. Create a release first using './release.sh patch|minor|major'"
fi
echo "$latest_tag"
}
# Verify Docker images exist
verify_docker_images() {
local version=$1
info "Verifying Docker images exist for version $version..."
for image in backend collect frontend proxy; do
local image_url="ghcr.io/nethserver/my/$image:$version"
info "Checking: $image_url"
if $CONTAINER_CMD manifest inspect "$image_url" >/dev/null 2>&1; then
success "$image image found"
else
error "Image not found: $image_url. Please ensure the release workflow has completed successfully."
fi
done
success "All Docker images verified successfully"
}
# Update render.yaml with new image tags
update_render_yaml() {
local version=$1
info "Updating render.yaml with version $version..."
# Create backup
cp render.yaml render.yaml.backup
# Update backend image tag
sed -i.tmp 's|ghcr\.io/nethserver/my/backend:v[0-9]*\.[0-9]*\.[0-9]*|ghcr.io/nethserver/my/backend:'"$version"'|g' render.yaml
# Update collect image tag
sed -i.tmp 's|ghcr\.io/nethserver/my/collect:v[0-9]*\.[0-9]*\.[0-9]*|ghcr.io/nethserver/my/collect:'"$version"'|g' render.yaml
# Update frontend image tag
sed -i.tmp 's|ghcr\.io/nethserver/my/frontend:v[0-9]*\.[0-9]*\.[0-9]*|ghcr.io/nethserver/my/frontend:'"$version"'|g' render.yaml
# Update proxy image tag
sed -i.tmp 's|ghcr\.io/nethserver/my/proxy:v[0-9]*\.[0-9]*\.[0-9]*|ghcr.io/nethserver/my/proxy:'"$version"'|g' render.yaml
# Remove sed backup file
rm -f render.yaml.tmp
# Show changes
info "Updated render.yaml with the following image references:"
grep "url: ghcr.io/nethserver/my/" render.yaml | sed 's/^/ /'
success "render.yaml updated successfully"
}
# Commit and push changes
commit_and_push() {
local version=$1
local git_user_name
local git_user_email
# Get git user info
git_user_name=$(git config user.name)
git_user_email=$(git config user.email)
if [ -z "$git_user_name" ] || [ -z "$git_user_email" ]; then
error "Git user name and email must be configured. Run: git config --global user.name 'Your Name' && git config --global user.email 'your@email.com'"
fi
info "Committing changes as $git_user_name <$git_user_email>..."
# Check if there are changes to commit
if git diff --quiet render.yaml; then
warning "No changes to commit in render.yaml"
return
fi
# Add and commit changes
git add render.yaml
git commit -m "deploy: update render.yaml to use $version images"
# Push to remote
info "Pushing to remote..."
git push origin main
success "Changes committed and pushed to main branch"
}
# Clean up backup file
cleanup() {
if [ -f "render.yaml.backup" ]; then
rm -f render.yaml.backup
info "Cleaned up backup file"
fi
}
# Show usage
usage() {
echo "Usage: $0 [--skip-verify]"
echo ""
echo "Deploy the latest git tag to production by updating render.yaml"
echo ""
echo "Options:"
echo " --skip-verify Skip Docker image verification (faster but less safe)"
echo ""
echo "This script will:"
echo " 1. Get the latest git tag"
echo " 2. Show the tag and ask for confirmation"
echo " 3. Verify Docker images exist (unless --skip-verify)"
echo " 4. Update render.yaml with new image tags"
echo " 5. Commit and push changes"
echo " 6. Render will automatically deploy the updated services"
exit 1
}
# Main function
main() {
local skip_verify=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--skip-verify)
skip_verify=true
shift
;;
-h|--help)
usage
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
info "Starting deployment process..."
# Detect container runtime
detect_container_runtime
# Pre-flight checks
check_git_status
check_main_branch
# Get latest tag
latest_tag=$(get_latest_tag)
info "Latest git tag: $latest_tag"
# Confirm with user
echo ""
read -p "Do you want to deploy $latest_tag to production? [y/N] " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
warning "Deployment cancelled"
exit 0
fi
# Set trap for cleanup
trap cleanup EXIT
# Verify images exist (unless skipped)
if [ "$skip_verify" = false ]; then
verify_docker_images "$latest_tag"
else
warning "Skipping Docker image verification"
fi
# Update render.yaml
update_render_yaml "$latest_tag"
# Commit and push
commit_and_push "$latest_tag"
success "Deployment initiated successfully!"
success "render.yaml updated with $latest_tag images and pushed to main"
success "Render will now automatically deploy the updated services"
# Show useful links
echo ""
info "Monitor deployment progress at:"
info "- Render Dashboard: https://dashboard.render.com"
info "- Production URL: https://my.nethesis.it"
echo ""
info "Deployment Summary:"
info "- Backend: ghcr.io/nethserver/my/backend:$latest_tag"
info "- Collect: ghcr.io/nethserver/my/collect:$latest_tag"
info "- Frontend: ghcr.io/nethserver/my/frontend:$latest_tag"
info "- Proxy: ghcr.io/nethserver/my/proxy:$latest_tag"
}
# Run main function
main "$@"