Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up Docker macro for GHCR #37

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/pants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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"