Skip to content

Commit

Permalink
Improvements to caching (#265)
Browse files Browse the repository at this point in the history
* Bump actions/cache to v4 to suppress node warnings

* Respect `PUB_CACHE` from the environment

* Cache pub-cache separately from flutter

In order to reduce the extra work that happens when you update your
pubspec.lock, don't use `hashFiles('**/pubspec.lock')` in the hash of
the flutter installation, and separately cache the `.pubspec-cache`.

Also return the `.pub-cache` to `$HOME`, since it's not part of the tool
installation.

* Fix README example of pub-cache-path and document `PUB_CACHE`
  • Loading branch information
jorgenpt committed Feb 13, 2024
1 parent 14bf530 commit 8440825
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,13 @@ steps:
cache: true
cache-key: 'flutter-:os:-:channel:-:version:-:arch:-:hash:' # optional, change this to force refresh cache
cache-path: '${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:' # optional, change this to specify the cache path
pub-cache-key: 'flutter-pub:os:-:channel:-:version:-:arch:-:hash:' # optional, change this to force refresh cache of dart pub get dependencies
pub-cache-path: '${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:' # optional, change this to specify the cache path
architecture: x64 # optional, x64 or arm64
- run: flutter --version
```

Note: `cache-key` and `cache-path` has support for several dynamic values:
Note: `cache-key`, `pub-cache-key`, and `cache-path` has support for several dynamic values:

- `:os:`
- `:channel:`
Expand All @@ -199,5 +201,7 @@ steps:
echo CHANNEL=${{ steps.flutter-action.outputs.CHANNEL }}
echo VERSION=${{ steps.flutter-action.outputs.VERSION }}
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 }}
shell: bash
```
23 changes: 19 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ inputs:
cache:
description: 'Cache the Flutter SDK'
required: false
default: false
default: 'false'
cache-key:
description: 'Identifier for the Flutter SDK cache'
required: false
default: 'flutter-:os:-:channel:-:version:-:arch:-:hash:'
pub-cache-key:
description: 'Identifier for the Dart .pub-cache cache'
required: false
default: 'flutter-pub:os:-:channel:-:version:-:arch:-:hash:'
cache-path:
description: 'Flutter SDK cache path'
required: false
Expand All @@ -40,21 +44,32 @@ outputs:
value: '${{ steps.flutter-action.outputs.VERSION }}'
ARCHITECTURE:
value: '${{ steps.flutter-action.outputs.ARCHITECTURE }}'
PUB-CACHE-KEY:
value: '${{ steps.flutter-action.outputs.PUB-CACHE-KEY }}'
PUB-CACHE-PATH:
value: '${{ steps.flutter-action.outputs.PUB-CACHE-PATH }}'
runs:
using: 'composite'
steps:
- run: chmod +x $GITHUB_ACTION_PATH/setup.sh
shell: bash
- id: flutter-action
run: $GITHUB_ACTION_PATH/setup.sh -p -c '${{ inputs.cache-path }}' -k '${{ inputs.cache-key }}' -n '${{ inputs.flutter-version }}' -a '${{ inputs.architecture }}' ${{ inputs.channel }}
run: $GITHUB_ACTION_PATH/setup.sh -p -c '${{ inputs.cache-path }}' -k '${{ inputs.cache-key }}' -d '${{ inputs.pub-cache-key }}' -n '${{ inputs.flutter-version }}' -a '${{ inputs.architecture }}' ${{ inputs.channel }}
shell: bash
- if: ${{ inputs.cache == 'true' }}
uses: actions/cache@v4
with:
path: ${{ steps.flutter-action.outputs.CACHE-PATH }}
key: ${{ steps.flutter-action.outputs.CACHE-KEY }}-${{ hashFiles('**/pubspec.lock') }}
key: ${{ steps.flutter-action.outputs.CACHE-KEY }}
restore-keys: |
${{ steps.flutter-action.outputs.CACHE-KEY }}-${{ hashFiles('**/pubspec.lock') }}
${{ steps.flutter-action.outputs.CACHE-KEY }}
- if: ${{ inputs.cache == 'true' }}
uses: actions/cache@v4
with:
path: ${{ steps.flutter-action.outputs.PUB-CACHE-PATH }}
key: ${{ steps.flutter-action.outputs.PUB-CACHE-KEY }}-${{ hashFiles('**/pubspec.lock') }}
restore-keys: |
${{ steps.flutter-action.outputs.PUB-CACHE-KEY }}-${{ hashFiles('**/pubspec.lock') }}
${{ steps.flutter-action.outputs.PUB-CACHE-KEY }}
- run: $GITHUB_ACTION_PATH/setup.sh -c '${{ steps.flutter-action.outputs.CACHE-PATH }}' -n '${{ steps.flutter-action.outputs.VERSION }}' -a '${{ steps.flutter-action.outputs.ARCHITECTURE }}' ${{ steps.flutter-action.outputs.CHANNEL }}
shell: bash
16 changes: 13 additions & 3 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ download_archive() {

CACHE_PATH=""
CACHE_KEY=""
PUB_CACHE_KEY=""
PRINT_ONLY=""
TEST_MODE=false
ARCH=""
VERSION=""

while getopts 'tc:k:pa:n:' flag; do
while getopts 'tc:k:d:pa:n:' flag; do
case "$flag" in
c) CACHE_PATH="$OPTARG" ;;
k) CACHE_KEY="$OPTARG" ;;
d) PUB_CACHE_KEY="$OPTARG" ;;
p) PRINT_ONLY=true ;;
t) TEST_MODE=true ;;
a) ARCH="$(echo "$OPTARG" | awk '{print tolower($0)}')" ;;
Expand All @@ -94,6 +96,9 @@ CHANNEL="${ARR_CHANNEL[0]}"
[[ -z $ARCH ]] && ARCH=x64
[[ -z $CACHE_PATH ]] && CACHE_PATH="$RUNNER_TEMP/flutter/:channel:-:version:-:arch:"
[[ -z $CACHE_KEY ]] && CACHE_KEY="flutter-:os:-:channel:-:version:-:arch:-:hash:"
[[ -z $PUB_CACHE_KEY ]] && PUB_CACHE_KEY="flutter-pub-:os:-:channel:-:version:-:arch:-:hash:"
# Here we specifically use `PUB_CACHE` (and not `PUB_CACHE_PATH`), because `PUB_CACHE` is what dart (and flutter) looks for in the environment
[[ -z $PUB_CACHE ]] && PUB_CACHE="$HOME/.pub-cache"

if [[ "$TEST_MODE" == true ]]; then
RELEASE_MANIFEST=$(cat "$(dirname -- "${BASH_SOURCE[0]}")/test/$MANIFEST_JSON_PATH")
Expand Down Expand Up @@ -130,6 +135,7 @@ expand_key() {
}

CACHE_KEY=$(expand_key "$CACHE_KEY")
PUB_CACHE_KEY=$(expand_key "$PUB_CACHE_KEY")
CACHE_PATH=$(expand_key "$(transform_path "$CACHE_PATH")")

if [[ "$PRINT_ONLY" == true ]]; then
Expand All @@ -145,6 +151,8 @@ if [[ "$PRINT_ONLY" == true ]]; then
echo "ARCHITECTURE=$info_architecture"
echo "CACHE-KEY=$CACHE_KEY"
echo "CACHE-PATH=$CACHE_PATH"
echo "PUB-CACHE-KEY=$PUB_CACHE_KEY"
echo "PUB-CACHE-PATH=$PUB_CACHE"
exit 0
fi

Expand All @@ -154,6 +162,8 @@ if [[ "$PRINT_ONLY" == true ]]; then
echo "ARCHITECTURE=$info_architecture"
echo "CACHE-KEY=$CACHE_KEY"
echo "CACHE-PATH=$CACHE_PATH"
echo "PUB-CACHE-KEY=$PUB_CACHE_KEY"
echo "PUB-CACHE-PATH=$PUB_CACHE"
} >>"$GITHUB_OUTPUT"

exit 0
Expand All @@ -174,11 +184,11 @@ fi

{
echo "FLUTTER_ROOT=$CACHE_PATH"
echo "PUB_CACHE=$CACHE_PATH/.pub-cache"
echo "PUB_CACHE=$PUB_CACHE"
} >>"$GITHUB_ENV"

{
echo "$CACHE_PATH/bin"
echo "$CACHE_PATH/bin/cache/dart-sdk/bin"
echo "$CACHE_PATH/.pub-cache/bin"
echo "$PUB_CACHE/bin"
} >>"$GITHUB_PATH"

0 comments on commit 8440825

Please sign in to comment.