From 1c99552585ed50c06e160c19b053faa815a43944 Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Wed, 4 Nov 2026 19:17:57 +0100 Subject: [PATCH 1/3] Add script to build and push images without GitHub Actions bin/build-and-push.sh builds the images on a workstation the way the build workflow does: from the latest release tag, for linux/amd64 and linux/arm64, with the same tags, labels and registry build cache, and with the PHP versions read from the workflow of that tag. Without options it only shows the plan; --build builds without pushing, --push builds and pushes after a confirmation. Refs OPS-8S Co-Authored-By: Claude Opus 5.5 --- README.md | 25 +++++ bin/build-and-push.sh | 227 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 252 insertions(+) create mode 100755 bin/build-and-push.sh diff --git a/README.md b/README.md index e10383d..5b39a37 100644 --- a/README.md +++ b/README.md @@ -222,3 +222,28 @@ be rebuilt using the latest Flownative base image (`flownative/base`). Whenever new images were built, a custom event is triggered which in turn triggers builds of the `flownative/beach-php` image, which is derived from this one. + +### Building without GitHub Actions + +If GitHub Actions is not available, `bin/build-and-push.sh` builds the +images on a workstation the way the build workflow does: from the latest +release tag, for `linux/amd64` and `linux/arm64`, with the same tags, +labels and build cache, and with the PHP versions taken from +`.github/workflows/docker.build.yaml` of that tag. + +```bash +docker login +docker login harbor.flownative.io + +bin/build-and-push.sh # show what would be built +bin/build-and-push.sh --push # build and push all versions +bin/build-and-push.sh --push --php 8.4 # only one version +``` + +`--build` builds without pushing, `--ref ` builds another tag, and +`--no-cache` builds from scratch. See `bin/build-and-push.sh --help`. + +The script does not trigger the builds of the derived images. Run the +script of the same name in `docker-beach-php` (and afterwards +`docker-beach-php-k8s`) and in `docker-composer` once the images are +pushed. diff --git a/bin/build-and-push.sh b/bin/build-and-push.sh new file mode 100755 index 0000000..50a4572 --- /dev/null +++ b/bin/build-and-push.sh @@ -0,0 +1,227 @@ +#!/usr/bin/env bash +# +# Builds the PHP images on a workstation the way the GitHub workflow +# .github/workflows/docker.build.yaml does, and optionally pushes them. +# Meant for the case that GitHub Actions is not available. +# +# Runs with the Bash 3.2 that ships with macOS. + +set -euo pipefail + +IMAGES="flownative/php harbor.flownative.io/docker/php" +CACHE_REPOSITORY="harbor.flownative.io/docker/php-build-cache" +PLATFORMS="linux/amd64,linux/arm64" +SOURCE_URL="https://github.com/flownative/docker-php" +WORKFLOW_FILE=".github/workflows/docker.build.yaml" +NEXT_STEPS="Images derived from these need to be rebuilt next: + docker-beach-php (then docker-beach-php-k8s) + docker-composer +Run bin/build-and-push.sh --push in each of them." + +usage() { + cat < Only build this version, e.g. 8.4 or 8.4.27 + --ref Build this Git tag instead of the latest release tag + --build Build all platforms, but do not push (e.g. to measure) + --push Build and push to Docker Hub and Harbor + --builder Buildx builder to use (default: flownative, created if missing) + --no-cache Build from scratch, as for a new PHP version + --yes Do not ask before pushing + -h, --help Show this help + +Log in first: + docker login + docker login harbor.flownative.io +EOF +} + +info() { printf '\033[1m%s\033[0m\n' "$*"; } +warn() { printf '\033[33mWarning: %s\033[0m\n' "$*" >&2; } +fail() { printf '\033[31mError: %s\033[0m\n' "$*" >&2; exit 1; } + +php_filter="" +ref="" +mode="plan" +builder="flownative" +assume_yes=false +use_cache=true + +while [ $# -gt 0 ]; do + case "$1" in + --php) php_filter="${2:?--php needs a version}"; shift 2 ;; + --ref) ref="${2:?--ref needs a tag}"; shift 2 ;; + --build) mode="build"; shift ;; + --push) mode="push"; shift ;; + --builder) builder="${2:?--builder needs a name}"; shift 2 ;; + --yes) assume_yes=true; shift ;; + --no-cache) use_cache=false; shift ;; + -h|--help) usage; exit 0 ;; + *) usage >&2; echo >&2; fail "Unknown option: $1" ;; + esac +done + +command -v docker >/dev/null || fail "docker is not installed" +docker buildx version >/dev/null 2>&1 || fail "docker buildx is not available" + +repository_root="$(cd "$(dirname "$0")/.." && git rev-parse --show-toplevel)" + +# --- Source ------------------------------------------------------------------- + +# GitHub may be the very thing that is down, so local tags have to do then. +if ! git -C "${repository_root}" fetch --quiet --tags --force origin 2>/dev/null; then + warn "Could not fetch tags from origin, using the tags known locally" +fi + +if [ -z "${ref}" ]; then + ref="$(git -C "${repository_root}" tag --list 'v*.*.*' --sort=-v:refname | head -n 1)" + [ -n "${ref}" ] || fail "No release tag found" +fi +git -C "${repository_root}" rev-parse --verify --quiet "${ref}^{commit}" >/dev/null \ + || fail "Unknown ref: ${ref}" + +work_directory="$(mktemp -d)" +worktree="${work_directory}/source" +cleanup() { + git -C "${repository_root}" worktree remove --force "${worktree}" 2>/dev/null || true + rm -rf "${work_directory}" +} +trap cleanup EXIT + +git -C "${repository_root}" worktree add --quiet --detach "${worktree}" "${ref}" +revision="$(git -C "${worktree}" rev-parse HEAD)" +created="$(date -u +%Y-%m-%dT%H:%M:%SZ)" + +# --- Versions ----------------------------------------------------------------- + +# The matrix line looks like: "php: [ 8.2.35, 8.3.36, 8.4.27, 8.5.12 ]" +matrix="$(sed -n 's/^[[:space:]]*php:[[:space:]]*\[\(.*\)\].*$/\1/p' "${worktree}/${WORKFLOW_FILE}" | head -n 1 | tr ',' ' ' | tr -s ' ' | sed 's/^ //; s/ $//')" +[ -n "${matrix}" ] || fail "No PHP versions found in ${WORKFLOW_FILE} of ${ref}" + +versions="" +for version in ${matrix}; do + echo "${version}" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$' \ + || fail "Unexpected PHP version '${version}' in ${WORKFLOW_FILE}" + if [ -z "${php_filter}" ] || [ "${php_filter}" = "${version}" ] || [ "${php_filter}" = "${version%.*}" ]; then + versions="${versions} ${version}" + fi +done +[ -n "${versions}" ] || fail "PHP ${php_filter} is not built by ${ref} (available: ${matrix})" + +tags_for() { + local version="$1" image + for image in ${IMAGES}; do + echo "${image}:${version%.*}" + echo "${image}:${version}" + done +} + +# --- Plan --------------------------------------------------------------------- + +info "Source: ${ref} (${revision})" +info "Platforms: ${PLATFORMS}" +info "Mode: ${mode}" +info "Cache: ${use_cache}" +for version in ${versions}; do + echo + info "PHP ${version}" + tags_for "${version}" | sed 's/^/ /' +done +echo + +if [ "${mode}" = "plan" ]; then + echo "Nothing was built. Use --build to build or --push to build and push." + exit 0 +fi + +# --- Preconditions ------------------------------------------------------------ + +if [ "${mode}" = "push" ]; then + docker_config="${DOCKER_CONFIG:-${HOME}/.docker}/config.json" + for registry in "https://index.docker.io/v1/" "harbor.flownative.io"; do + grep -q "\"${registry}\"" "${docker_config}" 2>/dev/null \ + || fail "Not logged in to ${registry}, run: docker login ${registry#https://index.docker.io/v1/}" + done + + if [ "${assume_yes}" != true ]; then + [ -t 0 ] || fail "Refusing to push without confirmation, pass --yes" + read -r -p "Push these images to Docker Hub and Harbor? [y/N] " answer + case "${answer}" in + y|Y|yes) ;; + *) echo "Aborted."; exit 1 ;; + esac + fi +fi + +if ! docker buildx inspect "${builder}" >/dev/null 2>&1; then + info "Creating buildx builder ${builder}" + docker buildx create --name "${builder}" --driver docker-container --bootstrap >/dev/null +fi + +# --- Build -------------------------------------------------------------------- + +summary="" +for version in ${versions}; do + cache_reference="${CACHE_REPOSITORY}:${version%.*}" + metadata_file="${work_directory}/metadata-${version}.json" + + arguments=( + --builder "${builder}" + --platform "${PLATFORMS}" + --build-arg "PHP_VERSION=${version}" + --label "org.opencontainers.image.title=PHP" + --label "org.opencontainers.image.description=Docker image providing PHP CLI and PHP-FPM with various extensions pre-installed" + --label "org.opencontainers.image.licenses=MIT" + --label "org.opencontainers.image.vendor=Flownative GmbH" + --label "org.opencontainers.image.version=${version}" + --label "org.opencontainers.image.created=${created}" + --label "org.opencontainers.image.revision=${revision}" + --label "org.opencontainers.image.source=${SOURCE_URL}" + --label "org.opencontainers.image.url=${SOURCE_URL}" + --metadata-file "${metadata_file}" + ) + if [ "${use_cache}" = true ]; then + arguments+=(--cache-from "type=registry,ref=${cache_reference}") + else + arguments+=(--no-cache) + fi + for tag in $(tags_for "${version}"); do + arguments+=(--tag "${tag}") + done + if [ "${mode}" = "push" ]; then + # Like docker/build-push-action does for public repositories + arguments+=( + --push + --provenance "mode=max" + --cache-to "type=registry,ref=${cache_reference},mode=max,image-manifest=true,oci-mediatypes=true,ignore-error=true" + ) + fi + + info "Building PHP ${version}" + started=${SECONDS} + if ! docker buildx build "${arguments[@]}" "${worktree}"; then + fail "Build of PHP ${version} failed after $(( (SECONDS - started) / 60 )) min" + fi + duration=$(( SECONDS - started )) + + digest="$(sed -n 's/.*"containerimage\.digest":[[:space:]]*"\([^"]*\)".*/\1/p' "${metadata_file}" | head -n 1)" + summary="${summary}$(printf ' PHP %-8s %3d min %02d s %s' "${version}" $((duration / 60)) $((duration % 60)) "${digest}") +" +done + +# --- Summary ------------------------------------------------------------------ + +echo +info "Done (${mode})" +printf '%s' "${summary}" +if [ "${mode}" = "push" ]; then + echo + echo "${NEXT_STEPS}" +fi From 8f52ed2391e260804a1646268b820ac75917c5d3 Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Wed, 4 Nov 2026 19:28:49 +0100 Subject: [PATCH 2/3] Always pull the base image when building locally The nightly builds are meant to pick up the newest base image, which is published under the same tag, so a builder must not use the copy it already has. Refs OPS-8S Co-Authored-By: Claude Opus 5.5 --- bin/build-and-push.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/build-and-push.sh b/bin/build-and-push.sh index 50a4572..efced85 100755 --- a/bin/build-and-push.sh +++ b/bin/build-and-push.sh @@ -172,9 +172,12 @@ for version in ${versions}; do cache_reference="${CACHE_REPOSITORY}:${version%.*}" metadata_file="${work_directory}/metadata-${version}.json" + # --pull, because the nightly builds are meant to pick up the newest + # base image, which is published under the same tag. arguments=( --builder "${builder}" --platform "${PLATFORMS}" + --pull --build-arg "PHP_VERSION=${version}" --label "org.opencontainers.image.title=PHP" --label "org.opencontainers.image.description=Docker image providing PHP CLI and PHP-FPM with various extensions pre-installed" From 975f131dea2696a9b4512854ee34927903f26394 Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Thu, 5 Nov 2026 09:10:50 +0100 Subject: [PATCH 3/3] Read the build matrix from main, like the workflow On GitHub the matrix comes from the workflow on main, while the code is the one of the latest release tag. The script read the matrix from the workflow of the tag instead, so a version entered on main but not yet tagged was built by the workflow but not by the script. The workflow now keeps one build cache per platform. The script builds both platforms at once, so it reads both caches and writes none, instead of replacing them with a combined one. Refs OPS-8S Co-Authored-By: Claude Opus 5.5 --- README.md | 7 ++++--- bin/build-and-push.sh | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5b39a37..763d042 100644 --- a/README.md +++ b/README.md @@ -227,9 +227,10 @@ derived from this one. If GitHub Actions is not available, `bin/build-and-push.sh` builds the images on a workstation the way the build workflow does: from the latest -release tag, for `linux/amd64` and `linux/arm64`, with the same tags, -labels and build cache, and with the PHP versions taken from -`.github/workflows/docker.build.yaml` of that tag. +release tag, for `linux/amd64` and `linux/arm64`, with the same tags +and labels, and with the PHP versions taken from +`.github/workflows/docker.build.yaml` on `main`, like on GitHub. It +reads the registry build cache of the workflow, but does not write it. ```bash docker login diff --git a/bin/build-and-push.sh b/bin/build-and-push.sh index efced85..651eaa2 100755 --- a/bin/build-and-push.sh +++ b/bin/build-and-push.sh @@ -13,6 +13,9 @@ CACHE_REPOSITORY="harbor.flownative.io/docker/php-build-cache" PLATFORMS="linux/amd64,linux/arm64" SOURCE_URL="https://github.com/flownative/docker-php" WORKFLOW_FILE=".github/workflows/docker.build.yaml" +# Like on GitHub, the matrix comes from the workflow on main, while the +# code is the one of the latest release tag. +WORKFLOW_REF="origin/main" NEXT_STEPS="Images derived from these need to be rebuilt next: docker-beach-php (then docker-beach-php-k8s) docker-composer @@ -23,7 +26,7 @@ usage() { Usage: $(basename "$0") [options] Builds the PHP images from the latest release tag, with the same tags, -labels, platforms and build cache as the GitHub workflow. +labels and platforms as the GitHub workflow, and reads its build cache. Without --build or --push only the plan is shown. @@ -98,12 +101,15 @@ trap cleanup EXIT git -C "${repository_root}" worktree add --quiet --detach "${worktree}" "${ref}" revision="$(git -C "${worktree}" rev-parse HEAD)" created="$(date -u +%Y-%m-%dT%H:%M:%SZ)" +workflow="${work_directory}/workflow.yaml" +git -C "${repository_root}" show "${WORKFLOW_REF}:${WORKFLOW_FILE}" > "${workflow}" \ + || fail "Could not read ${WORKFLOW_FILE} from ${WORKFLOW_REF}" # --- Versions ----------------------------------------------------------------- # The matrix line looks like: "php: [ 8.2.35, 8.3.36, 8.4.27, 8.5.12 ]" -matrix="$(sed -n 's/^[[:space:]]*php:[[:space:]]*\[\(.*\)\].*$/\1/p' "${worktree}/${WORKFLOW_FILE}" | head -n 1 | tr ',' ' ' | tr -s ' ' | sed 's/^ //; s/ $//')" -[ -n "${matrix}" ] || fail "No PHP versions found in ${WORKFLOW_FILE} of ${ref}" +matrix="$(sed -n 's/^[[:space:]]*php:[[:space:]]*\[\(.*\)\].*$/\1/p' "${workflow}" | head -n 1 | tr ',' ' ' | tr -s ' ' | sed 's/^ //; s/ $//')" +[ -n "${matrix}" ] || fail "No PHP versions found in ${WORKFLOW_FILE} of ${WORKFLOW_REF}" versions="" for version in ${matrix}; do @@ -113,7 +119,7 @@ for version in ${matrix}; do versions="${versions} ${version}" fi done -[ -n "${versions}" ] || fail "PHP ${php_filter} is not built by ${ref} (available: ${matrix})" +[ -n "${versions}" ] || fail "PHP ${php_filter} is not built according to ${WORKFLOW_REF} (available: ${matrix})" tags_for() { local version="$1" image @@ -126,6 +132,7 @@ tags_for() { # --- Plan --------------------------------------------------------------------- info "Source: ${ref} (${revision})" +info "Matrix: ${WORKFLOW_FILE} on ${WORKFLOW_REF}" info "Platforms: ${PLATFORMS}" info "Mode: ${mode}" info "Cache: ${use_cache}" @@ -190,8 +197,14 @@ for version in ${versions}; do --label "org.opencontainers.image.url=${SOURCE_URL}" --metadata-file "${metadata_file}" ) + # The workflow keeps one cache per platform. This build covers both + # platforms at once, so it reads both caches but writes none, rather + # than replacing them with a combined one. if [ "${use_cache}" = true ]; then - arguments+=(--cache-from "type=registry,ref=${cache_reference}") + arguments+=( + --cache-from "type=registry,ref=${cache_reference}-amd64" + --cache-from "type=registry,ref=${cache_reference}-arm64" + ) else arguments+=(--no-cache) fi @@ -203,7 +216,6 @@ for version in ${versions}; do arguments+=( --push --provenance "mode=max" - --cache-to "type=registry,ref=${cache_reference},mode=max,image-manifest=true,oci-mediatypes=true,ignore-error=true" ) fi