Skip to content
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: 7 additions & 5 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ permissions:

env:
REGISTRY: ghcr.io
IMAGE: ghcr.io/${{ github.repository }}
FONT_NAME: afio

jobs:
Expand Down Expand Up @@ -67,17 +66,20 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- id: sha
- id: meta-inputs
env:
SHA: ${{ github.event.pull_request.head.sha }}
REPOSITORY: ${{ github.repository }}
run: |
SHA="${{ github.event.pull_request.head.sha }}"
echo "short=${SHA::7}" >> "$GITHUB_OUTPUT"
echo "image=${REGISTRY}/$(printf '%s' "$REPOSITORY" | LC_ALL=C tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT"

- id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.IMAGE }}
images: ${{ steps.meta-inputs.outputs.image }}
tags: |
type=raw,value=sha-${{ steps.sha.outputs.short }}
type=raw,value=sha-${{ steps.meta-inputs.outputs.short }}

- uses: docker/build-push-action@v7
with:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ permissions:

env:
REGISTRY: ghcr.io
IMAGE: ghcr.io/${{ github.repository }}
FONT_NAME: afio

jobs:
Expand Down Expand Up @@ -146,7 +145,9 @@ jobs:
env:
VERSION: ${{ steps.pr.outputs.version }}
SHORT_SHA: ${{ steps.pr.outputs.short_sha }}
REPOSITORY: ${{ github.repository }}
run: |
IMAGE="${REGISTRY}/$(printf '%s' "$REPOSITORY" | LC_ALL=C tr '[:upper:]' '[:lower:]')"
docker buildx imagetools create \
-t "${IMAGE}:${VERSION}" \
-t "${IMAGE}:latest" \
Expand Down
43 changes: 42 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,46 @@ set -e

export DOCKER_BUILDKIT=1

normalize_image_ref() {
local ref="$1"
local digest=""
local without_digest="$ref"
local last_component tag="" name
local first rest first_lower

if [[ "$ref" == *@* ]]; then
digest="@${ref#*@}"
without_digest="${ref%%@*}"
fi

last_component="${without_digest##*/}"
if [[ "$last_component" == *:* ]]; then
tag=":${last_component#*:}"
name="${without_digest%"$tag"}"
else
name="$without_digest"
fi

# Match Docker's splitDockerDomain rule: preserve a registry domain component
# (contains '.' or ':', equals 'localhost', or has uppercase letters) so a
# private registry like Registry/font is not redirected to Docker Hub.
if [[ "$name" == */* ]]; then
first="${name%%/*}"
rest="${name#*/}"
first_lower=$(printf '%s' "$first" | LC_ALL=C tr '[:upper:]' '[:lower:]')
if [[ "$first" == *.* || "$first" == *:* || "$first" == "localhost" || "$first" != "$first_lower" ]]; then
rest=$(printf '%s' "$rest" | LC_ALL=C tr '[:upper:]' '[:lower:]')
name="${first}/${rest}"
else
name=$(printf '%s' "$name" | LC_ALL=C tr '[:upper:]' '[:lower:]')
fi
else
name=$(printf '%s' "$name" | LC_ALL=C tr '[:upper:]' '[:lower:]')
fi

printf '%s%s%s' "$name" "$tag" "$digest"
}

BUILD_DIR=/build
FONT_NAME=${FONT_NAME:-afio}
BUILD_PLAN=${BUILD_PLAN:-private-build-plans.toml}
Expand All @@ -17,7 +57,7 @@ mkdir -p "$OUTPUT_DIR"

if [ -z "$IMAGE_REF" ]; then
VERSION=$(cat VERSION | tr -d '[:space:]')
IMAGE_REF="${IMAGE}:${VERSION}"
IMAGE_REF=$(normalize_image_ref "${IMAGE}:${VERSION}")

if docker pull "$IMAGE_REF" 2>/dev/null; then
echo "Using pre-built image: $IMAGE_REF"
Expand All @@ -30,6 +70,7 @@ if [ -z "$IMAGE_REF" ]; then
IMAGE_REF=$(cat "$OUTPUT_DIR"/iddfile)
fi
else
IMAGE_REF=$(normalize_image_ref "$IMAGE_REF")
echo "Using provided image: $IMAGE_REF"
fi

Expand Down
136 changes: 136 additions & 0 deletions tests/test_build_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,142 @@ def run_plan(contents):
self.assertNotEqual(first, changed)
self.assertFalse((Path(changed) / "obsolete.ttf").exists())

def test_image_ref_repository_name_is_lowercased(self):
cases = [
(
"ghcr.io/TaivasJumala/iosevka-nerd-font:sha-bf43ccc",
"ghcr.io/taivasjumala/iosevka-nerd-font:sha-bf43ccc",
),
(
"ghcr.io/TaivasJumala/iosevka-nerd-font:V0.0.19-RC1",
"ghcr.io/taivasjumala/iosevka-nerd-font:V0.0.19-RC1",
),
(
"localhost:5000/TaivasJumala/Iosevka:V1@sha256:abcdef",
"localhost:5000/taivasjumala/iosevka:V1@sha256:abcdef",
),
(
"localhost:5000/TaivasJumala/Iosevka",
"localhost:5000/taivasjumala/iosevka",
),
(
"Registry/font:V1",
"Registry/font:V1",
),
(
"Registry/MyFont:V1",
"Registry/myfont:V1",
),
(
"owner/MyFont:V1",
"owner/myfont:V1",
),
(
"sha256:0123456789abcdef",
"sha256:0123456789abcdef",
),
]

with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
docker = root / "docker"
docker.write_text('#!/bin/sh\nprintf "%s\\n" "$@" > "$DOCKER_ARGS"\n')
docker.chmod(0o755)
arguments = root / "arguments"
plan = root / "private-build-plans.toml"
plan.write_text("[buildPlans.afio]\n")

for image_ref, expected in cases:
with self.subTest(image_ref=image_ref):
env = {
**os.environ,
"PATH": f"{root}:{os.environ['PATH']}",
"IMAGE_REF": image_ref,
"BUILD_PLAN": plan.name,
"DOCKER_ARGS": str(arguments),
}
subprocess.run(["bash", str(BUILD_SCRIPT)], cwd=root, env=env,
check=True, capture_output=True, text=True)
args = arguments.read_text().splitlines()
self.assertEqual(args[-3], expected)

def test_image_env_repository_name_is_lowercased_before_pull(self):
cases = [
(
"ghcr.io/TaivasJumala/iosevka-nerd-font",
"ghcr.io/taivasjumala/iosevka-nerd-font:0.0.19",
),
(
"Registry/MyFont",
"Registry/myfont:0.0.19",
),
]

for image, expected in cases:
with self.subTest(image=image):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
docker = root / "docker"
docker.write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> "$DOCKER_LOG"\n')
docker.chmod(0o755)
log = root / "docker.log"
(root / "VERSION").write_text("0.0.19\n")
plan = root / "private-build-plans.toml"
plan.write_text("[buildPlans.afio]\n")
env = {
**os.environ,
"PATH": f"{root}:{os.environ['PATH']}",
"IMAGE": image,
"IMAGE_REF": "",
"BUILD_PLAN": plan.name,
"DOCKER_LOG": str(log),
}
subprocess.run(["bash", str(BUILD_SCRIPT)], cwd=root, env=env,
check=True, capture_output=True, text=True)
calls = log.read_text().splitlines()
self.assertEqual(calls[0], f"pull {expected}")
self.assertIn(f" {expected} -c ", calls[1])

def test_failed_pull_falls_back_to_local_build_image_id(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
docker = root / "docker"
docker.write_text(
'#!/bin/sh\n'
'printf "%s\\n" "$*" >> "$DOCKER_LOG"\n'
'if [ "$1" = "pull" ]; then\n'
' exit 1\n'
'fi\n'
'if [ "$1" = "buildx" ]; then\n'
' while [ "$#" -gt 0 ]; do\n'
' if [ "$1" = "--iidfile" ]; then\n'
' printf "sha256:deadbeef" > "$2"\n'
' break\n'
' fi\n'
' shift\n'
' done\n'
'fi\n'
)
docker.chmod(0o755)
log = root / "docker.log"
(root / "VERSION").write_text("0.0.19\n")
plan = root / "private-build-plans.toml"
plan.write_text("[buildPlans.afio]\n")
env = {
**os.environ,
"PATH": f"{root}:{os.environ['PATH']}",
"IMAGE": "ghcr.io/TaivasJumala/iosevka-nerd-font",
"IMAGE_REF": "",
"BUILD_PLAN": plan.name,
"DOCKER_LOG": str(log),
}
subprocess.run(["bash", str(BUILD_SCRIPT)], cwd=root, env=env,
check=True, capture_output=True, text=True)
calls = log.read_text().splitlines()
self.assertEqual(calls[0], "pull ghcr.io/taivasjumala/iosevka-nerd-font:0.0.19")
self.assertTrue(calls[1].startswith("buildx build "))
self.assertIn(" sha256:deadbeef -c ", calls[2])


if __name__ == "__main__":
unittest.main()