Skip to content

Commit

Permalink
Cleaned up Docker macro for GHCR (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshjoshi committed Jun 18, 2024
1 parent b7655b7 commit 36e9d5b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Dockerfiles

## Cache Strategy

## Deprecations/Removals

Expand Down
48 changes: 39 additions & 9 deletions pants-plugins/macros.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
def default_ghcr_cache(target_name: str) -> tuple[str, Optional[str]]: # noqa: F821
"""
From Github Actions default env vars, try to constitute
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
REGISTRY = "ghcr.io/robotpajamas"
REPO_NAME = "dockerfiles"

# These are always populated in a PR, but not in a direct push
head_ref: str = env("GITHUB_HEAD_REF", "") # noqa: F821
base_ref: str = env("GITHUB_BASE_REF", "") # noqa: F821

# This is always populated, but in a PR it is: <pr_number>/merge - if we're in a PR, ignore
ref_name: str = env("GITHUB_REF_NAME", "") if not head_ref else "" # noqa: F821

cache_from_refs = [
f"{REGISTRY}/{REPO_NAME}/{target_name}/build-cache:{tag.replace('/', '-')}"
for tag in [head_ref, base_ref, ref_name, "main"]
if tag
]
cache_from = [{"type": "registry", "ref": ref} for ref in cache_from_refs]

# Cache to the head_ref only in a PR, otherwise, cache to the ref_name
resolved_ref = head_ref or ref_name
cache_to = None
if resolved_ref:
cache_to = {
"type": "registry",
"mode": "max",
"ref": f"{REGISTRY}/{REPO_NAME}/{target_name}/build-cache:{resolved_ref.replace('/', '-')}",
}

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, by default.
Expand All @@ -12,20 +48,14 @@ def docker_image_gha(name: str, **kwargs) -> None:
# **kwargs,
# )

cache_from, cache_to = default_ghcr_cache(name)
cache_from = kwargs.pop(
"cache_from",
{
"type": "registry",
"ref": f"ghcr.io/robotpajamas/dockerfiles/{name}/build-cache:main",
},
cache_from,
)
cache_to = kwargs.pop(
"cache_to",
{
"type": "registry",
"mode": "max",
"ref": f"ghcr.io/robotpajamas/dockerfiles/{name}/build-cache:main",
},
cache_to,
)

docker_image( # noqa: F821
Expand Down
3 changes: 3 additions & 0 deletions pants.ci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ env_vars = [
"ACTIONS_CACHE_URL",
"ACTIONS_RUNTIME_TOKEN",
]

[docker.registries.ghcr]
address = "ghcr.io/robotpajamas"

0 comments on commit 36e9d5b

Please sign in to comment.