From 9cc78e5d28e1fbf864db31c5f404493b5ab798fb Mon Sep 17 00:00:00 2001 From: sj Date: Tue, 18 Jun 2024 08:59:02 -0400 Subject: [PATCH 1/4] Trying to use Docker registry alias --- pants-plugins/macros.py | 6 ++++-- pants.ci.toml | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pants-plugins/macros.py b/pants-plugins/macros.py index 8d104ba..b7bf9ee 100644 --- a/pants-plugins/macros.py +++ b/pants-plugins/macros.py @@ -12,11 +12,13 @@ def docker_image_gha(name: str, **kwargs) -> None: # **kwargs, # ) + REPO_NAME = "dockerfiles" + cache_from = kwargs.pop( "cache_from", { "type": "registry", - "ref": f"ghcr.io/robotpajamas/dockerfiles/{name}/build-cache:main", + "ref": f"@ghcr/{REPO_NAME}/{name}/build-cache:main", }, ) cache_to = kwargs.pop( @@ -24,7 +26,7 @@ def docker_image_gha(name: str, **kwargs) -> None: { "type": "registry", "mode": "max", - "ref": f"ghcr.io/robotpajamas/dockerfiles/{name}/build-cache:main", + "ref": f"@ghcr/{REPO_NAME}/{name}/build-cache:main", }, ) diff --git a/pants.ci.toml b/pants.ci.toml index c65cb0f..16d425f 100644 --- a/pants.ci.toml +++ b/pants.ci.toml @@ -8,3 +8,6 @@ env_vars = [ "ACTIONS_CACHE_URL", "ACTIONS_RUNTIME_TOKEN", ] + +[docker.registries.ghcr] +address = "ghcr.io/robotpajamas" From 4730937664e146041ec2967abfb2a5bc2baa6838 Mon Sep 17 00:00:00 2001 From: sj Date: Tue, 18 Jun 2024 10:16:31 -0400 Subject: [PATCH 2/4] Echo some env vars --- .github/workflows/pants.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/pants.yml b/.github/workflows/pants.yml index 1098ff2..0de02a9 100644 --- a/.github/workflows/pants.yml +++ b/.github/workflows/pants.yml @@ -63,6 +63,18 @@ jobs: gha-cache-key: cache0-py${{ matrix.python_version }} named-caches-hash: ${{ hashFiles('build-support/lockfiles/*.lock') }} + - name: Emit GH env vars + run: | + echo "Event name: ${{ github.event_name }}" + echo "Base ref: ${{ github.base_ref }}" + echo "Head ref: ${{ github.head_ref }}" + echo "Ref: ${{ github.ref }}" + echo "Ref name: ${{ github.ref_name }}" + echo "Repo: ${{ github.repository }}" + echo "Owner: ${{ github.repository_owner }}" + echo "Actor: ${{ github.actor }}" + echo "Triggering Actor: ${{ github.triggering_actor }}" + - name: Bootstrap Pants run: | pants --version From 45fa51ace94d019be81d12cd41b1a17ae795fc5a Mon Sep 17 00:00:00 2001 From: sj Date: Tue, 18 Jun 2024 11:45:25 -0400 Subject: [PATCH 3/4] Testing out new cache strategy --- pants-plugins/macros.py | 50 ++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/pants-plugins/macros.py b/pants-plugins/macros.py index b7bf9ee..c7ad06e 100644 --- a/pants-plugins/macros.py +++ b/pants-plugins/macros.py @@ -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: /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. @@ -12,22 +48,14 @@ def docker_image_gha(name: str, **kwargs) -> None: # **kwargs, # ) - REPO_NAME = "dockerfiles" - + cache_from, cache_to = default_ghcr_cache(name) cache_from = kwargs.pop( "cache_from", - { - "type": "registry", - "ref": f"@ghcr/{REPO_NAME}/{name}/build-cache:main", - }, + cache_from, ) cache_to = kwargs.pop( "cache_to", - { - "type": "registry", - "mode": "max", - "ref": f"@ghcr/{REPO_NAME}/{name}/build-cache:main", - }, + cache_to, ) docker_image( # noqa: F821 From 8a7d77f10e02734bf05462a7ed6f0aa789f4b899 Mon Sep 17 00:00:00 2001 From: sj Date: Tue, 18 Jun 2024 11:49:18 -0400 Subject: [PATCH 4/4] Previous build added new cache - checking if hit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 63bf96d..e789601 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Dockerfiles +## Cache Strategy ## Deprecations/Removals