diff --git a/pants-plugins/macros.py b/pants-plugins/macros.py index 36bda0e..8208756 100644 --- a/pants-plugins/macros.py +++ b/pants-plugins/macros.py @@ -1,4 +1,48 @@ -def default_ghcr_cache(target_name: str) -> tuple[str, Optional[str]]: # noqa: F821 +def docker_image_gha(name: str, **kwargs) -> None: + """ + A macro to create a Docker image that caches to Github Actions cache, by default. + """ + cache_from: list[dict[str, str]] = kwargs.pop( + "cache_from", [{"type": "gha", "scope": name}] + ) + cache_to: dict[str, str] = kwargs.pop( + "cache_to", {"type": "gha", "mode": "max", "scope": name} + ) + + docker_image( # noqa: F821 + name=name, + cache_from=cache_from, + cache_to=cache_to, + **kwargs, + ) + + +def docker_image_ghcr(name: str, **kwargs) -> None: + """ + A macro to create a Docker image that caches to Github Container Registry, by default. + """ + + cache_from, cache_to = _default_ghcr_cache(name) + cache_from = kwargs.pop( + "cache_from", + cache_from, + ) + cache_to = kwargs.pop( + "cache_to", + cache_to, + ) + + docker_image( # noqa: F821 + name=f"{name}-ghcr", + cache_from=cache_from, + cache_to=cache_to, + **kwargs, + ) + + +def _default_ghcr_cache( + target_name: str, +) -> tuple[list[dict[str, str]], Optional[dict[str, str]]]: # noqa: F821 """ Using Github Actions default env vars - create a cache_from, and cache_to taking into account branches and PRs for better cache locality. @@ -6,6 +50,7 @@ def default_ghcr_cache(target_name: str) -> tuple[str, Optional[str]]: # noqa: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables """ # Need to specify this, because Pants doesn't reconcile `@ghcr` in the cache details + # https://github.com/pantsbuild/pants/issues/21082 REGISTRY = "ghcr.io/robotpajamas" REPO_NAME = "dockerfiles" @@ -34,41 +79,3 @@ def default_ghcr_cache(target_name: str) -> tuple[str, Optional[str]]: # noqa: } return cache_from, cache_to - - -def docker_image_gha(name: str, **kwargs) -> None: - """ - A macro to create a Docker image that caches to Github Actions cache, by default. - """ - cache_from = kwargs.pop("cache_from", {"type": "gha", "scope": name}) - cache_to = kwargs.pop("cache_to", {"type": "gha", "mode": "max", "scope": name}) - - docker_image( # noqa: F821 - name=name, - cache_from=cache_from, - cache_to=cache_to, - **kwargs, - ) - - -def docker_image_ghcr(name: str, **kwargs) -> None: - """ - A macro to create a Docker image that caches to Github Container Registry, by default. - """ - - cache_from, cache_to = default_ghcr_cache(name) - cache_from = kwargs.pop( - "cache_from", - cache_from, - ) - cache_to = kwargs.pop( - "cache_to", - cache_to, - ) - - docker_image( # noqa: F821 - name=f"{name}-ghcr", - cache_from=cache_from, - cache_to=cache_to, - **kwargs, - )