File tree Expand file tree Collapse file tree 6 files changed +97
-3
lines changed Expand file tree Collapse file tree 6 files changed +97
-3
lines changed Original file line number Diff line number Diff line change @@ -218,6 +218,18 @@ jobs:
218
218
dependency-versions : " ${{ matrix.dependency-versions }}"
219
219
custom-cache-key : ' my-super-custom-cache-key'
220
220
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
+
221
233
run-no-cleanup :
222
234
needs : test
223
235
name : " Run unclean"
Original file line number Diff line number Diff line change @@ -148,6 +148,32 @@ wish to update the cache.
148
148
custom-cache-key: "my-custom-cache-key"
149
149
` ` `
150
150
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
+
151
177
# ## Matrix Example
152
178
153
179
GitHub Workflows allow you to set up a [job matrix](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix),
Original file line number Diff line number Diff line change @@ -30,6 +30,10 @@ inputs:
30
30
description : >-
31
31
A custom cache key to use instead of an auto-generated cache key.
32
32
required : false
33
+ custom-cache-suffix :
34
+ description : >-
35
+ A custom suffix to add to the auto-generated cache key.
36
+ required : false
33
37
34
38
runs :
35
39
using : " composite"
66
70
"${{ inputs.composer-options }}" \
67
71
"${{ hashFiles('**/composer.json', '**/composer.lock') }}" \
68
72
"${{ inputs.custom-cache-key }}" \
73
+ "${{ inputs.custom-cache-suffix }}" \
69
74
"${{ inputs.working-directory }}"
70
75
71
76
- name : " Cache Composer dependencies"
Original file line number Diff line number Diff line change @@ -25,7 +25,8 @@ dependency_versions="${3:-locked}"
25
25
composer_options=" ${4} "
26
26
files_hash=" ${5} "
27
27
custom_cache_key=" ${6} "
28
- working_directory=" ${7} "
28
+ custom_cache_suffix=" ${7} "
29
+ working_directory=" ${8} "
29
30
30
31
key=()
31
32
restore_key=()
41
42
if [ -n " ${custom_cache_key} " ]; then
42
43
key+=(" ${custom_cache_key} " )
43
44
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} " )
45
46
46
47
restore_key=(" $( make_key " ${key[@]} " ) -" )
47
48
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ set gitHubOutputFile cache_key_output_06.txt
9
9
set ::env(GITHUB_OUTPUT) $gitHubOutputFile
10
10
11
11
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"
13
13
match_max 100000
14
14
15
15
expect -exact "::debug::Cache primary key is 'Linux-php-8.1.1-composer-locked-path/to/working/dir-long-files-hash'"
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments