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
124 changes: 124 additions & 0 deletions .github/scripts/smoke-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env bash
# Boots a built image and checks it does what its README promises, so a broken
# image never gets a floating tag. Run by build-and-push.yml between pushing an
# image by digest and tagging it; also runnable by hand:
#
# .github/scripts/smoke-test.sh <variant> <image> <php-version>
# .github/scripts/smoke-test.sh wordpress ghcr.io/aliasproject/php:8.4-frankenphp-wordpress 8.4
#
# variant: cli | base | dev | wordpress | wordpress-dev
# cli runs php; no server
# base, dev the frankenphp Tier 1 extensions
# wordpress, wordpress-dev Tier 1 plus the WordPress extension set
# The frankenphp variants are booted and served over HTTP, so the PHP that is
# checked is the one embedded in the server (not the separate `php` binary).
set -euo pipefail

if [ "$#" -ne 3 ]; then
echo "usage: $0 <cli|base|dev|wordpress|wordpress-dev> <image> <php-version>" >&2
exit 2
fi
variant=$1
image=$2
php_version=$3

tier1="pdo_mysql zip intl bcmath pcntl posix"
wordpress_ext="mysqli gd exif imagick redis igbinary soap"
case "$variant" in
cli) extensions="pdo_mysql zip intl bcmath pcntl" ;;
base | dev) extensions="$tier1" ;;
wordpress | wordpress-dev) extensions="$tier1 $wordpress_ext" ;;
*) echo "unknown variant: $variant" >&2; exit 2 ;;
esac

fail() { echo "SMOKE TEST FAILED ($variant, $image): $*" >&2; exit 1; }

# Shared checks on a PHP version string and a list of loaded extensions.
check_php() {
local version=$1 loaded=$2 ext
case "$version" in
"$php_version".*) ;;
*) fail "PHP version is '$version', expected $php_version.x" ;;
esac
for ext in $extensions; do
# Whole-word match, case-insensitive (get_loaded_extensions() and
# php -m disagree on case for some, e.g. "Zend OPcache").
printf '%s\n' "$loaded" | tr ' ' '\n' | grep -qix "$ext" \
|| fail "extension '$ext' is not loaded"
done
}

if [ "$variant" = cli ]; then
version=$(docker run --rm "$image" php -r 'echo PHP_VERSION;') || fail "php did not run"
loaded=$(docker run --rm "$image" php -m | tr '\n' ' ') || fail "php -m did not run"
check_php "$version" "$loaded"
uid=$(docker run --rm "$image" id -u) || fail "id did not run"
[ "$uid" != 0 ] || fail "runs as root"
echo "ok: $image (PHP $version, uid $uid)"
exit 0
fi

fixture=$(mktemp -d)
name="smoke-$$"
cleanup() {
docker rm -f "$name" >/dev/null 2>&1 || true
rm -rf "$fixture"
}
trap cleanup EXIT

# A page that reports what the server's own PHP looks like. The container user
# is not the runner's, so the fixture must be world-readable.
mkdir -p "$fixture/public"
cat > "$fixture/public/index.php" <<'PHP'
<?php
echo "VERSION=", PHP_VERSION, "\n";
echo "SAPI=", php_sapi_name(), "\n";
echo "EXTENSIONS=", implode(' ', get_loaded_extensions()), "\n";
PHP
chmod -R a+rX "$fixture"

# Short health interval: the image's own HEALTHCHECK (30s) would make the wait
# below much longer than it needs to be.
docker run -d --name "$name" -p 127.0.0.1::80 -v "$fixture:/app:ro" \
--health-interval 2s --health-timeout 5s --health-retries 5 --health-start-period 30s \
"$image" >/dev/null

health=starting
for _ in $(seq 1 60); do
state=$(docker inspect -f '{{.State.Status}}' "$name")
if [ "$state" != running ]; then
docker logs "$name" >&2 || true
fail "container is '$state', not running"
fi
health=$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$name")
[ "$health" = healthy ] && break
sleep 2
done
if [ "$health" != healthy ]; then
docker logs "$name" >&2 || true
fail "not healthy after 120s (health: $health)"
fi

port=$(docker port "$name" 80/tcp | head -n1 | sed 's/.*://')
body=$(curl -fsS --max-time 10 "http://127.0.0.1:$port/") \
|| fail "GET / did not return 2xx"

version=$(printf '%s\n' "$body" | sed -n 's/^VERSION=//p')
sapi=$(printf '%s\n' "$body" | sed -n 's/^SAPI=//p')
loaded=$(printf '%s\n' "$body" | sed -n 's/^EXTENSIONS=//p')
[ "$sapi" = frankenphp ] || fail "SAPI is '$sapi', expected frankenphp"
check_php "$version" "$loaded"

uid=$(docker exec "$name" id -u)
[ "$uid" != 0 ] || fail "runs as root"

# docker-configure ran at boot and rendered its snippets.
docker exec "$name" test -f /config/caddy/site.d/00-hardening.caddyfile \
|| fail "docker-configure did not render /config/caddy/site.d/00-hardening.caddyfile"
docker exec "$name" test -s /usr/local/etc/php/conf.d/app.ini \
|| fail "docker-configure did not render conf.d/app.ini"

# Composer is baked into every frankenphp image.
docker exec "$name" composer --version >/dev/null 2>&1 || fail "composer is not runnable"

echo "ok: $image (PHP $version, uid $uid, healthy)"
122 changes: 101 additions & 21 deletions .github/workflows/build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,40 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push ${{ matrix.name }}
# Pushed by digest only (no tags yet): the image has to boot before it
# gets the floating tags, same as the frankenphp images below.
- name: Build and push by digest ${{ matrix.name }}
id: build
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || contains(needs.detect-changes.outputs.changed_files, matrix.file) }}
uses: docker/build-push-action@v7
with:
context: ${{ matrix.context }}
file: ${{ matrix.file }}
push: true
platforms: ${{ matrix.platforms }}
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.tag }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.tag }}-latest
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.tag }}-${{ github.sha }}
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
# Scheduled runs rebuild from scratch: a cached layer would keep
# last week's Alpine packages whenever the base image is unchanged.
no-cache: ${{ github.event_name == 'schedule' }}
cache-from: type=gha,scope=${{ matrix.name }}
cache-to: type=gha,mode=max,scope=${{ matrix.name }}

- name: Smoke test ${{ matrix.name }}
if: ${{ steps.build.outcome == 'success' }}
run: |
tag="${{ matrix.tag }}"
.github/scripts/smoke-test.sh cli \
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build.outputs.digest }}" \
"${tag%%-*}"

- name: Tag ${{ matrix.name }}
if: ${{ steps.build.outcome == 'success' }}
run: |
docker buildx imagetools create \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.tag }} \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.tag }}-latest \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.tag }}-${{ github.sha }} \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build.outputs.digest }}

# frankenphp base images, split by architecture across two native runners
# instead of one QEMU multi-platform build. Measured from a real run: the
# exact same install-php-extensions step took 258s on native amd64 vs
Expand Down Expand Up @@ -240,17 +256,18 @@ jobs:
# Exact artifact names: a missing arch fails here rather than
# publishing a single-arch tag.
- name: Download digest (amd64)
id: digest_amd64
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || (contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile', matrix.version)) || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/docker-configure', matrix.version))) }}
uses: actions/download-artifact@v4
with:
path: /tmp/digests
path: /tmp/digests/amd64
name: digest-fpbase-${{ matrix.version }}-amd64

- name: Download digest (arm64)
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || (contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile', matrix.version)) || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/docker-configure', matrix.version))) }}
uses: actions/download-artifact@v4
with:
path: /tmp/digests
path: /tmp/digests/arm64
name: digest-fpbase-${{ matrix.version }}-arm64

- name: Set up Docker Buildx
Expand All @@ -265,17 +282,38 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Boot the amd64 image (the runner's own architecture) before it gets
# any tag. A failure here leaves the existing floating tags untouched,
# and holds back the images built FROM this one (their jobs need this
# one to succeed).
- name: Checkout smoke test script
if: ${{ steps.digest_amd64.outcome == 'success' }}
uses: actions/checkout@v7
with:
sparse-checkout: .github/scripts

- name: Smoke test (amd64)
if: ${{ steps.digest_amd64.outcome == 'success' }}
run: |
.github/scripts/smoke-test.sh "base" \
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:$(find /tmp/digests/amd64 -type f -printf '%f')" \
"${{ matrix.version }}"

- name: Create manifest list and push
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || (contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile', matrix.version)) || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/docker-configure', matrix.version))) }}
working-directory: /tmp/digests
run: |
# Exactly one amd64 and one arm64 digest.
test "$(ls | wc -l)" -eq 2
# Exactly one amd64 and one arm64 digest (file name = digest).
test "$(find amd64 -type f | wc -l)" -eq 1
test "$(find arm64 -type f | wc -l)" -eq 1
amd64=$(find amd64 -type f -printf '%f')
arm64=$(find arm64 -type f -printf '%f')
docker buildx imagetools create \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-frankenphp \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-frankenphp-latest \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-frankenphp-${{ github.sha }} \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:${amd64}" \
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:${arm64}"

# frankenphp-dev and frankenphp-wordpress, same digest-split/native-arm64
# treatment as build-frankenphp-base and for the same reason: both FROM
Expand Down Expand Up @@ -398,17 +436,18 @@ jobs:
# wordpress that glob also matched wordpress-dev's digests, so the
# -wordpress tags also carried the dev images.
- name: Download digest (amd64)
id: digest_amd64
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile.{1}', matrix.version, matrix.variant)) || (contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile', matrix.version)) || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/docker-configure', matrix.version))) }}
uses: actions/download-artifact@v4
with:
path: /tmp/digests
path: /tmp/digests/amd64
name: digest-fpdep-${{ matrix.version }}-${{ matrix.variant }}-amd64

- name: Download digest (arm64)
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile.{1}', matrix.version, matrix.variant)) || (contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile', matrix.version)) || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/docker-configure', matrix.version))) }}
uses: actions/download-artifact@v4
with:
path: /tmp/digests
path: /tmp/digests/arm64
name: digest-fpdep-${{ matrix.version }}-${{ matrix.variant }}-arm64

- name: Set up Docker Buildx
Expand All @@ -423,17 +462,38 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Boot the amd64 image (the runner's own architecture) before it gets
# any tag. A failure here leaves the existing floating tags untouched,
# and holds back the images built FROM this one (their jobs need this
# one to succeed).
- name: Checkout smoke test script
if: ${{ steps.digest_amd64.outcome == 'success' }}
uses: actions/checkout@v7
with:
sparse-checkout: .github/scripts

- name: Smoke test (amd64)
if: ${{ steps.digest_amd64.outcome == 'success' }}
run: |
.github/scripts/smoke-test.sh "${{ matrix.variant }}" \
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:$(find /tmp/digests/amd64 -type f -printf '%f')" \
"${{ matrix.version }}"

- name: Create manifest list and push
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile.{1}', matrix.version, matrix.variant)) || (contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile', matrix.version)) || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/docker-configure', matrix.version))) }}
working-directory: /tmp/digests
run: |
# Exactly one amd64 and one arm64 digest.
test "$(ls | wc -l)" -eq 2
# Exactly one amd64 and one arm64 digest (file name = digest).
test "$(find amd64 -type f | wc -l)" -eq 1
test "$(find arm64 -type f | wc -l)" -eq 1
amd64=$(find amd64 -type f -printf '%f')
arm64=$(find arm64 -type f -printf '%f')
docker buildx imagetools create \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-frankenphp-${{ matrix.variant }} \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-frankenphp-${{ matrix.variant }}-latest \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-frankenphp-${{ matrix.variant }}-${{ github.sha }} \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:${amd64}" \
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:${arm64}"

# wordpress-dev is FROM -wordpress, so it builds only after
# merge-frankenphp-dependent has published that tag. In the same matrix
Expand Down Expand Up @@ -537,17 +597,18 @@ jobs:
# Exact artifact names: a missing arch fails here rather than
# publishing a single-arch tag.
- name: Download digest (amd64)
id: digest_amd64
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile.{1}', matrix.version, matrix.variant)) || (contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile', matrix.version)) || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/docker-configure', matrix.version))) }}
uses: actions/download-artifact@v4
with:
path: /tmp/digests
path: /tmp/digests/amd64
name: digest-fpdep-${{ matrix.version }}-${{ matrix.variant }}-amd64

- name: Download digest (arm64)
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile.{1}', matrix.version, matrix.variant)) || (contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile', matrix.version)) || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/docker-configure', matrix.version))) }}
uses: actions/download-artifact@v4
with:
path: /tmp/digests
path: /tmp/digests/arm64
name: digest-fpdep-${{ matrix.version }}-${{ matrix.variant }}-arm64

- name: Set up Docker Buildx
Expand All @@ -562,17 +623,36 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Boot the amd64 image (the runner's own architecture) before it gets
# any tag. A failure here leaves the existing floating tags untouched.
- name: Checkout smoke test script
if: ${{ steps.digest_amd64.outcome == 'success' }}
uses: actions/checkout@v7
with:
sparse-checkout: .github/scripts

- name: Smoke test (amd64)
if: ${{ steps.digest_amd64.outcome == 'success' }}
run: |
.github/scripts/smoke-test.sh "${{ matrix.variant }}" \
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:$(find /tmp/digests/amd64 -type f -printf '%f')" \
"${{ matrix.version }}"

- name: Create manifest list and push
if: ${{ inputs.force_build_all || github.event_name == 'schedule' || inputs[matrix.force_input] || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile.{1}', matrix.version, matrix.variant)) || (contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/Dockerfile', matrix.version)) || contains(needs.detect-changes.outputs.changed_files, format('php/{0}/frankenphp/docker-configure', matrix.version))) }}
working-directory: /tmp/digests
run: |
# Exactly one amd64 and one arm64 digest.
test "$(ls | wc -l)" -eq 2
# Exactly one amd64 and one arm64 digest (file name = digest).
test "$(find amd64 -type f | wc -l)" -eq 1
test "$(find arm64 -type f | wc -l)" -eq 1
amd64=$(find amd64 -type f -printf '%f')
arm64=$(find arm64 -type f -printf '%f')
docker buildx imagetools create \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-frankenphp-${{ matrix.variant }} \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-frankenphp-${{ matrix.variant }}-latest \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-frankenphp-${{ matrix.variant }}-${{ github.sha }} \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:${amd64}" \
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:${arm64}"

summary:
runs-on: ubuntu-latest
Expand Down
Loading
Loading