From 844082558df106d66af07532c8942561c84c28d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Tjern=C3=B8?= Date: Mon, 12 Feb 2024 16:13:38 -0800 Subject: [PATCH] Improvements to caching (#265) * 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` --- README.md | 6 +++++- action.yml | 23 +++++++++++++++++++---- setup.sh | 16 +++++++++++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 294b785..ef26ac2 100644 --- a/README.md +++ b/README.md @@ -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:` @@ -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 ``` diff --git a/action.yml b/action.yml index 106034b..299ef2d 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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 diff --git a/setup.sh b/setup.sh index 1dcfccd..08a1e49 100755 --- a/setup.sh +++ b/setup.sh @@ -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)}')" ;; @@ -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") @@ -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 @@ -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 @@ -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 @@ -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"