|
14 | 14 | """ |
15 | 15 |
|
16 | 16 | import argparse |
| 17 | +import hashlib |
17 | 18 | import os |
18 | 19 | import re |
19 | 20 | import shutil |
@@ -284,7 +285,37 @@ def versioned_tag(self) -> str: |
284 | 285 |
|
285 | 286 | @property |
286 | 287 | def cache_tags(self) -> tuple[str, str]: |
287 | | - base = f"buildcache-{self.target}-{self.base_image_slug}" |
| 288 | + # Docker image tags have a 128-character limit. |
| 289 | + # If the base slug is too long, hash it to create a shorter unique identifier. |
| 290 | + MAX_TAG_LENGTH = 128 |
| 291 | + base_slug = self.base_image_slug |
| 292 | + |
| 293 | + # Reserve space for prefix, branch, and separators |
| 294 | + prefix = f"buildcache-{self.target}-" |
| 295 | + branch_suffix = ( |
| 296 | + f"-{_sanitize_branch(GIT_REF)}" |
| 297 | + if GIT_REF not in ("main", "refs/heads/main", "unknown") |
| 298 | + else "" |
| 299 | + ) |
| 300 | + main_suffix = "-main" if GIT_REF in ("main", "refs/heads/main") else "" |
| 301 | + |
| 302 | + # Calculate available space for base_slug |
| 303 | + reserved = len(prefix) + max(len(branch_suffix), len(main_suffix)) |
| 304 | + available = MAX_TAG_LENGTH - reserved |
| 305 | + |
| 306 | + # If base_slug is too long, use a hash |
| 307 | + if len(base_slug) > available: |
| 308 | + # Use first 8 chars of SHA256 hash for uniqueness while keeping it short |
| 309 | + hash_digest = hashlib.sha256(base_slug.encode()).hexdigest()[:12] |
| 310 | + base_slug_short = hash_digest |
| 311 | + logger.debug( |
| 312 | + f"[build] Base image slug too long ({len(base_slug)} chars), " |
| 313 | + f"using hash: {base_slug_short}" |
| 314 | + ) |
| 315 | + else: |
| 316 | + base_slug_short = base_slug |
| 317 | + |
| 318 | + base = f"{prefix}{base_slug_short}" |
288 | 319 | if GIT_REF in ("main", "refs/heads/main"): |
289 | 320 | return f"{base}-main", base |
290 | 321 | elif GIT_REF != "unknown": |
|
0 commit comments