Skip to content

Commit 947e9c9

Browse files
beninato8anies1212
andauthored
Add cache hit outputs (#364)
* feat: impl cache hit outputs * support flutter cache and pub cache outputs * add cache output example to readme * use cache step directly * update readme --------- Co-authored-by: anies1212 <[email protected]>
1 parent 395322a commit 947e9c9

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,38 @@ dynamic values:
335335
- `:hash:`
336336
- `:sha256:`
337337

338+
### Using cache outputs
339+
340+
> [!NOTE]
341+
> `PUB-CACHE-HIT` and `CACHE-HIT` directly use the `cache-hit` output from `actions/cache@v4`, which is the following:
342+
> - `cache-hit` - A string value to indicate an exact match was found for the key.
343+
> - If there's a cache hit, this will be 'true' or 'false' to indicate if there's an exact match for `key`.
344+
> - If there's a cache miss, this will be an empty string.
345+
346+
Example usage (inspired by [actions/cache@v4](https://github.com/actions/cache/blob/c45d39173a637a28edbd526cb160189cc4e84f5a/README.md#skipping-steps-based-on-cache-hit) and [#346](https://github.com/subosito/flutter-action/pull/346)) to skip `melos bootstrap` if there was a pub cache hit:
347+
348+
```
349+
steps:
350+
- name: Checkout repository
351+
uses: actions/checkout@v4
352+
353+
- name: Set up Flutter
354+
uses: subosito/flutter-action@v2
355+
id: flutter-action
356+
with:
357+
channel: stable
358+
cache: true
359+
360+
- name: Conditionally run melos bootstrap
361+
if: steps.flutter-action.outputs.PUB-CACHE-HIT != 'true'
362+
run: melos bootstrap
363+
364+
- name: Continue with build
365+
run: flutter build apk
366+
```
367+
368+
## Outputs
369+
338370
Use outputs from `flutter-action`:
339371
340372
```yaml
@@ -356,6 +388,8 @@ steps:
356388
echo ARCHITECTURE=${{ steps.flutter-action.outputs.ARCHITECTURE }}
357389
echo PUB-CACHE-PATH=${{ steps.flutter-action.outputs.PUB-CACHE-PATH }}
358390
echo PUB-CACHE-KEY=${{ steps.flutter-action.outputs.PUB-CACHE-KEY }}
391+
echo CACHE-HIT=${{ steps.flutter-action.outputs.CACHE-HIT }}
392+
echo PUB-CACHE-HIT=${{ steps.flutter-action.outputs.PUB-CACHE-HIT }}
359393
```
360394

361395
If you don't need to install Flutter and just want the outputs, you can use the
@@ -379,7 +413,11 @@ steps:
379413
echo ARCHITECTURE=${{ steps.flutter-action.outputs.ARCHITECTURE }}
380414
echo PUB-CACHE-PATH=${{ steps.flutter-action.outputs.PUB-CACHE-PATH }}
381415
echo PUB-CACHE-KEY=${{ steps.flutter-action.outputs.PUB-CACHE-KEY }}
416+
echo CACHE-HIT=${{ steps.flutter-action.outputs.CACHE-HIT }}
417+
echo PUB-CACHE-HIT=${{ steps.flutter-action.outputs.PUB-CACHE-HIT }}
382418
shell: bash
383419
```
384-
[Alif Rachmawadi]: https://github.com/subosito
385-
[Bartek Pacia]: https://github.com/bartekpacia
420+
421+
[Alif Rachmawadi](https://github.com/subosito)
422+
423+
[Bartek Pacia](https://github.com/bartekpacia)

action.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ outputs:
7676
GIT_SOURCE:
7777
value: "${{ steps.flutter-action.outputs.GIT_SOURCE }}"
7878
description: Git source of Flutter SDK repository to clone
79+
CACHE-HIT:
80+
value: "${{ steps.cache-flutter.outputs.cache-hit }}"
81+
description: "`true` if the flutter cache was a hit"
82+
PUB-CACHE-HIT:
83+
value: "${{ steps.cache-pub.outputs.cache-hit }}"
84+
description: "`true` if the pub cache was a hit"
7985

8086
runs:
8187
using: composite
@@ -109,6 +115,7 @@ runs:
109115
${{ inputs.channel }}
110116
111117
- name: Cache Flutter
118+
id: cache-flutter
112119
uses: actions/cache@v4
113120
if: ${{ inputs.cache == 'true' }}
114121
with:
@@ -117,6 +124,7 @@ runs:
117124

118125
- name: Cache pub dependencies
119126
uses: actions/cache@v4
127+
id: cache-pub
120128
if: ${{ inputs.cache == 'true' }}
121129
with:
122130
path: ${{ steps.flutter-action.outputs.PUB-CACHE-PATH }}

0 commit comments

Comments
 (0)