diff --git a/README.md b/README.md index e10383d..763d042 100644 --- a/README.md +++ b/README.md @@ -222,3 +222,29 @@ 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 +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 +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..651eaa2 --- /dev/null +++ b/bin/build-and-push.sh @@ -0,0 +1,242 @@ +#!/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" +# 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 +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)" +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' "${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 + 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 according to ${WORKFLOW_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 "Matrix: ${WORKFLOW_FILE} on ${WORKFLOW_REF}" +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" + + # --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" + --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}" + ) + # 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}-amd64" + --cache-from "type=registry,ref=${cache_reference}-arm64" + ) + 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" + ) + 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