diff --git a/.github/workflows/update-php.yml b/.github/workflows/update-php.yml new file mode 100644 index 0000000..a00307b --- /dev/null +++ b/.github/workflows/update-php.yml @@ -0,0 +1,183 @@ +name: Update PHP +# Builds PHP from build/Dockerfile and opens a pull request with the new +# binaries in php-files/. +# +# Runs daily and opens a PR when php.net has a newer release of the PHP +# branch the Dockerfile is on (e.g. 8.3.x). Run it by hand from the Actions +# tab to build a specific version or to rebuild the current one. +# +# PRs opened with the default GITHUB_TOKEN do not trigger the other workflows +# (Jest, e2e). To have them run on the PR, add a fine-grained token with +# contents and pull-requests write access as the PHP_UPDATE_TOKEN secret. +# With the default token, "Allow GitHub Actions to create and approve pull +# requests" must be enabled under Settings > Actions > General. + +on: + schedule: + - cron: '23 7 * * *' + workflow_dispatch: + inputs: + php_version: + description: 'PHP version, e.g. 8.3.37, or 8.4 for the latest 8.4.x. Empty: latest release of the current branch.' + required: false + default: '' + rebuild: + description: 'Build and open a PR even if the version is unchanged (picks up newer Amazon Linux libraries).' + type: boolean + default: false + +permissions: + contents: write + pull-requests: write + +concurrency: + group: update-php + cancel-in-progress: false + +jobs: + update: + name: build and open PR + # The bundled php links libxml2.so.2, which ubuntu-latest (26.04) lacks; + # the Jest run below needs it. + runs-on: ubuntu-24.04 + if: github.event_name == 'workflow_dispatch' || github.repository_owner == 'mitchmac' + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Resolve PHP release + id: php + run: ./build/update-php-version.sh "$REQUESTED" + env: + REQUESTED: ${{ inputs.php_version }} + + - name: Decide whether to build + id: plan + env: + CHANGED: ${{ steps.php.outputs.changed }} + VERSION: ${{ steps.php.outputs.version }} + REBUILD: ${{ inputs.rebuild }} + EVENT: ${{ github.event_name }} + run: | + if [ "$CHANGED" = true ]; then + branch="update-php/$VERSION" + elif [ "$REBUILD" = true ]; then + branch="update-php/$VERSION-rebuild" + else + echo "PHP $VERSION is already in build/Dockerfile; nothing to do." >> "$GITHUB_STEP_SUMMARY" + echo "build=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + + # A scheduled run leaves an existing PR branch alone, so review isn't + # disturbed by a daily rebuild. A manual run replaces it. + if [ "$EVENT" = schedule ] && git ls-remote --exit-code --heads origin "$branch" > /dev/null; then + echo "Branch $branch already exists; not rebuilding." >> "$GITHUB_STEP_SUMMARY" + echo "build=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + + echo "branch=$branch" >> "$GITHUB_OUTPUT" + echo "build=true" >> "$GITHUB_OUTPUT" + + - name: Build PHP + if: steps.plan.outputs.build == 'true' + run: ./build/build.sh + + - name: Check the binary + if: steps.plan.outputs.build == 'true' + id: check + env: + VERSION: ${{ steps.php.outputs.version }} + working-directory: ./php-files + run: | + export LD_LIBRARY_PATH="$PWD/lib" + ./php -c php.ini -v | tee "$RUNNER_TEMP/version.txt" + grep -q "^PHP $VERSION " "$RUNNER_TEMP/version.txt" + ./php -c php.ini -v | grep -q "Zend OPcache" + for ext in curl gd mbstring mysqli openssl pdo_mysql pdo_sqlite sqlite3 zip exif; do + ./php -c php.ini -m | grep -qix "$ext" || { echo "Missing extension: $ext"; exit 1; } + done + { + echo 'version<> "$GITHUB_OUTPUT" + + - uses: actions/setup-node@v4 + if: steps.plan.outputs.build == 'true' + with: + node-version: 22 + cache: 'npm' + cache-dependency-path: 'package-lock.json' + + - name: Run tests against the new binary + if: steps.plan.outputs.build == 'true' + run: | + npm ci + npm run test + npm run test:wordpress + + - name: Bump package version + if: steps.plan.outputs.build == 'true' + id: package + run: | + version=$(node -p "require('./package.json').version.split('.').map((n, i) => i === 2 ? +n + 1 : n).join('.')") + npm pkg set version="$version" + echo "version=$version" >> "$GITHUB_OUTPUT" + + - name: Write PR description + if: steps.plan.outputs.build == 'true' + env: + PREVIOUS: ${{ steps.php.outputs.previous }} + VERSION: ${{ steps.php.outputs.version }} + SHA256: ${{ steps.php.outputs.sha256 }} + PACKAGE_VERSION: ${{ steps.package.outputs.version }} + PHP_V: ${{ steps.check.outputs.version }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + major=${VERSION%%.*} + { + if [ "$PREVIOUS" = "$VERSION" ]; then + echo "Rebuilds PHP $VERSION with the current Amazon Linux 2023 packages." + else + echo "Updates PHP from $PREVIOUS to $VERSION ([changelog](https://www.php.net/ChangeLog-$major.php#$VERSION))." + fi + echo + echo "- \`php-$VERSION.tar.bz2\` SHA-256: \`$SHA256\` (from php.net's release API, checked during the build)" + echo "- Package version: $PACKAGE_VERSION" + echo "- Built and tested by [this workflow run]($RUN_URL): \`npm run test\` and \`npm run test:wordpress\` passed against the new binary." + echo + echo '```' + echo "$PHP_V" + echo '```' + echo + echo 'Changed files:' + echo + echo '```' + git diff --stat + echo '```' + } > "$RUNNER_TEMP/pr-body.md" + + - name: Open pull request + if: steps.plan.outputs.build == 'true' + id: pr + uses: peter-evans/create-pull-request@v7 + with: + token: ${{ secrets.PHP_UPDATE_TOKEN || github.token }} + branch: ${{ steps.plan.outputs.branch }} + add-paths: | + build/Dockerfile + php-files/** + package.json + commit-message: PHP ${{ steps.php.outputs.version }} + title: PHP ${{ steps.php.outputs.version }} + body-path: ${{ runner.temp }}/pr-body.md + delete-branch: true + + - name: Summary + if: steps.pr.outputs.pull-request-url + env: + PR_URL: ${{ steps.pr.outputs.pull-request-url }} + run: | + echo "Pull request: $PR_URL" >> "$GITHUB_STEP_SUMMARY" diff --git a/build/build.sh b/build/build.sh index 963f1fb..57b35f0 100755 --- a/build/build.sh +++ b/build/build.sh @@ -1,28 +1,44 @@ #!/bin/sh +# Build PHP in Amazon Linux 2023 (build/Dockerfile) and copy the binary, the +# opcache extension and the shared libraries it needs into php-files/. -rm ../php-files/php ../php-files/exts/* ../php-files/lib/* +set -eu -docker build -t lambda-php81 . -container=$(docker create lambda-php81) +cd "$(dirname "$0")" -docker -D cp $container:/work/php-81-bin/bin/php ../php-files/php -docker -D cp $container:/work/php-81-bin/lib/php/extensions/no-debug-non-zts-20230831/opcache.so ../php-files/exts/ +image=lambda-php81 -docker -D cp -L $container:/usr/lib64/libsqlite3.so.0.8.6 ../php-files/lib/libsqlite3.so.0 -docker -D cp -L $container:/usr/lib64/libcrypt.so.2 ../php-files/lib/libcrypt.so.2 -docker -D cp -L $container:/usr/lib64/libpng16.so.16 ../php-files/lib/libpng16.so.16 -docker -D cp -L $container:/usr/lib64/libfreetype.so.6 ../php-files/lib/libfreetype.so.6 -docker -D cp -L $container:/usr/lib64/libonig.so.5 ../php-files/lib/libonig.so.5 -docker -D cp -L $container:/usr/lib64/libzip.so.5 ../php-files/lib/libzip.so.5 -docker -D cp -L $container:/usr/lib64/libharfbuzz.so.0 ../php-files/lib/libharfbuzz.so.0 -docker -D cp -L $container:/usr/lib64/libbrotlidec.so.1 ../php-files/lib/libbrotlidec.so.1 -docker -D cp -L $container:/usr/lib64/libgraphite2.so.3 ../php-files/lib/libgraphite2.so.3 -docker -D cp -L $container:/usr/lib64/libbrotlicommon.so.1 ../php-files/lib/libbrotlicommon.so.1 -docker -D cp -L $container:/usr/lib64/libjpeg.so.62 ../php-files/lib/libjpeg.so.62 -docker -D cp -L $container:/usr/lib64/libwebp.so.7 ../php-files/lib/libwebp.so.7 +docker build -t "$image" . +container=$(docker create "$image") +trap 'docker rm "$container" > /dev/null' EXIT -docker rm $container +# The extension directory name carries PHP's API version +# (no-debug-non-zts-20230831 for 8.3), so ask the build for it. +ext_dir=$(docker run --rm "$image" /work/php-81-bin/bin/php-config --extension-dir) + +rm -f ../php-files/php ../php-files/exts/* ../php-files/lib/* +mkdir -p ../php-files/exts ../php-files/lib + +docker cp "$container:/work/php-81-bin/bin/php" ../php-files/php +docker cp "$container:$ext_dir/opcache.so" ../php-files/exts/ + +for lib in \ + libsqlite3.so.0 \ + libcrypt.so.2 \ + libpng16.so.16 \ + libfreetype.so.6 \ + libonig.so.5 \ + libzip.so.5 \ + libharfbuzz.so.0 \ + libbrotlidec.so.1 \ + libgraphite2.so.3 \ + libbrotlicommon.so.1 \ + libjpeg.so.62 \ + libwebp.so.7 +do + docker cp -L "$container:/usr/lib64/$lib" "../php-files/lib/$lib" +done strip ../php-files/php strip ../php-files/lib/*.so.* -strip ../php-files/exts/*.so \ No newline at end of file +strip ../php-files/exts/*.so diff --git a/build/update-php-version.sh b/build/update-php-version.sh new file mode 100755 index 0000000..6929434 --- /dev/null +++ b/build/update-php-version.sh @@ -0,0 +1,72 @@ +#!/bin/bash +# Point build/Dockerfile at a PHP release: sets PHP_VERSION and PHP_SHA256. +# +# ./update-php-version.sh latest release of the branch the Dockerfile is on (e.g. 8.3) +# ./update-php-version.sh 8.3.37 a specific release +# +# The SHA-256 comes from php.net's release API; the Dockerfile verifies the +# downloaded tarball against it, so the build fails if the two disagree. +# +# Prints key=value lines (previous, version, sha256, changed) and appends them +# to $GITHUB_OUTPUT when it is set. + +set -euo pipefail + +cd "$(dirname "$0")" +dockerfile=Dockerfile + +current=$(sed -n 's/^ARG PHP_VERSION=//p' "$dockerfile") +current_sha=$(sed -n 's/^ARG PHP_SHA256=//p' "$dockerfile") +if [ -z "$current" ] || [ -z "$current_sha" ]; then + echo "Could not find ARG PHP_VERSION / ARG PHP_SHA256 in $dockerfile" >&2 + exit 1 +fi + +requested=${1:-} +if [ -z "$requested" ]; then + # "8.3.36" -> "8.3": the API returns the newest release of that branch. + requested=$(echo "$current" | cut -d. -f1,2) +fi +if ! [[ "$requested" =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then + echo "Not a PHP version: $requested" >&2 + exit 1 +fi + +release=$(curl -fsSL --retry 3 "https://www.php.net/releases/index.php?json&version=${requested}") + +version=$(echo "$release" | jq -r '.version // empty') +if [ -z "$version" ]; then + echo "php.net has no release for $requested: $release" >&2 + exit 1 +fi + +sha256=$(echo "$release" | jq -r --arg f "php-${version}.tar.bz2" '.source[]? | select(.filename == $f) | .sha256') +if ! [[ "$sha256" =~ ^[0-9a-f]{64}$ ]]; then + echo "No SHA-256 for php-${version}.tar.bz2 in php.net's release data" >&2 + exit 1 +fi + +current_minor=$(echo "$current" | cut -d. -f1,2) +target_minor=$(echo "$version" | cut -d. -f1,2) +if [ "$current_minor" != "$target_minor" ]; then + echo "Warning: moving from PHP $current_minor to $target_minor; check the configure flags and php.ini" >&2 +fi + +changed=false +if [ "$version" != "$current" ] || [ "$sha256" != "$current_sha" ]; then + changed=true + sed -i \ + -e "s/^ARG PHP_VERSION=.*/ARG PHP_VERSION=${version}/" \ + -e "s/^ARG PHP_SHA256=.*/ARG PHP_SHA256=${sha256}/" \ + "$dockerfile" +fi + +output="previous=${current} +version=${version} +sha256=${sha256} +changed=${changed}" + +echo "$output" +if [ -n "${GITHUB_OUTPUT:-}" ]; then + echo "$output" >> "$GITHUB_OUTPUT" +fi diff --git a/readme.md b/readme.md index 1b7629c..3a71ea6 100644 --- a/readme.md +++ b/readme.md @@ -134,3 +134,15 @@ Run `npm test` for the generic runtime and `npm run test:wordpress` for storage, router, updater, ownership and loader tests. Test `npm pack` in a clean starter using its `SERVERLESSWP_LOCAL` Docker fixtures before publication. Deployments must explicitly include PHP assets: JavaScript tracing alone is insufficient. + +## Updating PHP + +`php-files/` holds a PHP binary built in Amazon Linux 2023 by +`build/Dockerfile`. The **Update PHP** workflow checks php.net daily for a new +release of the PHP branch the Dockerfile is on, builds it, runs the tests and +opens a pull request with the new binaries and a patch version bump. Run it from +the Actions tab to build a specific version (`8.3.37`, or `8.4` for the latest +8.4.x) or to rebuild the current one. + +To do the same locally, run `build/update-php-version.sh [version]` to set the +version and SHA-256 in the Dockerfile, then `build/build.sh` (requires Docker).