ci: add pull_request_target image build/mirror workflow flow

This commit is contained in:
kevinveenbirkenbach committed 2026-03-17 23:43:53 +01:00
commit e2bc2ea9e2
16 files changed
+384 -60

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
set -euo pipefail
: "${BUILD_CONTEXT_DIR:?Missing BUILD_CONTEXT_DIR}"
: "${MATRIX_DISTRO:?Missing MATRIX_DISTRO}"
: "${IMAGE_TAG:?Missing IMAGE_TAG}"
: "${GITHUB_REPOSITORY_OWNER:?Missing GITHUB_REPOSITORY_OWNER}"
: "${USE_NIX_TOKEN:?Missing USE_NIX_TOKEN}"
max_attempts="${MAX_ATTEMPTS:-7}"
retry_delay_seconds="${RETRY_DELAY_SECONDS:-20}"
attempt=1
nix_arg=()
if [[ "${USE_NIX_TOKEN}" == "true" ]]; then
: "${NIX_GITHUB_TOKEN:?Missing NIX_GITHUB_TOKEN}"
nix_arg=( --build-arg "NIX_CONFIG=access-tokens = github.com=${NIX_GITHUB_TOKEN}" )
else
echo "pull_request_target on fork detected: skipping NIX_CONFIG token build-arg."
fi
while true; do
echo "=== Build & push attempt ${attempt}/${max_attempts} ==="
if docker buildx build \
--file "${BUILD_CONTEXT_DIR}/Dockerfile" \
--push \
--tag "ghcr.io/${GITHUB_REPOSITORY_OWNER}/infinito-${MATRIX_DISTRO}:${IMAGE_TAG}" \
--build-arg "PKGMGR_IMAGE=ghcr.io/kevinveenbirkenbach/pkgmgr-${MATRIX_DISTRO}:stable" \
"${nix_arg[@]}" \
--cache-from "type=gha,scope=infinito-${MATRIX_DISTRO}" \
--cache-to "type=gha,mode=max,scope=infinito-${MATRIX_DISTRO}" \
"${BUILD_CONTEXT_DIR}"; then
echo "Build & push succeeded on attempt ${attempt}/${max_attempts}."
break
fi
if [[ "${attempt}" -ge "${max_attempts}" ]]; then
echo "Build & push failed after ${max_attempts} attempts."
exit 1
fi
echo "Attempt ${attempt} failed. Retrying in ${retry_delay_seconds}s..."
sleep "${retry_delay_seconds}"
attempt=$((attempt + 1))
done
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env bash
set -euo pipefail
: "${PR_NUMBER:?Missing PR_NUMBER}"
: "${GITHUB_REPOSITORY:?Missing GITHUB_REPOSITORY}"
: "${GITHUB_OUTPUT:?Missing GITHUB_OUTPUT}"
ref="refs/pull/${PR_NUMBER}/merge"
repo_url="https://github.com/${GITHUB_REPOSITORY}.git"
sha="${PR_MERGE_SHA:-}"
checkout_ref="${ref}"
if [[ -n "${sha}" ]]; then
if [[ ! "${sha}" =~ ^[0-9a-f]{40}$ ]]; then
echo "Invalid PR_MERGE_SHA '${sha}', falling back to ${ref}" >&2
sha=""
else
checkout_ref="${sha}"
fi
fi
if [[ -z "${sha}" ]]; then
sha="$(git ls-remote "${repo_url}" "${ref}" | awk '{print $1}')"
if [[ -z "${sha}" ]]; then
echo "Failed to resolve merge SHA for ${ref}" >&2
exit 1
fi
checkout_ref="${sha}"
fi
{
echo "checkout_ref=${checkout_ref}"
echo "image_tag=ci-${sha}"
} >> "${GITHUB_OUTPUT}"
echo "Resolved fork PR merge ref ${ref} -> ${sha} (checkout_ref=${checkout_ref})"
+70
View File
@@ -0,0 +1,70 @@
#!/usr/bin/env bash
set -euo pipefail
: "${GHCR_NAMESPACE:?Missing GHCR_NAMESPACE}"
: "${GHCR_PREFIX:?Missing GHCR_PREFIX}"
: "${REPO_ROOT:?Missing REPO_ROOT}"
max_attempts="${MAX_WAIT_ATTEMPTS:-180}"
sleep_seconds="${WAIT_SLEEP_SECONDS:-10}"
mapfile -t mirror_refs < <(
GHCR_NAMESPACE="${GHCR_NAMESPACE}" \
GHCR_PREFIX="${GHCR_PREFIX}" \
REPO_ROOT="${REPO_ROOT}" \
python - <<'PY'
import os
from pathlib import Path
from cli.mirror.providers import GHCRProvider
from cli.mirror.util import iter_role_images
provider = GHCRProvider(
os.environ["GHCR_NAMESPACE"],
os.environ["GHCR_PREFIX"],
)
repo_root = Path(os.environ["REPO_ROOT"]).resolve()
seen = set()
for img in iter_role_images(repo_root):
ref = f"{provider.image_base(img)}:{img.version}"
if ref in seen:
continue
seen.add(ref)
print(ref)
PY
)
total="${#mirror_refs[@]}"
if [[ "${total}" -eq 0 ]]; then
echo "No mirror refs found. Nothing to wait for."
exit 0
fi
echo "Fork pull_request detected; waiting for mirrored images from pull_request_target."
echo "Need ${total} mirror refs."
missing=()
for attempt in $(seq 1 "${max_attempts}"); do
missing=()
for ref in "${mirror_refs[@]}"; do
if ! skopeo inspect "docker://${ref}" >/dev/null 2>&1; then
missing+=("${ref}")
fi
done
if [[ "${#missing[@]}" -eq 0 ]]; then
echo "All mirrored images are available."
exit 0
fi
echo "[${attempt}/${max_attempts}] Missing ${#missing[@]}/${total} mirror refs. Waiting ${sleep_seconds}s..."
echo "Example missing: ${missing[0]}"
sleep "${sleep_seconds}"
done
echo "Timed out waiting for mirrored images." >&2
echo "Still missing (first 20):" >&2
for ref in "${missing[@]:0:20}"; do
echo " - ${ref}" >&2
done
exit 1
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -euo pipefail
: "${MATRIX_DISTRO:?Missing MATRIX_DISTRO}"
: "${IMAGE_TAG:?Missing IMAGE_TAG}"
: "${GITHUB_REPOSITORY_OWNER:?Missing GITHUB_REPOSITORY_OWNER}"
image="ghcr.io/${GITHUB_REPOSITORY_OWNER}/infinito-${MATRIX_DISTRO}:${IMAGE_TAG}"
max_attempts="${MAX_WAIT_ATTEMPTS:-180}"
sleep_seconds="${WAIT_SLEEP_SECONDS:-10}"
echo "Fork pull_request detected; waiting for image built via pull_request_target:"
echo " ${image}"
for attempt in $(seq 1 "${max_attempts}"); do
if docker buildx imagetools inspect "${image}" >/dev/null 2>&1; then
echo "Image is available: ${image}"
exit 0
fi
echo "[${attempt}/${max_attempts}] Waiting for image..."
sleep "${sleep_seconds}"
done
echo "Timed out waiting for prebuilt image: ${image}" >&2
exit 1