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 <all.for.me1212sna@gmail.com>
This commit is contained in:
Nicholas Beninato
2025-06-24 14:41:32 -07:00
committed by GitHub
parent 395322a6cd
commit 947e9c91a5
2 changed files with 48 additions and 2 deletions

View File

@@ -335,6 +335,38 @@ dynamic values:
- `:hash:` - `:hash:`
- `:sha256:` - `:sha256:`
### Using cache outputs
> [!NOTE]
> `PUB-CACHE-HIT` and `CACHE-HIT` directly use the `cache-hit` output from `actions/cache@v4`, which is the following:
> - `cache-hit` - A string value to indicate an exact match was found for the key.
> - If there's a cache hit, this will be 'true' or 'false' to indicate if there's an exact match for `key`.
> - If there's a cache miss, this will be an empty string.
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:
```
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Flutter
uses: subosito/flutter-action@v2
id: flutter-action
with:
channel: stable
cache: true
- name: Conditionally run melos bootstrap
if: steps.flutter-action.outputs.PUB-CACHE-HIT != 'true'
run: melos bootstrap
- name: Continue with build
run: flutter build apk
```
## Outputs
Use outputs from `flutter-action`: Use outputs from `flutter-action`:
```yaml ```yaml
@@ -356,6 +388,8 @@ steps:
echo ARCHITECTURE=${{ steps.flutter-action.outputs.ARCHITECTURE }} echo ARCHITECTURE=${{ steps.flutter-action.outputs.ARCHITECTURE }}
echo PUB-CACHE-PATH=${{ steps.flutter-action.outputs.PUB-CACHE-PATH }} echo PUB-CACHE-PATH=${{ steps.flutter-action.outputs.PUB-CACHE-PATH }}
echo PUB-CACHE-KEY=${{ steps.flutter-action.outputs.PUB-CACHE-KEY }} echo PUB-CACHE-KEY=${{ steps.flutter-action.outputs.PUB-CACHE-KEY }}
echo CACHE-HIT=${{ steps.flutter-action.outputs.CACHE-HIT }}
echo PUB-CACHE-HIT=${{ steps.flutter-action.outputs.PUB-CACHE-HIT }}
``` ```
If you don't need to install Flutter and just want the outputs, you can use the If you don't need to install Flutter and just want the outputs, you can use the
@@ -379,7 +413,11 @@ steps:
echo ARCHITECTURE=${{ steps.flutter-action.outputs.ARCHITECTURE }} echo ARCHITECTURE=${{ steps.flutter-action.outputs.ARCHITECTURE }}
echo PUB-CACHE-PATH=${{ steps.flutter-action.outputs.PUB-CACHE-PATH }} echo PUB-CACHE-PATH=${{ steps.flutter-action.outputs.PUB-CACHE-PATH }}
echo PUB-CACHE-KEY=${{ steps.flutter-action.outputs.PUB-CACHE-KEY }} echo PUB-CACHE-KEY=${{ steps.flutter-action.outputs.PUB-CACHE-KEY }}
echo CACHE-HIT=${{ steps.flutter-action.outputs.CACHE-HIT }}
echo PUB-CACHE-HIT=${{ steps.flutter-action.outputs.PUB-CACHE-HIT }}
shell: bash shell: bash
``` ```
[Alif Rachmawadi]: https://github.com/subosito
[Bartek Pacia]: https://github.com/bartekpacia [Alif Rachmawadi](https://github.com/subosito)
[Bartek Pacia](https://github.com/bartekpacia)

View File

@@ -76,6 +76,12 @@ outputs:
GIT_SOURCE: GIT_SOURCE:
value: "${{ steps.flutter-action.outputs.GIT_SOURCE }}" value: "${{ steps.flutter-action.outputs.GIT_SOURCE }}"
description: Git source of Flutter SDK repository to clone description: Git source of Flutter SDK repository to clone
CACHE-HIT:
value: "${{ steps.cache-flutter.outputs.cache-hit }}"
description: "`true` if the flutter cache was a hit"
PUB-CACHE-HIT:
value: "${{ steps.cache-pub.outputs.cache-hit }}"
description: "`true` if the pub cache was a hit"
runs: runs:
using: composite using: composite
@@ -109,6 +115,7 @@ runs:
${{ inputs.channel }} ${{ inputs.channel }}
- name: Cache Flutter - name: Cache Flutter
id: cache-flutter
uses: actions/cache@v4 uses: actions/cache@v4
if: ${{ inputs.cache == 'true' }} if: ${{ inputs.cache == 'true' }}
with: with:
@@ -117,6 +124,7 @@ runs:
- name: Cache pub dependencies - name: Cache pub dependencies
uses: actions/cache@v4 uses: actions/cache@v4
id: cache-pub
if: ${{ inputs.cache == 'true' }} if: ${{ inputs.cache == 'true' }}
with: with:
path: ${{ steps.flutter-action.outputs.PUB-CACHE-PATH }} path: ${{ steps.flutter-action.outputs.PUB-CACHE-PATH }}