From 07b9c6a8fde3e90d7df50c0b1f2e18761d8dca70 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Fri, 28 Aug 2026 21:43:16 +0530 Subject: [PATCH] Use php-darwin caches on macOS --- src/scripts/darwin.sh | 33 ++++++++++++++++++++++++++++++--- src/scripts/unix.sh | 11 +++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index f6fe5ea6..66bde29e 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -174,6 +174,30 @@ get_brewed_php() { fi } +# Function to setup PHP from the cached builds. +setup_cached_versions() { + local cache_log + local cache_status + + cache_log=$(mktemp "${RUNNER_TEMP:-/tmp}/setup-php-php-darwin.XXXXXX") || return 1 + if run_script_source="https://raw.githubusercontent.com/shivammathur/php-darwin/main/scripts/install.sh" \ + run_script_errexit=true run_script "php-darwin" "$version" "${debug:?}" "${ts:?}" \ + >"$cache_log" 2>&1; then + if [ "${PHP_DARWIN_SHOW_LOG:-false}" = true ]; then + cat "$cache_log" >&4 + fi + rm -f "$cache_log" + return 0 + else + cache_status=$? + fi + printf 'setup-php: php-darwin cache installation failed (exit %s); falling back to Homebrew\n' \ + "$cache_status" >&4 + cat "$cache_log" >&4 + rm -f "$cache_log" + return "$cache_status" +} + # Function to setup PHP 5.6 and newer using Homebrew. add_php() { action=$1 @@ -182,6 +206,9 @@ add_php() { php_keg="php@$version$suffix" php_formula="shivammathur/php/$php_keg" if [[ "$existing_version" = "false" || -n "$suffix" || "$action" = "upgrade" ]]; then + if [ "${runner:?}" != "self-hosted" ] && [ "${use_package_cache:-true}" != "false" ] && setup_cached_versions; then + return 0 + fi update_dependencies add_brew_tap "$php_tap" fi @@ -252,13 +279,13 @@ setup_php() { if [[ "$version" =~ ${old_versions:?} ]]; then run_script "php5-darwin" "${version/./}" >/dev/null 2>&1 status="Installed" - elif [ "${existing_version:0:3}" != "$version" ]; then - add_php "install" "$existing_version" >/dev/null 2>&1 + elif [[ "${existing_version:0:3}" != "$version" || -n "$(get_php_formula_suffix)" ]]; then + add_php "install" "$existing_version" 4>&2 >/dev/null 2>&1 status="Installed" elif [[ "${existing_version:0:3}" = "$version" && "${update:?}" = "true" ]]; then brew_php_version="$(brew info --json "php@$version" 2>/dev/null | jq -r '.[].versions.stable')" if [ "$brew_php_version" != "$existing_version" ]; then - add_php "upgrade" "$existing_version" >/dev/null 2>&1 + add_php "upgrade" "$existing_version" 4>&2 >/dev/null 2>&1 status="Upgraded" fi fi diff --git a/src/scripts/unix.sh b/src/scripts/unix.sh index 093fbe26..e93cd877 100644 --- a/src/scripts/unix.sh +++ b/src/scripts/unix.sh @@ -232,8 +232,15 @@ run_script() { repo=$1 shift args=("$@") - get -q -e /tmp/install.sh "$github/$repo/$latest/install.sh" "$jsdeliver/$repo@main/scripts/install.sh" "$setup_php/$repo/install.sh" - bash /tmp/install.sh "${args[@]}" + script_sources=() + [ -z "${run_script_source:-}" ] || script_sources+=("$run_script_source") + script_sources+=("$github/$repo/$latest/install.sh" "$jsdeliver/$repo@main/scripts/install.sh" "$setup_php/$repo/install.sh") + get -q -e /tmp/install.sh "${script_sources[@]}" + if [ "${run_script_errexit:-false}" = true ]; then + bash -e /tmp/install.sh "${args[@]}" + else + bash /tmp/install.sh "${args[@]}" + fi } # Function to install required packages on self-hosted runners.