Has anyone come up with a plugin or a decent way t...
# general
s
Has anyone come up with a plugin or a decent way to handle
cache_to
and
cache_from
without having to specify them manually? I find lots of chats but nothing conclusive?
w
What's the problem you're running into?
s
Every time I am defining a new
docker_image
, I need to manually fill in these values, and I haven't found a way to parametrize them
Copy code
docker_image(
    name="dev",
    source="Dockerfile.dev",
    cache_from=[
        {
            "type": "registry",
            "ref": "europe-docker.pkg.dev/xxxxx/infra/base-images/images/coder/dev:cache-{{build_args.DOCKER_TAG_BRANCH}}",
        },
        {
            "type": "registry",
            "ref": "europe-docker.pkg.dev/xxxxx/infra/base-images/images/coder/dev:main",
        },
    ],
    cache_to={
        "type": "registry",
        "ref": "europe-docker.pkg.dev/xxxxx/infra/base-images/images/coder/dev:cache-{{build_args.DOCKER_TAG_BRANCH}},mode=max",
    },
)
I do have these defaults setup
Copy code
__defaults__(
    dict(
        docker_image=dict(
            source="Dockerfile",
            build_platform=["linux/amd64"],
            secrets={"gcp": env("GOOGLE_APPLICATION_CREDENTIALS")},
            image_tags=[
                "{pants.hash}",
                "{build_args.DOCKER_TAG_BRANCH}",
                "{build_args.DOCKER_TAG_BRANCH_COMMIT}",
                "{build_args.DOCKER_TAG_COMMIT}",
            ],
        )
    )
)
and I wish I could do the same with
cache_from
and
cache_to
w
What about writing a macro?
s
I don't think you can get the right interpolation in the macro. do you have a working example?
we build the repository name like this:
Copy code
repository = "xxxxx/xxxxx{full_directory}/{name}"
and I don't think you get these interpolations in the macro
if wrong, could you show me?
w
In public, no - I worked with a client and we used a macro - but there may be some details that wouldn’t work in your scenario. We passed in whatever data we needed to that macro, and it was perfectly adequate for the job - but again, case by case
I’d recommend giving it a shot - because we had similar complexity, and while I don’t know if build_args are available directly, they could also be passed into the macro
s
I have given it several shots 🙂 I was hoping there was someone with a working example
thank you for the advice
s
You can use build_file_dir in a macro
Copy code
def my_image(name: str, ...):
    docker_image(
        name=name, 
        cache_to=f"repo/{build_file_dir()}/{name}"
        ...
    )
b
wow
😄 1
s
Copy code
# helpers/pants-plugins/macros/docker_image.py
original_docker_image = docker_image

def docker_image(**kwargs):
    cache_tag = "cache-{build_args.DOCKER_TAG_BRANCH}"
    name = kwargs["name"]
    repository = f"xxxxxxxx/{build_file_dir()}/{name}"
    cache_from=[
        {
            "type": "registry",
            "ref": repository + ":cache-{{build_args.DOCKER_TAG_BRANCH}}",
        },
        {
            "type": "registry",
            "ref": repository + ":main",
        },
    ]
    cache_to={
        "type": "registry",
        "ref": repository + ":cache-{{build_args.DOCKER_TAG_BRANCH}},mode=max",
    }
    
    kwargs.setdefault("cache_from", cache_from)
    kwargs.setdefault("cache_to", cache_to)

    original_docker_image(**kwargs)
and in
pants.toml
Copy code
build_file_prelude_globs = [
  "helpers/pants-plugins/macros/docker_image.py"
]
it works!
s
Nice