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