Skip to content

Commit f9bc9bd

Browse files
committed
Cache the PHP the musl legs build from source
The musl thread-safe legs and 8.2 build PHP from the release tarball, about 6 minutes per leg. install-alpine-php.sh now keeps that build in $PHP_CACHE_DIR, which phar.yml restores with actions/cache under the key `install-alpine-php.sh --resolve` prints (Alpine release, architecture, PHP release, thread safety), so a new patch release or RC builds again. Installing from the cache takes seconds. Amp-Thread-ID: https://ampcode.com/threads/T-01a18495-f2db-74a3-a734-f1c0dbe24b9f
1 parent a82af18 commit f9bc9bd

4 files changed

Lines changed: 99 additions & 7 deletions

File tree

‎.github/scripts/install-alpine-php.sh‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,63 @@
55
# no thread-safe PHP, while the official php:*-alpine Docker images are
66
# thread-safe from 8.6 on — and for a minor the Alpine release no longer
77
# packages (8.2).
8+
#
9+
# That build takes minutes, so with $PHP_CACHE_DIR set the PHP it installs
10+
# into /usr/local (where nothing else lives in the Alpine images) is kept
11+
# there as php.tar, and installed from there when the directory already has
12+
# it. phar.yml keeps the directory in actions/cache under the key
13+
# `install-alpine-php.sh --resolve` prints: the Alpine release, the
14+
# architecture, the PHP release build-php.sh resolves and the thread
15+
# safety, so a new patch release (or release candidate) or a new Alpine
16+
# snapshot builds again. --resolve installs no PHP and prints
17+
# `release=<key>` when this leg builds PHP, `release=` when it installs
18+
# Alpine's packages.
819
set -eu
920

10-
apk add --no-cache bash curl git make g++ musl-dev linux-headers patch tar zstd
21+
apk add --no-cache bash curl git make g++ musl-dev linux-headers patch tar zstd >&2
1122

1223
V="$(echo "$PHP_MINOR" | tr -d .)"
1324
# Alpine's php86 packages are currently only in edge/testing.
14-
if [ "$PHP_MINOR" = "8.6" ]; then
15-
echo 'https://dl-cdn.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories
25+
EDGE_TESTING='https://dl-cdn.alpinelinux.org/alpine/edge/testing'
26+
if [ "$PHP_MINOR" = "8.6" ] && ! grep -qx "$EDGE_TESTING" /etc/apk/repositories; then
27+
echo "$EDGE_TESTING" >> /etc/apk/repositories
1628
fi
1729

30+
BUILD_PHP="$(dirname "$0")/../turbo-build/build-php.sh"
31+
BUILDS_PHP=0
1832
if [ "${PHP_ZTS:-0}" = "1" ] || [ -z "$(apk search -q -x "php$V")" ]; then
33+
BUILDS_PHP=1
34+
fi
35+
36+
if [ "${1:-}" = "--resolve" ]; then
37+
if [ "$BUILDS_PHP" = "0" ]; then
38+
echo "release="
39+
exit 0
40+
fi
41+
apk add --no-cache jq >&2
42+
RELEASE="$(PHP_MINOR="$PHP_MINOR" PHP_RESOLVE_ONLY=1 sh "$BUILD_PHP")"
43+
ZTS_SUFFIX=""
44+
if [ "${PHP_ZTS:-0}" = "1" ]; then
45+
ZTS_SUFFIX="-zts"
46+
fi
47+
echo "release=alpine$(cat /etc/alpine-release)-$(uname -m)-php$RELEASE$ZTS_SUFFIX"
48+
exit 0
49+
fi
50+
51+
if [ "$BUILDS_PHP" = "1" ]; then
1952
apk add --no-cache jq pkgconf xz libxml2-dev oniguruma-dev curl-dev openssl-dev zlib-dev
20-
PHP_MINOR="$PHP_MINOR" PHP_ZTS="${PHP_ZTS:-0}" sh "$(dirname "$0")/../turbo-build/build-php.sh"
53+
if [ -n "${PHP_CACHE_DIR:-}" ] && [ -f "$PHP_CACHE_DIR/php.tar" ]; then
54+
if tar -xf "$PHP_CACHE_DIR/php.tar" -C /usr/local && php -r 'exit((int) PHP_ZTS === (int) getenv("PHP_ZTS") ? 0 : 1);'; then
55+
echo "install-alpine-php.sh: installed PHP $(php -r 'echo PHP_VERSION;') from $PHP_CACHE_DIR"
56+
exit 0
57+
fi
58+
echo "install-alpine-php.sh: the PHP in $PHP_CACHE_DIR does not run here; building it again" >&2
59+
fi
60+
PHP_MINOR="$PHP_MINOR" PHP_ZTS="${PHP_ZTS:-0}" sh "$BUILD_PHP"
61+
if [ -n "${PHP_CACHE_DIR:-}" ]; then
62+
mkdir -p "$PHP_CACHE_DIR"
63+
tar -cf "$PHP_CACHE_DIR/php.tar" -C /usr/local .
64+
fi
2165
exit 0
2266
fi
2367

‎.github/turbo-build/build-php.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ if [ -z "$PHP_URL" ] || [ -z "$PHP_SHA256" ]; then
4343
echo "build-php.sh: php.net lists no tar.xz with a sha256 for PHP $PHP_VERSION" >&2
4444
exit 1
4545
fi
46+
# install-alpine-php.sh --resolve: the release alone, for the cache key of
47+
# the PHP it builds
48+
if [ "${PHP_RESOLVE_ONLY:-0}" = "1" ]; then
49+
echo "$PHP_VERSION"
50+
exit 0
51+
fi
4652
echo "build-php.sh: building PHP $PHP_VERSION from $PHP_URL"
4753
export PHP_VERSION
4854
PHP_ZTS="${PHP_ZTS:-0}"

‎.github/workflows/phar.yml‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,10 +928,27 @@ jobs:
928928
- name: "Trust the checkout despite the container/host uid mismatch"
929929
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
930930

931+
# The thread-safe legs and 8.2 build PHP from source, minutes on every
932+
# leg; install-alpine-php.sh keeps that build in alpine-php-cache, under
933+
# a key naming exactly what it built (see the script). Nothing to cache
934+
# for a leg installing Alpine's packages (release= is empty).
935+
- name: "Resolve the PHP build to cache (Alpine container)"
936+
id: alpine-php
937+
if: matrix.target.family == 'musl'
938+
run: sh .github/scripts/install-alpine-php.sh --resolve >> "$GITHUB_OUTPUT"
939+
940+
- name: "Restore the PHP build (Alpine container)"
941+
if: steps.alpine-php.outputs.release != ''
942+
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
943+
with:
944+
path: "alpine-php-cache"
945+
key: "alpine-php-${{ steps.alpine-php.outputs.release }}-${{ hashFiles('.github/turbo-build/build-php.sh') }}"
946+
931947
# The build needs php/php-config on PATH for the matrix PHP version.
932948
- name: "Install PHP and build tools (Alpine container)"
933949
if: matrix.target.family == 'musl'
934-
run: sh .github/scripts/install-alpine-php.sh
950+
# the container's path of the workspace, which github.workspace is not
951+
run: PHP_CACHE_DIR="$GITHUB_WORKSPACE/alpine-php-cache" sh .github/scripts/install-alpine-php.sh
935952

936953
- name: "Install PHP (macOS)"
937954
if: matrix.target.family == 'macos'
@@ -1098,9 +1115,21 @@ jobs:
10981115
run: |
10991116
docker run -d --name alpine-build \
11001117
-v "$PWD:/work" -w /work \
1101-
-e PHP_MINOR -e PHP_ZTS \
1118+
-e PHP_MINOR -e PHP_ZTS -e PHP_CACHE_DIR=/work/alpine-php-cache \
11021119
"$ALPINE_IMAGE" sleep 7200
11031120
1121+
# see turbo-compile
1122+
- name: "Resolve the PHP build to cache"
1123+
id: alpine-php
1124+
run: docker exec alpine-build sh .github/scripts/install-alpine-php.sh --resolve >> "$GITHUB_OUTPUT"
1125+
1126+
- name: "Restore the PHP build"
1127+
if: steps.alpine-php.outputs.release != ''
1128+
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
1129+
with:
1130+
path: "alpine-php-cache"
1131+
key: "alpine-php-${{ steps.alpine-php.outputs.release }}-${{ hashFiles('.github/turbo-build/build-php.sh') }}"
1132+
11041133
- name: "Install PHP and build tools"
11051134
run: docker exec alpine-build sh .github/scripts/install-alpine-php.sh
11061135

@@ -2154,9 +2183,21 @@ jobs:
21542183
run: |
21552184
docker run -d --name alpine-test \
21562185
-v "$PWD:/work" -w /work \
2157-
-e PHP_MINOR -e PHP_ZTS \
2186+
-e PHP_MINOR -e PHP_ZTS -e PHP_CACHE_DIR=/work/alpine-php-cache \
21582187
"$ALPINE_IMAGE" sleep 7200
21592188
2189+
# see turbo-compile
2190+
- name: "Resolve the PHP build to cache"
2191+
id: alpine-php
2192+
run: docker exec alpine-test sh .github/scripts/install-alpine-php.sh --resolve >> "$GITHUB_OUTPUT"
2193+
2194+
- name: "Restore the PHP build"
2195+
if: steps.alpine-php.outputs.release != ''
2196+
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
2197+
with:
2198+
path: "alpine-php-cache"
2199+
key: "alpine-php-${{ steps.alpine-php.outputs.release }}-${{ hashFiles('.github/turbo-build/build-php.sh') }}"
2200+
21602201
- name: "Install PHP and tools"
21612202
run: docker exec alpine-test sh .github/scripts/install-alpine-php.sh
21622203

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ e2e/bashunit
2929
e2e/restart-manager/probe.log
3030
e2e/restart-manager/starts.log
3131
e2e/restart-manager/out.txt
32+
/alpine-php-cache

0 commit comments

Comments
 (0)