Skip to content

Instantly share code, notes, and snippets.

@dixyes
Created February 18, 2024 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dixyes/a3784b1f4dd3b53bf406da1cdc63fbfb to your computer and use it in GitHub Desktop.
Save dixyes/a3784b1f4dd3b53bf406da1cdc63fbfb to your computer and use it in GitHub Desktop.
Fuck buildx cache
#!/usr/bin/env python3
'''
Usage: ./fuckcache.py \
--mount=type=secret,id=npmrc,target=/root/.npmrc \
--mount=type=cache,target=/root/.npm \
--mount=type=cache,id=somecache,target=/var/cache/somecache
like docker RUN instruction
it will product dockerfile and build it
'''
import os
import sys
import re
import tempfile
import subprocess
argRe = re.compile(r"^--mount=([^,]+)(,[^,]+)*$")
caches = []
for argStr in sys.argv[1:]:
match = argRe.match(argStr)
if not match:
print("not recognized", argStr)
continue
arg = {}
for part in match.groups():
k, v = part.lstrip(",").split("=", maxsplit=1)
arg[k] = v
if arg["type"] != "cache":
print("not cache", argStr)
continue
arg["raw"] = argStr
caches.append(arg)
# make Dockerfile
tempdir = tempfile.mkdtemp()
print("tempdir", tempdir)
dockerfile = os.path.join(tempdir, "Dockerfile")
with open(dockerfile, "w") as f:
f.write("FROM alpine\n")
f.write("RUN \\\n")
for cache in caches:
f.write(f" {cache['raw']} \\\n")
f.write(f" set -x ; \\\n")
# re-order caches to delete recursive
for cache in sorted(caches, key=lambda x: len(x["target"]), reverse=True):
f.write(f" rm -rf {cache['target']}/* {cache['target']}/.* ; \\\n")
# make build failed
f.write(f" echo done; false\n")
# subprocess.run(["cat", f"{tempdir}/Dockerfile"])
subprocess.run(["docker", "buildx", "build", tempdir, "--force-rm", "--progress=plain"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment