From d07cd9875fcb041bbfa63c2c592c4e68a137e1ca Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 30 Sep 2024 12:58:39 +0200 Subject: [PATCH] Close #8: Implement caching (#14) For now we cache only the PHP SDK, the PHP binaries and the development packs. This already greatly improves the setup performance (it might easily safe a minute or two, in case of cache hits). To be able to use the current PHP revision as part of the cache key, we factor out determine-revision.ps1. We create a separate cache for the PHP-SDK since this likely rarely changes, and since the cached variant is apparently much faster than fetching a GH release of the PHP-SDK. Since clients may not want to use the cache, possibly because they have already a lot of other files in their caches, we explicitly require clients to opt-in via the `cache` input parameter. --- README.md | 1 + action.yml | 25 +++++++++++++- determine-revision.ps1 | 34 +++++++++++++++++++ run.ps1 | 74 ++++++++++++++++-------------------------- 4 files changed, 87 insertions(+), 47 deletions(-) create mode 100644 determine-revision.ps1 diff --git a/README.md b/README.md index 9250caf..80bec81 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ for building and testing PHP extensions on Windows. - `ts`: thread-safety (`nts` or `ts`) - `deps`: dependency libraries to install; for now, only [core dependencies](https://windows.php.net/downloads/php-sdk/deps/) are available +- `cache`: whether to cache the PHP SDK, PHP and development pack Note that for PHP versions 7.4 and below, `runs-on: windows-2022` will not work as the correct toolset is not available. For these versions, you should use diff --git a/action.yml b/action.yml index a44ed8e..c24a28a 100644 --- a/action.yml +++ b/action.yml @@ -14,6 +14,11 @@ inputs: description: "List of dependency libraries" required: false default: '@()' + cache: + description: "Whether the PHP-SDK should be cached" + type: boolean + required: false + default: false outputs: toolset: description: "The required toolset version" @@ -27,6 +32,24 @@ outputs: runs: using: "composite" steps: + - name: Determine current PHP revision + id: revision + run: ${{github.action_path}}/determine-revision -version ${{inputs.version}} + shell: powershell + - name: Cache PHP SDK + if: ${{inputs.cache == 'true'}} + uses: actions/cache@v4 + with: + path: php-sdk + key: php-sdk-2.3.0 + - name: Cache PHP + if: ${{inputs.cache == 'true'}} + uses: actions/cache@v4 + with: + path: | + php-bin + php-dev + key: php-${{steps.revision.outputs.version}}-${{inputs.arch}}-${{inputs.ts}} - id: setup - run: ${{github.action_path}}/run -version ${{inputs.version}} -arch ${{inputs.arch}} -ts ${{inputs.ts}} -deps ${{inputs.deps}} + run: ${{github.action_path}}/run -version ${{inputs.version}} -revision ${{steps.revision.outputs.version}} -baseurl ${{steps.revision.outputs.baseurl}} -arch ${{inputs.arch}} -ts ${{inputs.ts}} -deps ${{inputs.deps}} shell: powershell diff --git a/determine-revision.ps1 b/determine-revision.ps1 new file mode 100644 index 0000000..455ec4e --- /dev/null +++ b/determine-revision.ps1 @@ -0,0 +1,34 @@ +param ( + [Parameter(Mandatory)] [String] $version +) + +$ErrorActionPreference = "Stop" + +$baseurl = "https://downloads.php.net/~windows/releases/archives" +$releases = @{ + "7.0" = "7.0.33" + "7.1" = "7.1.33" + "7.2" = "7.2.34" + "7.3" = "7.3.33" + "7.4" = "7.4.33" + "8.0" = "8.0.30" +} +$phpversion = $releases.$version +if (-not $phpversion) { + $baseurl = "https://downloads.php.net/~windows/releases" + $url = "$baseurl/releases.json" + $releases = Invoke-WebRequest $url | ConvertFrom-Json + $phpversion = $releases.$version.version + if (-not $phpversion) { + $baseurl = "https://downloads.php.net/~windows/qa" + $url = "$baseurl/releases.json" + $releases = Invoke-WebRequest $url | ConvertFrom-Json + $phpversion = $releases.$version.version + if (-not $phpversion) { + throw "unknown version" + } + } +} + +Write-Output "version=$phpversion" >> $Env:GITHUB_OUTPUT +Write-Output "baseurl=$baseurl" >> $Env:GITHUB_OUTPUT diff --git a/run.ps1 b/run.ps1 index 6ea6bac..e8ed5e4 100644 --- a/run.ps1 +++ b/run.ps1 @@ -1,5 +1,7 @@ param ( [Parameter(Mandatory)] [String] $version, + [Parameter(Mandatory)] [String] $revision, + [Parameter(Mandatory)] [String] $baseurl, [Parameter(Mandatory)] [String] $arch, [Parameter(Mandatory)] [String] $ts, [Parameter(Mandatory)] [AllowEmptyCollection()] [Array] $deps @@ -45,60 +47,40 @@ if (-not $toolset) { throw "toolset not available" } -Write-Output "Install PHP SDK ..." +if (-not (Test-Path "php-sdk")) { + Write-Output "Install PHP SDK ..." -$temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru -$url = "https://github.com/php/php-sdk-binary-tools/releases/download/php-sdk-2.3.0/php-sdk-binary-tools-php-sdk-2.3.0.zip" -Invoke-WebRequest $url -OutFile $temp -Expand-Archive $temp -DestinationPath "." -Rename-Item "php-sdk-binary-tools-php-sdk-2.3.0" "php-sdk" - -$baseurl = "https://downloads.php.net/~windows/releases/archives" -$releases = @{ - "7.0" = "7.0.33" - "7.1" = "7.1.33" - "7.2" = "7.2.34" - "7.3" = "7.3.33" - "7.4" = "7.4.33" - "8.0" = "8.0.30" -} -$phpversion = $releases.$version -if (-not $phpversion) { - $baseurl = "https://downloads.php.net/~windows/releases" - $url = "$baseurl/releases.json" - $releases = Invoke-WebRequest $url | ConvertFrom-Json - $phpversion = $releases.$version.version - if (-not $phpversion) { - $baseurl = "https://downloads.php.net/~windows/qa" - $url = "$baseurl/releases.json" - $releases = Invoke-WebRequest $url | ConvertFrom-Json - $phpversion = $releases.$version.version - if (-not $phpversion) { - throw "unknown version" - } - } + $temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru + $url = "https://github.com/php/php-sdk-binary-tools/releases/download/php-sdk-2.3.0/php-sdk-binary-tools-php-sdk-2.3.0.zip" + Invoke-WebRequest $url -OutFile $temp + Expand-Archive $temp -DestinationPath "." + Rename-Item "php-sdk-binary-tools-php-sdk-2.3.0" "php-sdk" } $tspart = if ($ts -eq "nts") {"nts-Win32"} else {"Win32"} -Write-Output "Install PHP $phpversion ..." +if (-not (Test-path "php-bin")) { + Write-Output "Install PHP $revision ..." -$temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru -$fname = "php-$phpversion-$tspart-$vs-$arch.zip" -$url = "$baseurl/$fname" -Write-Output "Downloading $url ..." -Invoke-WebRequest $url -OutFile $temp -Expand-Archive $temp "php-bin" + $temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru + $fname = "php-$revision-$tspart-$vs-$arch.zip" + $url = "$baseurl/$fname" + Write-Output "Downloading $url ..." + Invoke-WebRequest $url -OutFile $temp + Expand-Archive $temp "php-bin" +} -Write-Output "Install development pack ..." +if (-not (Test-Path "php-dev")) { + Write-Output "Install development pack ..." -$temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru -$fname = "php-devel-pack-$phpversion-$tspart-$vs-$arch.zip" -$url = "$baseurl/$fname" -Write-Output "Downloading $url ..." -Invoke-WebRequest $url -OutFile $temp -Expand-Archive $temp "." -Rename-Item "php-$phpversion-devel-$vs-$arch" "php-dev" + $temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru + $fname = "php-devel-pack-$revision-$tspart-$vs-$arch.zip" + $url = "$baseurl/$fname" + Write-Output "Downloading $url ..." + Invoke-WebRequest $url -OutFile $temp + Expand-Archive $temp "." + Rename-Item "php-$revision-devel-$vs-$arch" "php-dev" +} if ($deps.Count -gt 0) { $baseurl = "https://downloads.php.net/~windows/php-sdk/deps"