From 947e9c91a5355bf3e320e442fd60a6c1cdda4eec Mon Sep 17 00:00:00 2001 From: Nicholas Beninato Date: Tue, 24 Jun 2025 14:41:32 -0700 Subject: [PATCH] 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 --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++-- action.yaml | 8 ++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f0f5650..bdcf109 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,38 @@ dynamic values: - `:hash:` - `: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`: ```yaml @@ -356,6 +388,8 @@ steps: echo ARCHITECTURE=${{ steps.flutter-action.outputs.ARCHITECTURE }} echo PUB-CACHE-PATH=${{ steps.flutter-action.outputs.PUB-CACHE-PATH }} 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 @@ -379,7 +413,11 @@ steps: echo ARCHITECTURE=${{ steps.flutter-action.outputs.ARCHITECTURE }} echo PUB-CACHE-PATH=${{ steps.flutter-action.outputs.PUB-CACHE-PATH }} 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 ``` -[Alif Rachmawadi]: https://github.com/subosito -[Bartek Pacia]: https://github.com/bartekpacia + +[Alif Rachmawadi](https://github.com/subosito) + +[Bartek Pacia](https://github.com/bartekpacia) diff --git a/action.yaml b/action.yaml index c9d477c..7b14150 100644 --- a/action.yaml +++ b/action.yaml @@ -76,6 +76,12 @@ outputs: GIT_SOURCE: value: "${{ steps.flutter-action.outputs.GIT_SOURCE }}" 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: using: composite @@ -109,6 +115,7 @@ runs: ${{ inputs.channel }} - name: Cache Flutter + id: cache-flutter uses: actions/cache@v4 if: ${{ inputs.cache == 'true' }} with: @@ -117,6 +124,7 @@ runs: - name: Cache pub dependencies uses: actions/cache@v4 + id: cache-pub if: ${{ inputs.cache == 'true' }} with: path: ${{ steps.flutter-action.outputs.PUB-CACHE-PATH }}