Skip to content

Commit 703718d

Browse files
Fix Docker cache tag length exceeding 128 character limit (#1029)
Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 40bd3f0 commit 703718d

File tree

1 file changed

+32
-1
lines changed
  • openhands-agent-server/openhands/agent_server/docker

1 file changed

+32
-1
lines changed

openhands-agent-server/openhands/agent_server/docker/build.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""
1515

1616
import argparse
17+
import hashlib
1718
import os
1819
import re
1920
import shutil
@@ -288,7 +289,37 @@ def versioned_tag(self) -> str:
288289

289290
@property
290291
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}"
292323
if GIT_REF in ("main", "refs/heads/main"):
293324
return f"{base}-main", base
294325
elif GIT_REF != "unknown":

0 commit comments

Comments
 (0)