Skip to content

Commit

Permalink
Fixed macro typing
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshjoshi committed Jun 18, 2024
1 parent 93a2ef8 commit 8eb4011
Showing 1 changed file with 46 additions and 39 deletions.
85 changes: 46 additions & 39 deletions pants-plugins/macros.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
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.
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"

Expand Down Expand Up @@ -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,
)

0 comments on commit 8eb4011

Please sign in to comment.