Skip to content

Commit 83af392

Browse files
authored
✨ Add new custom-cache-suffix option (#239)
As discussed in 234. Includes tests. Fixes 234 Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
1 parent 7f9021e commit 83af392

File tree

6 files changed

+97
-3
lines changed

6 files changed

+97
-3
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ jobs:
218218
dependency-versions: "${{ matrix.dependency-versions }}"
219219
custom-cache-key: 'my-super-custom-cache-key'
220220

221+
- name: Clean up between tests
222+
run: |
223+
git clean -ffdx && git reset --hard HEAD
224+
composer clear-cache
225+
226+
- name: "Test: custom cache suffix"
227+
uses: ./
228+
with:
229+
working-directory: "${{ matrix.working-directory }}"
230+
dependency-versions: "${{ matrix.dependency-versions }}"
231+
custom-cache-suffix: $(/bin/date -u --date='last Mon' "+%F")
232+
221233
run-no-cleanup:
222234
needs: test
223235
name: "Run unclean"

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,32 @@ wish to update the cache.
148148
custom-cache-key: "my-custom-cache-key"
149149
```
150150

151+
#### custom-cache-suffix
152+
153+
`ramsey/composer-install` will auto-generate a cache key which is composed of
154+
the following elements:
155+
* The OS image name, like `ubuntu-latest`.
156+
* The exact PHP version, like `8.1.11`.
157+
* The options passed via `composer-options`.
158+
* The dependency version setting as per `dependency-versions`.
159+
* The working directory as per `working-directory`.
160+
* A hash of the `composer.json` and/or `composer.lock` files.
161+
162+
If you don't want to generate your own cache key, but do want to make the cache key
163+
even more specific, you can specify a suffix to be added to the cache key via the
164+
`custom-cache-suffix` parameter.
165+
166+
```yaml
167+
# Adds a suffix to the cache key which is equivalent to the full date-time
168+
# of "last Monday 00:00", which means that the cache will be force refreshed
169+
# via the first workflow which is run every Monday.
170+
- uses: "ramsey/composer-install@v2"
171+
with:
172+
custom-cache-suffix: $(/bin/date -u --date='last Mon' "+%F")
173+
```
174+
175+
:warning: Note: specifying a `custom-cache-key` will take precedence over the `custom-cache-suffix`.
176+
151177
### Matrix Example
152178

153179
GitHub Workflows allow you to set up a [job matrix](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix),

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ inputs:
3030
description: >-
3131
A custom cache key to use instead of an auto-generated cache key.
3232
required: false
33+
custom-cache-suffix:
34+
description: >-
35+
A custom suffix to add to the auto-generated cache key.
36+
required: false
3337

3438
runs:
3539
using: "composite"
@@ -66,6 +70,7 @@ runs:
6670
"${{ inputs.composer-options }}" \
6771
"${{ hashFiles('**/composer.json', '**/composer.lock') }}" \
6872
"${{ inputs.custom-cache-key }}" \
73+
"${{ inputs.custom-cache-suffix }}" \
6974
"${{ inputs.working-directory }}"
7075
7176
- name: "Cache Composer dependencies"

bin/cache_key.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ dependency_versions="${3:-locked}"
2525
composer_options="${4}"
2626
files_hash="${5}"
2727
custom_cache_key="${6}"
28-
working_directory="${7}"
28+
custom_cache_suffix="${7}"
29+
working_directory="${8}"
2930

3031
key=()
3132
restore_key=()
@@ -41,7 +42,7 @@ esac
4142
if [ -n "${custom_cache_key}" ]; then
4243
key+=("${custom_cache_key}")
4344
else
44-
key+=("${runner_os}" "php" "${php_version}" "composer" "${composer_options}" "${dependency_versions}" "${working_directory}")
45+
key+=("${runner_os}" "php" "${php_version}" "composer" "${composer_options}" "${dependency_versions}" "${working_directory}" "${custom_cache_suffix}")
4546

4647
restore_key=("$(make_key "${key[@]}")-")
4748

tests/expect/cache_key_06.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ set gitHubOutputFile cache_key_output_06.txt
99
set ::env(GITHUB_OUTPUT) $gitHubOutputFile
1010

1111
set timeout 3
12-
spawn ../../bin/cache_key.sh "Linux" "8.1.1" "" "" "long-files-hash" "" "path/to/working/dir"
12+
spawn ../../bin/cache_key.sh "Linux" "8.1.1" "" "" "long-files-hash" "" "" "path/to/working/dir"
1313
match_max 100000
1414

1515
expect -exact "::debug::Cache primary key is 'Linux-php-8.1.1-composer-locked-path/to/working/dir-long-files-hash'"

tests/expect/cache_key_08.exp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env -S expect -f
2+
3+
# For testing environment variables written to GITHUB_ENV
4+
set gitHubEnvFile cache_key_08.txt
5+
set ::env(GITHUB_ENV) $gitHubEnvFile
6+
7+
# For testing outputs variables written to GITHUB_OUTPUT
8+
set gitHubOutputFile cache_key_output_08.txt
9+
set ::env(GITHUB_OUTPUT) $gitHubOutputFile
10+
11+
set timeout 3
12+
spawn ../../bin/cache_key.sh "Linux" "8.1.12" "lowest" "--ignore-platform-req=php+" "long-files-hash" "" "suffix"
13+
match_max 100000
14+
15+
expect -exact "::debug::Cache primary key is 'Linux-php-8.1.12-composer---ignore-platform-req=php+-lowest-suffix-long-files-hash'"
16+
expect -exact "::debug::Cache restore keys are 'Linux-php-8.1.12-composer---ignore-platform-req=php+-lowest-suffix-'"
17+
expect eof
18+
19+
# Confirm environment variables.
20+
set fp [open $gitHubEnvFile r]
21+
set fileData [read $fp]
22+
close $fp
23+
24+
set expectedValue "CACHE_RESTORE_KEY<<EOF
25+
Linux-php-8.1.12-composer---ignore-platform-req=php+-lowest-suffix-
26+
EOF
27+
"
28+
29+
if { $expectedValue != $fileData } {
30+
puts "\nExpected environment variable does not match. Received:\n"
31+
puts $fileData
32+
exit 1
33+
}
34+
35+
# Verify output variables have been set correctly.
36+
set fp [open $gitHubOutputFile r]
37+
set fileData [read $fp]
38+
close $fp
39+
40+
set expectedValue "key=Linux-php-8.1.12-composer---ignore-platform-req=php+-lowest-suffix-long-files-hash\n"
41+
42+
if { $expectedValue != $fileData } {
43+
puts "\nExpected output variable does not match. Received:\n"
44+
puts $fileData
45+
exit 1
46+
}
47+
48+
# Clean up
49+
file delete $gitHubEnvFile
50+
file delete $gitHubOutputFile

0 commit comments

Comments
 (0)