-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·724 lines (578 loc) · 19.3 KB
/
release.sh
File metadata and controls
executable file
·724 lines (578 loc) · 19.3 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#!/bin/bash
set -euo pipefail
# =============================================================================
# Windshift Release Script
# =============================================================================
# Configuration
GHCR_REGISTRY="ghcr.io/windshiftapp/windshift"
GHCR_LOGBOOK_REGISTRY="ghcr.io/windshiftapp/logbook"
GITHUB_REPO="Windshiftapp/windshift"
DOCKER_PLATFORMS="linux/amd64,linux/arm64"
# Build configurations: GOOS/GOARCH
PLATFORMS=(
"linux/amd64"
"linux/arm64"
"windows/amd64"
"darwin/amd64"
"darwin/arm64"
)
# State variables
VERSION=""
NOTES_FILE=""
DRY_RUN=false
SKIP_FRONTEND=false
CONFIRM=true
TAG_CREATED=false
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# =============================================================================
# Utility Functions
# =============================================================================
log_info() { echo -e "${BLUE}[INFO]${NC} $*"; }
log_success() { echo -e "${GREEN}[OK]${NC} $*"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
log_error() { echo -e "${RED}[ERROR]${NC} $*"; }
log_step() { echo -e "${CYAN}[$1]${NC} $2"; }
die() { log_error "$*"; exit 1; }
dry_run_or_exec() {
if [ "$DRY_RUN" = true ]; then
log_info "[DRY-RUN] Would execute: $*"
return 0
else
"$@"
fi
}
# =============================================================================
# Version Management
# =============================================================================
get_git_tag() {
git describe --tags --exact-match HEAD 2>/dev/null || echo ""
}
get_latest_tag() {
git describe --tags --abbrev=0 2>/dev/null || echo ""
}
generate_next_version() {
local latest=$(get_latest_tag)
if [ -z "$latest" ]; then
echo "v0.1.0"
else
local version="${latest#v}"
local major minor patch
IFS='.' read -r major minor patch <<< "$version"
# Handle pre-release suffixes (e.g., v0.1.0-dev)
patch="${patch%%-*}"
patch=$((patch + 1))
echo "v${major}.${minor}.${patch}"
fi
}
validate_version() {
local version="$1"
if [[ ! "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?$ ]]; then
die "Invalid version format: $version (expected vX.Y.Z or vX.Y.Z-suffix)"
fi
}
determine_version() {
if [ -n "$VERSION" ]; then
validate_version "$VERSION"
log_info "Using specified version: $VERSION"
else
local current_tag=$(get_git_tag)
if [ -n "$current_tag" ]; then
VERSION="$current_tag"
log_info "Using existing tag on HEAD: $VERSION"
else
VERSION=$(generate_next_version)
log_info "Auto-generated version: $VERSION (bumping from $(get_latest_tag))"
fi
fi
}
tag_exists() {
git rev-parse "$1" &>/dev/null
}
create_git_tag() {
local tag="$1"
if tag_exists "$tag"; then
log_warn "Tag $tag already exists"
return 0
fi
if [ "$DRY_RUN" = true ]; then
log_info "[DRY-RUN] Would create git tag: $tag"
log_info "[DRY-RUN] Would push tag to remote"
return 0
fi
git tag -a "$tag" -m "Release $tag"
log_success "Created git tag: $tag"
git push origin "$tag"
log_success "Pushed tag to remote"
TAG_CREATED=true
}
# =============================================================================
# Pre-flight Checks
# =============================================================================
check_dependencies() {
log_info "Checking dependencies..."
local missing=()
command -v go >/dev/null 2>&1 || missing+=("go")
command -v npm >/dev/null 2>&1 || missing+=("npm")
if [ ${#missing[@]} -gt 0 ]; then
die "Missing required tools: ${missing[*]}"
fi
log_success "Dependencies OK"
}
check_docker() {
if ! command -v docker >/dev/null 2>&1; then
die "Docker not found - required for Docker builds"
fi
if ! docker buildx version &>/dev/null; then
die "Docker Buildx not available - required for multi-arch builds"
fi
}
check_gh_cli() {
if ! command -v gh >/dev/null 2>&1; then
die "GitHub CLI (gh) not found - required for GitHub releases"
fi
if ! gh auth status &>/dev/null; then
die "GitHub CLI not authenticated - run 'gh auth login' first"
fi
}
check_git_state() {
if ! git diff-index --quiet HEAD --; then
die "Uncommitted changes detected. Commit or stash before releasing."
fi
local branch=$(git branch --show-current)
if [ "$branch" != "main" ] && [ "$branch" != "master" ]; then
log_warn "Not on main/master branch (currently on: $branch)"
fi
}
# =============================================================================
# Build Functions
# =============================================================================
build_frontend() {
if [ "$SKIP_FRONTEND" = true ]; then
log_info "Skipping frontend build"
return 0
fi
log_step "1/8" "Building frontend..."
if [ "$DRY_RUN" = true ]; then
log_info "[DRY-RUN] Would run: cd frontend && npm install && npm run build"
return 0
fi
(cd frontend && npm install --silent && npm run build)
if [ ! -d "frontend/dist" ]; then
die "Frontend build failed: dist/ not created"
fi
log_success "Frontend built"
}
build_binary() {
local goos="$1"
local goarch="$2"
local output_path="dist/binaries/windshift-${goos}-${goarch}"
[ "$goos" = "windows" ] && output_path="${output_path}.exe"
log_info " Building for ${goos}/${goarch}..."
if [ "$DRY_RUN" = true ]; then
log_info " [DRY-RUN] Would build: $output_path"
return 0
fi
export CGO_ENABLED=0 GOOS="$goos" GOARCH="$goarch"
if go build -ldflags "-s -w" -o "$output_path" .; then
local size=$(ls -lh "$output_path" | awk '{print $5}')
log_success " Built: $output_path ($size)"
else
log_error " Failed to build for ${goos}/${goarch}"
return 1
fi
}
build_binaries() {
log_step "2/8" "Building server binaries..."
dry_run_or_exec mkdir -p dist/binaries
for platform in "${PLATFORMS[@]}"; do
IFS="/" read -r goos goarch <<< "$platform"
build_binary "$goos" "$goarch" || true
done
log_success "Server binary builds complete"
}
build_ws_binary() {
local goos="$1"
local goarch="$2"
local output_path="dist/binaries/ws-${goos}-${goarch}"
[ "$goos" = "windows" ] && output_path="${output_path}.exe"
log_info " Building ws for ${goos}/${goarch}..."
if [ "$DRY_RUN" = true ]; then
log_info " [DRY-RUN] Would build: $output_path"
return 0
fi
export CGO_ENABLED=0 GOOS="$goos" GOARCH="$goarch"
local version_clean="${VERSION#v}"
local git_commit=$(git rev-parse --short HEAD)
local build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
if go build -ldflags "-s -w -X main.version=${version_clean} -X main.commit=${git_commit} -X main.date=${build_date}" -o "$output_path" ./cmd/ws; then
local size=$(ls -lh "$output_path" | awk '{print $5}')
log_success " Built: $output_path ($size)"
else
log_error " Failed to build ws for ${goos}/${goarch}"
return 1
fi
}
build_ws_binaries() {
log_step "3/8" "Building ws CLI binaries..."
dry_run_or_exec mkdir -p dist/binaries
for platform in "${PLATFORMS[@]}"; do
IFS="/" read -r goos goarch <<< "$platform"
build_ws_binary "$goos" "$goarch" || true
done
log_success "ws CLI binary builds complete"
}
create_release_packages() {
log_step "4/8" "Creating server release packages..."
dry_run_or_exec mkdir -p dist/releases
if [ "$DRY_RUN" = true ]; then
log_info "[DRY-RUN] Would create release packages for all built binaries"
return 0
fi
for binary in dist/binaries/windshift-*; do
[ -f "$binary" ] || continue
local basename=$(basename "$binary")
local platform="${basename#windshift-}"
platform="${platform%.exe}"
local package_name="windshift-${VERSION}-${platform}"
local package_dir="dist/releases/${package_name}"
mkdir -p "$package_dir"
# Copy server binary
if [[ "$platform" == *windows* ]]; then
cp "$binary" "$package_dir/windshift.exe"
else
cp "$binary" "$package_dir/windshift"
fi
# Copy documentation
[ -f "README.md" ] && cp README.md "$package_dir/" || true
# Create sample config
cat > "$package_dir/config.example.env" << 'CONFIGEOF'
# Windshift Configuration
PORT=8080
# Database - Choose one:
# SQLite (default)
DATABASE_PATH=windshift.db
# PostgreSQL (uncomment to use)
# POSTGRES_CONNECTION_STRING=postgresql://user:password@localhost:5432/windshift?sslmode=disable
CONFIGEOF
# Create archive
if [[ "$platform" == *windows* ]]; then
(cd dist/releases && zip -q -r "${package_name}.zip" "${package_name}")
log_success "Created ${package_name}.zip"
else
(cd dist/releases && tar -czf "${package_name}.tar.gz" "${package_name}")
log_success "Created ${package_name}.tar.gz"
fi
rm -rf "$package_dir"
done
}
create_ws_release_packages() {
log_step "5/8" "Creating ws CLI release packages..."
dry_run_or_exec mkdir -p dist/releases
if [ "$DRY_RUN" = true ]; then
log_info "[DRY-RUN] Would create ws release packages for all built ws binaries"
log_info "[DRY-RUN] Would generate SHA256SUMS.txt for all release archives"
return 0
fi
for binary in dist/binaries/ws-*; do
[ -f "$binary" ] || continue
local basename=$(basename "$binary")
local platform="${basename#ws-}"
platform="${platform%.exe}"
local package_name="ws-${VERSION}-${platform}"
local package_dir="dist/releases/${package_name}"
mkdir -p "$package_dir"
# Copy ws binary
if [[ "$platform" == *windows* ]]; then
cp "$binary" "$package_dir/ws.exe"
else
cp "$binary" "$package_dir/ws"
fi
# Create archive
if [[ "$platform" == *windows* ]]; then
(cd dist/releases && zip -q -r "${package_name}.zip" "${package_name}")
log_success "Created ${package_name}.zip"
else
(cd dist/releases && tar -czf "${package_name}.tar.gz" "${package_name}")
log_success "Created ${package_name}.tar.gz"
fi
rm -rf "$package_dir"
done
# Generate checksums for all release archives (windshift + ws)
if ls dist/releases/*.tar.gz dist/releases/*.zip 2>/dev/null | head -1 >/dev/null; then
(cd dist/releases && sha256sum *.tar.gz *.zip 2>/dev/null > SHA256SUMS.txt || true)
log_success "Generated SHA256SUMS.txt"
fi
}
ensure_buildx() {
if ! docker buildx inspect windshift-builder &>/dev/null; then
log_info "Creating buildx builder..."
dry_run_or_exec docker buildx create --name windshift-builder --use
else
dry_run_or_exec docker buildx use windshift-builder
fi
}
build_docker() {
log_step "6/8" "Building Docker images..."
check_docker
ensure_buildx
local tags="-t ${GHCR_REGISTRY}:${VERSION}"
# Only tag as latest for official releases (not dev/test versions)
if [[ ! "$VERSION" =~ -dev|-test|-rc ]]; then
tags="$tags -t ${GHCR_REGISTRY}:latest"
fi
log_info "Platforms: ${DOCKER_PLATFORMS}"
log_info "Tags: ${GHCR_REGISTRY}:${VERSION}"
if [ "$DRY_RUN" = true ]; then
log_info "[DRY-RUN] Would build and push Docker images"
return 0
fi
docker buildx build \
--platform "$DOCKER_PLATFORMS" \
$tags \
--push \
.
log_success "Docker images pushed to ${GHCR_REGISTRY}"
}
build_logbook_docker() {
log_step "6b/8" "Building Logbook Docker images..."
check_docker
ensure_buildx
local tags="-t ${GHCR_LOGBOOK_REGISTRY}:${VERSION}"
# Only tag as latest for official releases (not dev/test versions)
if [[ ! "$VERSION" =~ -dev|-test|-rc ]]; then
tags="$tags -t ${GHCR_LOGBOOK_REGISTRY}:latest"
fi
log_info "Platforms: ${DOCKER_PLATFORMS}"
log_info "Tags: ${GHCR_LOGBOOK_REGISTRY}:${VERSION}"
if [ "$DRY_RUN" = true ]; then
log_info "[DRY-RUN] Would build and push Logbook Docker images"
return 0
fi
docker buildx build \
--platform "$DOCKER_PLATFORMS" \
$tags \
-f Dockerfile.logbook \
--push \
.
log_success "Logbook Docker images pushed to ${GHCR_LOGBOOK_REGISTRY}"
}
create_github_release() {
log_step "7/8" "Creating GitHub release..."
check_gh_cli
# Create git tag if needed
local current_tag=$(get_git_tag)
if [ -z "$current_tag" ]; then
create_git_tag "$VERSION"
fi
if [ "$DRY_RUN" = true ]; then
log_info "[DRY-RUN] Would create GitHub release ${VERSION}"
log_info "[DRY-RUN] Would upload assets from dist/releases/"
return 0
fi
# Collect assets
local assets=()
for file in dist/releases/*.tar.gz dist/releases/*.zip dist/releases/SHA256SUMS.txt; do
[ -f "$file" ] && assets+=("$file")
done
if [ ${#assets[@]} -eq 0 ]; then
log_warn "No release assets found"
fi
# Create release with notes file
gh release create "$VERSION" \
--repo "$GITHUB_REPO" \
--title "Windshift $VERSION" \
--notes-file "$NOTES_FILE" \
"${assets[@]}"
log_success "GitHub release created: https://github.com/${GITHUB_REPO}/releases/tag/${VERSION}"
}
# =============================================================================
# Commands
# =============================================================================
cmd_build() {
check_dependencies
determine_version
rm -rf dist/
build_frontend
build_binaries
build_ws_binaries
create_release_packages
create_ws_release_packages
echo ""
log_success "Build complete! Artifacts in dist/"
echo ""
echo "Release packages:"
ls -1 dist/releases/*.tar.gz dist/releases/*.zip 2>/dev/null | sed 's/^/ /' || echo " (none)"
}
cmd_push() {
check_dependencies
check_docker
determine_version
if [ "$CONFIRM" = true ] && [ "$DRY_RUN" = false ]; then
echo ""
echo "Windshift Docker Push: $VERSION"
echo "=============================="
echo "This will:"
echo " - Build frontend"
echo " - Build and push Docker images to ${GHCR_REGISTRY}"
echo " - Build and push Logbook Docker images to ${GHCR_LOGBOOK_REGISTRY}"
echo ""
echo "Note: This does NOT create a GitHub release."
echo ""
read -p "Continue? [y/N] " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] || exit 1
fi
rm -rf dist/
build_frontend
build_docker
build_logbook_docker
echo ""
log_success "Push complete!"
echo ""
echo "Docker images:"
echo " Windshift: ${GHCR_REGISTRY}:${VERSION}"
echo " Logbook: ${GHCR_LOGBOOK_REGISTRY}:${VERSION}"
}
cmd_release() {
# Validate release notes file
if [ -z "$NOTES_FILE" ]; then
die "Release notes file required. Use: ./release.sh release -v VERSION -n NOTES_FILE"
fi
if [ ! -f "$NOTES_FILE" ]; then
die "Release notes file not found: $NOTES_FILE"
fi
check_dependencies
check_docker
check_gh_cli
check_git_state
determine_version
if [ "$CONFIRM" = true ] && [ "$DRY_RUN" = false ]; then
echo ""
echo "Windshift Release: $VERSION"
echo "=========================="
echo "This will:"
echo " - Build frontend"
echo " - Build server binaries for multiple platforms"
echo " - Build ws CLI binaries for multiple platforms"
echo " - Create release packages with checksums"
echo " - Build and push Docker images (Windshift + Logbook)"
echo " - Create git tag and push"
echo " - Create GitHub release with assets"
echo ""
echo "Release notes: $NOTES_FILE"
echo ""
read -p "Continue? [y/N] " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] || exit 1
fi
rm -rf dist/
build_frontend
build_binaries
build_ws_binaries
create_release_packages
create_ws_release_packages
build_docker
build_logbook_docker
create_github_release
echo ""
log_success "Release $VERSION complete!"
echo ""
echo "GitHub: https://github.com/${GITHUB_REPO}/releases/tag/${VERSION}"
echo "Docker: docker pull ${GHCR_REGISTRY}:${VERSION}"
echo "Logbook: docker pull ${GHCR_LOGBOOK_REGISTRY}:${VERSION}"
}
# =============================================================================
# Help
# =============================================================================
show_help() {
cat << 'EOF'
Windshift Release Script
Usage: ./release.sh <command> [options]
Commands:
build Build binaries and packages locally (no publish)
push Build and push Docker images only (no GitHub release)
release Full release: binaries + Docker + GitHub release
Options:
-v, --version VERSION Specify version (e.g., v1.2.0)
-n, --notes FILE Release notes markdown file (required for 'release')
--dry-run Preview without executing
--skip-frontend Skip frontend build (use existing dist/)
-y, --yes Skip confirmation prompts
-h, --help Show this help
Examples:
# Quick Docker push for testing
./release.sh push -v v0.1.8-dev
# Full official release with release notes
./release.sh release -v v1.0.0 -n releases/v1.0.0.md
# Preview what would happen
./release.sh release -v v1.0.0 -n releases/v1.0.0.md --dry-run
# Just build binaries locally
./release.sh build
Release Notes:
For official releases, create a markdown file with your release notes:
releases/v1.0.0.md:
## What's New
- Feature X
## Bug Fixes
- Fixed issue #123
EOF
}
# =============================================================================
# Argument Parsing
# =============================================================================
parse_args() {
COMMAND="${1:-help}"
shift || true
while [[ $# -gt 0 ]]; do
case $1 in
-v|--version)
VERSION="$2"
shift 2
;;
-n|--notes)
NOTES_FILE="$2"
shift 2
;;
--dry-run)
DRY_RUN=true
shift
;;
--skip-frontend)
SKIP_FRONTEND=true
shift
;;
-y|--yes)
CONFIRM=false
shift
;;
-h|--help)
show_help
exit 0
;;
*)
die "Unknown option: $1"
;;
esac
done
}
main() {
parse_args "$@"
# Check we're in the right directory
if [ ! -f "main.go" ]; then
die "This script must be run from the project root directory"
fi
case "$COMMAND" in
build) cmd_build ;;
push) cmd_push ;;
release) cmd_release ;;
help|-h|--help) show_help ;;
*) die "Unknown command: $COMMAND (use --help for usage)" ;;
esac
}
main "$@"