This repository was archived by the owner on Jun 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·825 lines (702 loc) · 30.2 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·825 lines (702 loc) · 30.2 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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "click>=8.1.7",
# "semver>=3.0.2",
# "pyyaml>=6.0.2",
# "rich>=13.7.0",
# ]
# ///
"""
Docker Compose-based multi-platform build and push script with semantic versioning.
Now uses Docker Compose for all build operations instead of direct Docker commands.
"""
import os
import subprocess
import sys
from datetime import datetime
from pathlib import Path
import click
import semver
import yaml
from rich.console import Console
from rich.table import Table
console = Console()
class BuildError(Exception):
"""Custom exception for build errors"""
pass
class DockerComposeBuilder:
def __init__(
self,
image_name: str | None = None,
platforms: str = "linux/amd64,linux/arm64",
dockerfile: str = "Dockerfile",
registry: str = "ghcr.io",
builder_name: str = "multiarch-builder",
skip_push: bool = False,
build_cache: bool = True,
require_login: bool = True,
compose_file: str = "docker-compose.build.yml",
dev_mode: bool = False,
):
self.image_name = image_name or self._get_default_image_name()
self.platforms = platforms
self.dockerfile = dockerfile
self.registry = registry
self.builder_name = builder_name
self.skip_push = skip_push
self.build_cache = build_cache
self.require_login = require_login
self.compose_file = compose_file
self.dev_mode = dev_mode
self.github_user = os.environ.get("GITHUB_USER") or self._get_git_user()
self.github_token = os.environ.get("GITHUB_TOKEN")
self.env_vars = {}
def _get_default_image_name(self) -> str:
"""Get image name from git repository or current directory"""
# First, check if we're in GitHub Actions
github_repo = os.environ.get("GITHUB_REPOSITORY")
if github_repo:
# In GitHub Actions, use the repository name directly
return github_repo.lower()
try:
# Get git remote URL
result = self._run_command(
["git", "remote", "get-url", "origin"], check=False, verbose=False
)
if result.returncode == 0:
remote_url = result.stdout.strip()
# Parse GitHub URL
if "github.com" in remote_url:
# Handle both HTTPS and SSH URLs
if remote_url.startswith("https://"):
parts = (
remote_url.replace("https://github.com/", "")
.replace(".git", "")
.split("/")
)
elif remote_url.startswith("git@"):
parts = (
remote_url.replace("git@github.com:", "").replace(".git", "").split("/")
)
else:
parts = []
if len(parts) >= 2:
return f"{parts[0]}/{parts[1]}"
except Exception:
pass
# Fall back to current directory
current_dir = Path.cwd().name
return f"sensor/{current_dir}"
def _get_git_user(self) -> str:
"""Get git username"""
try:
result = subprocess.run(
["git", "config", "user.name"], capture_output=True, text=True, check=False
)
if result.returncode == 0:
return result.stdout.strip()
except Exception:
pass
return "GITHUB_USER_NOT_SET"
def _run_command(
self, cmd: list[str], check: bool = True, verbose: bool = True
) -> subprocess.CompletedProcess:
"""Run a command and return the result"""
if verbose:
console.print(f"[dim]Running: {' '.join(cmd)}[/dim]")
return subprocess.run(cmd, capture_output=True, text=True, check=check)
def _run_compose_command(
self,
args: list[str],
check: bool = True,
verbose: bool = True,
capture_output: bool = True,
env: dict = None,
) -> subprocess.CompletedProcess:
"""Run a docker compose command with proper environment"""
# Build the compose command
cmd = ["docker", "compose"]
if self.compose_file:
cmd.extend(["-f", self.compose_file])
cmd.extend(args)
if verbose:
console.print(f"[dim]Running: {' '.join(cmd)}[/dim]")
# Merge environment variables
run_env = os.environ.copy()
if env:
run_env.update(env)
run_env.update(self.env_vars)
return subprocess.run(
cmd, capture_output=capture_output, text=True, check=check, env=run_env
)
def validate_requirements(self):
"""Validate all requirements are met"""
console.print("[blue]Validating requirements...[/blue]")
# Check for required commands
for cmd, msg in [
("docker", "Docker is required but not installed"),
("git", "Git is required but not installed"),
]:
if subprocess.run(["which", cmd], capture_output=True).returncode != 0:
raise BuildError(msg)
# Check Docker Compose support
result = self._run_command(["docker", "compose", "version"], check=False, verbose=False)
if result.returncode != 0:
# Try docker-compose (hyphenated)
result = self._run_command(["docker-compose", "version"], check=False, verbose=False)
if result.returncode != 0:
raise BuildError(
"Docker Compose is required but not installed.\n"
"Please install Docker Desktop or Docker Compose plugin."
)
# Check compose file exists (create if it doesn't)
if not Path(self.compose_file).exists():
console.print(f"[yellow]Creating {self.compose_file}...[/yellow]")
self._create_compose_build_file()
# Check dockerfile exists
if not Path(self.dockerfile).exists():
raise BuildError(f"Dockerfile not found at {self.dockerfile}")
# Check docker daemon is running
result = self._run_command(["docker", "info"], check=False, verbose=False)
if result.returncode != 0:
raise BuildError("Docker daemon is not running")
# Check buildx support
result = self._run_command(["docker", "buildx", "version"], check=False, verbose=False)
if result.returncode != 0:
console.print(
"[yellow]![/yellow] Docker buildx support not available, "
"multi-platform builds may be limited"
)
console.print("[green]✓[/green] All requirements validated")
def _create_compose_build_file(self):
"""Create a docker-compose.build.yml file if it doesn't exist"""
# For Docker Compose, platforms should be specified as separate items
# We'll handle this dynamically based on the platforms string
platform_list = self.platforms.split(",")
build_config = {
"context": ".",
"dockerfile": "${DOCKERFILE:-Dockerfile}",
"platforms": platform_list,
"labels": {
"org.opencontainers.image.title": "Sensor Log Generator",
"org.opencontainers.image.description": "High-performance sensor data simulator",
"org.opencontainers.image.version": "${VERSION:-dev}",
"org.opencontainers.image.created": "${BUILD_DATE}",
"org.opencontainers.image.revision": "${GIT_COMMIT}",
},
"args": {
"BUILD_DATE": "${BUILD_DATE}",
"VERSION": "${VERSION:-dev}",
"GIT_COMMIT": "${GIT_COMMIT}",
},
}
# Only add cache config if we're pushing (avoid cache push errors for local builds)
if not self.skip_push and self.build_cache:
build_config["cache_from"] = [
"type=registry,ref=${CACHE_FROM:-ghcr.io/bacalhau-project/sensor-log-generator:buildcache}"
]
build_config["cache_to"] = [
"type=registry,ref=${CACHE_TO:-ghcr.io/bacalhau-project/sensor-log-generator:buildcache},mode=max"
]
compose_content = {
"services": {
"sensor-simulator": {
"image": "${IMAGE_TAG:-ghcr.io/bacalhau-project/sensor-log-generator:latest}",
"build": build_config,
}
}
}
with open(self.compose_file, "w") as f:
yaml.dump(compose_content, f, default_flow_style=False, sort_keys=False)
console.print(f"[green]✓[/green] Created {self.compose_file}")
def check_docker_login(self):
"""Check if user is logged into Docker registry"""
if not self.require_login:
console.print("[yellow]Skipping Docker login check[/yellow]")
return
# Check if we're in CI
if os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS"):
console.print(
"[blue]Running in CI environment, assuming authentication is handled[/blue]"
)
# Warn if trying to push to a repository that might not be accessible
if "bacalhau-project" in self.image_name and not os.environ.get(
"GITHUB_REPOSITORY", ""
).startswith("bacalhau-project"):
console.print(
f"[bold yellow]⚠️ Warning: Attempting to push to {self.registry}/{self.image_name}[/bold yellow]"
)
console.print(
"[yellow] You may not have write access to this repository.[/yellow]"
)
console.print(
"[yellow] Consider using --image-name with your own repository.[/yellow]"
)
return
console.print("[blue]Checking Docker registry login...[/blue]")
# Check if we're in CI environment
if os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS"):
console.print(
"[dim]Running in CI environment, assuming authentication is handled[/dim]"
)
return
if not self.github_token:
console.print(
"[yellow]![/yellow] GITHUB_TOKEN not set, attempting to use existing Docker credentials"
)
# Check if already logged in
result = subprocess.run(
["docker", "pull", f"{self.registry}/hello-world"],
capture_output=True,
text=True,
check=False,
)
if "pull access denied" in result.stderr or "unauthorized" in result.stderr.lower():
raise BuildError(
"Not logged in to GitHub Container Registry.\n"
"Please set GITHUB_TOKEN environment variable or run:\n"
f" echo $GITHUB_TOKEN | docker login {self.registry} -u USERNAME --password-stdin"
)
return
if not self.github_user:
raise BuildError("GITHUB_USER is not set")
# Login to registry
result = subprocess.run(
["docker", "login", self.registry, "--username", self.github_user, "--password-stdin"],
input=self.github_token,
capture_output=True,
text=True,
check=False,
)
if result.returncode != 0:
raise BuildError(f"Failed to log in to Docker registry: {result.stderr}")
console.print("[green]✓[/green] Successfully logged in to Docker registry")
def setup_buildx_builder(self):
"""Setup buildx builder for multi-platform builds"""
console.print("[blue]Setting up Docker Buildx for multi-platform builds...[/blue]")
# Check if buildx is available
result = self._run_command(["docker", "buildx", "version"], check=False, verbose=False)
if result.returncode != 0:
console.print(
"[yellow]![/yellow] Docker Buildx not available, "
"using standard Docker Compose build"
)
return False
# Check if builder exists
result = self._run_command(
["docker", "buildx", "inspect", self.builder_name], check=False, verbose=False
)
if result.returncode == 0:
# Check if builder is running
result = self._run_command(
["docker", "buildx", "inspect", "--bootstrap", self.builder_name],
check=False,
verbose=False,
)
if "Status: running" in result.stdout:
console.print(f"[green]✓[/green] Using existing builder '{self.builder_name}'")
self._run_command(["docker", "buildx", "use", self.builder_name], verbose=False)
return True
else:
console.print("[yellow]![/yellow] Removing non-functional builder")
self._run_command(
["docker", "buildx", "rm", self.builder_name], check=False, verbose=False
)
# Create new builder
console.print(f"Creating new builder '{self.builder_name}'...")
self._run_command(
[
"docker",
"buildx",
"create",
"--name",
self.builder_name,
"--driver",
"docker-container",
"--bootstrap",
],
verbose=False,
)
self._run_command(["docker", "buildx", "use", self.builder_name], verbose=False)
console.print("[green]✓[/green] Builder created and ready")
return True
def get_current_version(self) -> semver.Version | None:
"""Get the current version from git tags"""
try:
result = self._run_command(["git", "tag", "--list", "v*"], check=False)
if result.returncode == 0 and result.stdout:
tags = result.stdout.strip().split("\n")
# Filter valid semver tags
versions = []
for tag in tags:
tag = tag.strip()
if tag.startswith("v"):
tag = tag[1:]
try:
versions.append(semver.Version.parse(tag))
except Exception:
continue
if versions:
return max(versions)
except Exception:
pass
return None
def bump_version(self, current: semver.Version | None, bump_type: str) -> semver.Version:
"""Bump the version based on type"""
if current is None:
# Start with 1.0.0 if no version exists
return semver.Version(1, 0, 0)
if bump_type == "major":
return current.bump_major()
elif bump_type == "minor":
return current.bump_minor()
elif bump_type == "patch":
return current.bump_patch()
else:
raise BuildError(f"Invalid bump type: {bump_type}")
def parse_version(self, version_str: str) -> semver.Version:
"""Parse and validate a version string"""
try:
# Remove 'v' prefix if present
if version_str.startswith("v"):
version_str = version_str[1:]
return semver.Version.parse(version_str)
except ValueError as e:
raise BuildError(f"Invalid semver format '{version_str}': {e}")
def get_git_info(self) -> tuple[str, str]:
"""Get git commit hash and branch"""
commit = "unknown"
branch = "unknown"
try:
result = self._run_command(
["git", "rev-parse", "--short", "HEAD"], check=False, verbose=False
)
if result.returncode == 0:
commit = result.stdout.strip()
result = self._run_command(
["git", "branch", "--show-current"], check=False, verbose=False
)
if result.returncode == 0:
branch = result.stdout.strip()
except Exception:
pass
return commit, branch
def prepare_build_env(self, version: semver.Version, datetime_tag: str) -> list[str]:
"""Prepare environment variables for the build"""
commit, branch = self.get_git_info()
is_ci = os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS")
base_image = f"{self.registry}/{self.image_name}"
# Create version string with dev suffix for local builds
version_str = str(version)
if self.dev_mode or not is_ci:
# Add dev suffix with short commit hash
version_str = f"{version}-dev.{commit[:7]}" if commit != "unknown" else f"{version}-dev"
# Prepare tags based on environment
tags = []
if self.dev_mode or not is_ci:
# Development build
dev_timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
tags = [
f"{base_image}:dev",
f"{base_image}:dev-{dev_timestamp}",
f"{base_image}:{version_str}",
]
console.print(f"[yellow]📦 Development mode - version: {version_str}[/yellow]")
else:
# Production build
tags = [
f"{base_image}:latest",
f"{base_image}:{datetime_tag}",
f"{base_image}:{version}",
f"{base_image}:v{version}",
]
# Add git commit tag
if commit != "unknown":
tags.append(f"{base_image}:{commit}")
# Build environment variables
self.env_vars = {
"IMAGE_TAG": tags[0],
"PLATFORMS": self.platforms,
"DOCKERFILE": self.dockerfile,
"VERSION": version_str,
"BUILD_DATE": datetime.now().isoformat(),
"GIT_COMMIT": commit,
"GIT_BRANCH": branch,
"REGISTRY": self.registry,
"IMAGE_NAME": self.image_name,
"CACHE_FROM": f"{base_image}:buildcache",
"CACHE_TO": f"{base_image}:buildcache",
}
return tags
def build_and_push_with_compose(
self, version: semver.Version, datetime_tag: str, tags: list[str]
):
"""Build and push images using Docker Compose"""
console.print(f"[blue]Building with Docker Compose for platforms: {self.platforms}[/blue]")
console.print("[blue]Tags:[/blue]")
for tag in tags:
console.print(f" • {tag}")
console.print("\n[yellow]Building images... (this may take a few minutes)[/yellow]")
# Build with compose
build_args = ["build", "sensor-simulator"]
# Add push flag if not skipping
if not self.skip_push:
build_args.append("--push")
# Add no-cache flag if requested
if not self.build_cache:
build_args.append("--no-cache")
# Build each tag separately (Docker Compose limitation)
for tag in tags:
self.env_vars["IMAGE_TAG"] = tag
result = self._run_compose_command(
build_args,
check=False,
capture_output=False, # Show build output
env=self.env_vars,
)
if result.returncode != 0:
raise BuildError(f"Build failed for tag {tag}")
console.print(f"[green]✓[/green] Built {tag}")
action = "built" if self.skip_push else "built and pushed"
console.print(f"\n[green]✓[/green] Successfully {action} all images")
return tags
def create_git_tag(self, version: semver.Version):
"""Create a git tag for the version"""
if self.dev_mode:
console.print("[dim]Skipping git tag creation in dev mode[/dim]")
return
tag = f"v{version}"
console.print(f"[blue]Creating git tag {tag}...[/blue]")
# Check if tag already exists
result = self._run_command(["git", "tag", "--list", tag], check=False, verbose=False)
if result.stdout.strip():
console.print(f"[yellow]![/yellow] Tag {tag} already exists, skipping")
return
# Create the tag
self._run_command(["git", "tag", tag], verbose=False)
console.print(f"[green]✓[/green] Created git tag {tag}")
# Push the tag if not skipping push
if not self.skip_push:
console.print(f"[blue]Pushing tag {tag} to remote...[/blue]")
self._run_command(["git", "push", "origin", tag], verbose=False)
console.print("[green]✓[/green] Pushed tag to remote")
def write_tag_files(self, version: semver.Version, datetime_tag: str, tags: list[str]):
"""Write tag information to files for reference"""
console.print("[blue]Writing tag information to files...[/blue]")
Path(".latest-image-tag").write_text(f"{datetime_tag}\n")
# Use the version string from env_vars which includes dev suffix if applicable
version_str = self.env_vars.get("VERSION", str(version))
Path(".latest-semver").write_text(f"{version_str}\n")
# Write first tag as the main registry image
if tags:
Path(".latest-registry-image").write_text(f"{tags[0]}\n")
# Write compose environment file
env_content = []
for key, value in self.env_vars.items():
env_content.append(f"{key}={value}")
Path(".env.build").write_text("\n".join(env_content) + "\n")
console.print(f" → .latest-image-tag: {datetime_tag}")
console.print(f" → .latest-semver: {version_str}")
console.print(f" → .latest-registry-image: {tags[0] if tags else 'N/A'}")
console.print(" → .env.build: Build environment saved")
def print_summary(self, version: semver.Version, datetime_tag: str, tags: list[str]):
"""Print build summary"""
is_ci = os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS")
table = Table(title="Docker Compose Build Summary", show_header=True)
table.add_column("Property", style="cyan")
table.add_column("Value", style="green")
# Get version string with dev suffix if applicable
version_str = self.env_vars.get("VERSION", str(version))
table.add_row("Build System", "Docker Compose")
table.add_row("Compose File", self.compose_file)
table.add_row("Image Name", self.image_name)
table.add_row("Registry", self.registry)
table.add_row("Build Mode", "Development" if self.dev_mode else "Production")
table.add_row("Build Type", "CI/CD" if is_ci else "Local")
table.add_row("Semantic Version", version_str)
table.add_row("DateTime Tag", datetime_tag)
table.add_row("Platforms", self.platforms)
table.add_row("Tags Created", str(len(tags)))
table.add_row("Push Status", "Skipped" if self.skip_push else "Pushed")
console.print(table)
if not self.skip_push:
console.print("\n[green]Ready to run! Copy and paste this command:[/green]\n")
# Use appropriate tag based on build type
tag_to_use = "dev" if self.dev_mode else "latest"
console.print("[dim]# Run with Docker Compose:[/dim]")
console.print("docker compose up -d")
console.print("\n[dim]# Or run with Docker directly:[/dim]")
run_cmd = f"""docker run --rm \\
--name sensor-log-generator \\
-v "$(pwd)/data":/app/data \\
-v "$(pwd)/config":/app/config \\
-e CONFIG_FILE=/app/config/config.yaml \\
-e IDENTITY_FILE=/app/config/node-identity.json \\
{self.registry}/{self.image_name}:{tag_to_use}"""
console.print(run_cmd)
def cleanup(self):
"""Cleanup builder if needed"""
try:
if hasattr(self, "builder_name") and not self.dev_mode:
# Keep builder for future use unless explicitly removed
pass
except Exception:
pass
@click.command()
@click.option("--image-name", envvar="IMAGE_NAME", help="Docker image name")
@click.option(
"--platforms",
envvar="PLATFORMS",
default="linux/amd64,linux/arm64",
help="Target platforms (comma-separated)",
)
@click.option("--dockerfile", envvar="DOCKERFILE", default="Dockerfile", help="Path to Dockerfile")
@click.option("--registry", envvar="REGISTRY", default="ghcr.io", help="Docker registry")
@click.option("--version-tag", envvar="VERSION_TAG", help="Explicit version to use (e.g., 1.2.3)")
@click.option(
"--version-bump",
envvar="VERSION_BUMP",
type=click.Choice(["major", "minor", "patch"]),
default="minor",
help="Version bump type",
)
@click.option(
"--dev/--prod",
"dev_mode",
envvar="DEV_MODE",
default=False,
help="Build in development mode (faster, single platform)",
)
@click.option(
"--skip-push/--push",
envvar="SKIP_PUSH",
default=False,
help="Skip pushing to registry (auto-enabled for local builds unless PUSH_TO_REGISTRY=true)",
)
@click.option("--no-cache", is_flag=True, envvar="NO_CACHE", help="Disable build cache")
@click.option("--no-login", is_flag=True, envvar="NO_LOGIN", help="Skip Docker registry login")
@click.option(
"--compose-file",
envvar="COMPOSE_FILE",
default="docker-compose.build.yml",
help="Docker Compose file for building",
)
def main(
image_name: str | None,
platforms: str,
dockerfile: str,
registry: str,
version_tag: str | None,
version_bump: str,
dev_mode: bool,
skip_push: bool,
no_cache: bool,
no_login: bool,
compose_file: str,
):
"""
Build and push multi-platform Docker images using Docker Compose.
This script uses Docker Compose for orchestrating multi-platform builds
with proper semantic versioning, caching, and registry management.
Push behavior:
- CI environments: Push enabled by default
- Local builds: Push disabled by default (set PUSH_TO_REGISTRY=true to enable)
Examples:
# Local build (no push by default)
./build.py
# Local build with push
PUSH_TO_REGISTRY=true ./build.py
# Development build (single platform)
./build.py --dev
# Build specific version
./build.py --version-tag 2.0.0
# Build for specific platforms
./build.py --platforms linux/amd64
"""
console.print("\n[bold blue]🐳 Docker Compose Multi-Platform Builder[/bold blue]")
console.print("[dim]Using Docker Compose for orchestrated builds[/dim]")
# Determine if we should push based on environment
is_ci = os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS")
push_to_registry = os.environ.get("PUSH_TO_REGISTRY", "").lower() in ["true", "1", "yes"]
# Override skip_push based on environment unless explicitly set
if not is_ci and not push_to_registry and not skip_push:
# Not in CI and PUSH_TO_REGISTRY not set, so skip push by default
skip_push = True
console.print("[yellow]📦 Local build detected - push disabled by default[/yellow]")
console.print("[dim] Set PUSH_TO_REGISTRY=true to enable pushing[/dim]")
elif push_to_registry and skip_push:
# User wants to push but also specified --skip-push, honor the flag
console.print(
"[yellow]⚠️ PUSH_TO_REGISTRY=true but --skip-push specified, skipping push[/yellow]"
)
builder = DockerComposeBuilder(
image_name=image_name,
platforms=platforms,
dockerfile=dockerfile,
registry=registry,
builder_name="multiarch-builder",
skip_push=skip_push,
build_cache=not no_cache,
require_login=not no_login,
compose_file=compose_file,
dev_mode=dev_mode,
)
try:
# Validate requirements
builder.validate_requirements()
# Check Docker login if needed
if not skip_push:
builder.check_docker_login()
# Setup buildx builder
builder.setup_buildx_builder()
# Determine version
if version_tag:
# Use explicit version
version = builder.parse_version(version_tag)
console.print(f"[blue]Using explicit version: {version}[/blue]")
else:
# Get current version and bump
current_version = builder.get_current_version()
if current_version:
console.print(f"[blue]Current version: {current_version}[/blue]")
else:
console.print("[blue]No existing version found, starting at 1.0.0[/blue]")
version = builder.bump_version(current_version, version_bump)
if dev_mode:
commit, _ = builder.get_git_info()
version_display = (
f"{version}-dev.{commit[:7]}" if commit != "unknown" else f"{version}-dev"
)
console.print(f"[blue]New version ({version_bump} bump): {version_display}[/blue]")
else:
console.print(f"[blue]New version ({version_bump} bump): {version}[/blue]")
# Generate datetime tag
datetime_tag = datetime.now().strftime("%y%m%d%H%M")
# Prepare build environment
tags = builder.prepare_build_env(version, datetime_tag)
# Build and push with Docker Compose
tags = builder.build_and_push_with_compose(version, datetime_tag, tags)
# Create git tag (only for production builds)
is_ci = os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS")
if not skip_push and not dev_mode and is_ci:
builder.create_git_tag(version)
# Write tag files
builder.write_tag_files(version, datetime_tag, tags)
# Print summary
builder.print_summary(version, datetime_tag, tags)
console.print("\n[bold green]✓ Build completed successfully![/bold green]")
except BuildError as e:
console.print(f"\n[bold red]✗ Build failed:[/bold red] {e}")
sys.exit(1)
except KeyboardInterrupt:
console.print("\n[yellow]Build cancelled by user[/yellow]")
sys.exit(130)
except Exception as e:
console.print(f"\n[bold red]✗ Unexpected error:[/bold red] {e}")
sys.exit(1)
finally:
builder.cleanup()
if __name__ == "__main__":
main()