From 0ef13f12a53071c2362b0f884ad917692ff8cbc5 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 1 Apr 2024 04:03:05 +0200 Subject: [PATCH] Add `flutter-version-file` (#290) * update README with new info * setup.sh: update arg parsing to add support for `flutter-version-file` as `-f pubspec.yaml` * action.yaml: add flutter-version-file * setup.sh: require `yq` only when -f option is specified * fix tests and add a new one * enable shell options: `errexit` and `nounset` * move falling back to defaults to single place - setup.sh * action.yaml: reduce number of arguments in the last invocation --- .github/workflows/workflow.yaml | 33 +++++++++++-- README.md | 85 ++++++++++++++++++++++++++------- action.yaml | 55 +++++++++++---------- setup.sh | 36 ++++++++++---- test/pubspec.yaml | 5 ++ 5 files changed, 158 insertions(+), 56 deletions(-) create mode 100644 test/pubspec.yaml diff --git a/.github/workflows/workflow.yaml b/.github/workflows/workflow.yaml index 00964ff9..187c4328 100644 --- a/.github/workflows/workflow.yaml +++ b/.github/workflows/workflow.yaml @@ -76,12 +76,35 @@ jobs: - run: flutter --version shell: bash + test_version_file: + runs-on: ${{ matrix.operating-system }} + + strategy: + matrix: + operating-system: [ubuntu-latest] + + steps: + - name: Clone repository + uses: actions/checkout@v4 + - uses: ./ + with: + channel: stable + flutter-version-file: test/pubspec.yaml + - name: Verify Dart version + run: dart --version | grep '2.18.6' + shell: bash + - name: Verify Flutter version + run: flutter --version | grep '3.3.10' + shell: bash + test_print_output: runs-on: macos-latest steps: - name: Clone repository uses: actions/checkout@v4 + - run: ./setup.sh -t -p -f test/pubspec.yaml | grep '3.3.10' + shell: bash - run: ./setup.sh -t -p | grep 'stable' shell: bash - run: ./setup.sh -t -p | grep '3.7.7' @@ -146,15 +169,15 @@ jobs: shell: bash - run: ./setup.sh -t -p -n 0 any | grep 'flutter-macos-beta-0.11.13-x64-58c8489fcdb4e4ef6c010117584c9b23d15221aa' shell: bash - - run: ./setup.sh -t -p | grep '/Users/runner/work/_temp/flutter/stable-3.7.7-x64' + - run: ./setup.sh -t -p | grep '/Users/runner/hostedtoolcache/flutter/stable-3.7.7-x64' shell: bash - - run: ./setup.sh -t -p stable | grep '/Users/runner/work/_temp/flutter/stable-3.7.7-x64' + - run: ./setup.sh -t -p stable | grep '/Users/runner/hostedtoolcache/flutter/stable-3.7.7-x64' shell: bash - - run: ./setup.sh -t -p beta | grep '/Users/runner/work/_temp/flutter/beta-3.9.0-0.1.pre-x64' + - run: ./setup.sh -t -p beta | grep '/Users/runner/hostedtoolcache/flutter/beta-3.9.0-0.1.pre-x64' shell: bash - - run: ./setup.sh -t -p dev | grep '/Users/runner/work/_temp/flutter/dev-2.11.0-0.1.pre-x64' + - run: ./setup.sh -t -p dev | grep '/Users/runner/hostedtoolcache/flutter/dev-2.11.0-0.1.pre-x64' shell: bash - - run: ./setup.sh -t -p master | grep '/Users/runner/work/_temp/flutter/master-any-x64' + - run: ./setup.sh -t -p master | grep '/Users/runner/hostedtoolcache/flutter/master-any-x64' shell: bash - run: ./setup.sh -t -p -k 'custom-:channel:-:version:-:hash:' | grep 'custom-stable-3.7.7-2ad6cd72c040113b47ee9055e722606a490ef0da' shell: bash diff --git a/README.md b/README.md index 3a139243..46c0efcf 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ macOS. The following sections show how to configure this action. -## Flutter version +## Specifying Flutter version -Use specific version and channel: +### Use specific version and channel ```yaml steps: @@ -16,12 +16,52 @@ steps: - name: Set up Flutter uses: subosito/flutter-action@v2 with: + channel: stable flutter-version: 3.19.0 + - run: flutter --version +``` + +### Use version from pubspec.yaml + +This is inspired by [`actions/setup-go`](https://github.com/actions/setup-go). + +```yaml +steps: + - name: Clone repository + uses: actions/checkout@v4 + - name: Set up Flutter + uses: subosito/flutter-action@v2 + with: channel: stable + flutter-version-file: pubspec.yaml # path to pubspec.yaml - run: flutter --version ``` -Use latest release for particular channel: +> [!IMPORTANT] +> +> For `flutter-version-file` to work, you need to have the exact Flutter version +> defined in your pubspec.yaml: +> +> **Good** +> +> ```yaml +> environment: +> sdk: ">=3.3.0 <4.0.0" +> flutter: 3.19.0 +> ``` +> +> **Bad** +> +> ```yaml +> environment: +> sdk: ">=3.3.0 <4.0.0" +> flutter: ">= 3.19.0 <4.0.0" +> ``` + +> [!WARNING] +> +> Using `flutter-version-file` requires [`yq`][https://github.com/mikefarah/yq], +> which is not pre-installed in `windows` images. Install it yourself. ```yaml steps: @@ -34,7 +74,7 @@ steps: - run: flutter --version ``` -Use latest release for particular version and/or channel: +### Use latest release for particular version and/or channel ```yaml steps: @@ -43,12 +83,12 @@ steps: - name: Set up Flutter uses: subosito/flutter-action@v2 with: - flutter-version: 1.22.x channel: dev + flutter-version: 1.22.x - run: flutter --version ``` -Use particular version on any channel: +### Use particular version on any channel ```yaml steps: @@ -57,12 +97,12 @@ steps: - name: Set up Flutter uses: subosito/flutter-action@v2 with: - flutter-version: 3.x channel: any + flutter-version: 3.x - run: flutter --version ``` -Use particular git reference on master channel: +### Use particular git reference on master channel ```yaml steps: @@ -71,8 +111,8 @@ steps: - name: Set up Flutter uses: subosito/flutter-action@v2 with: - flutter-version: 5b12b74 # tag, commit or branch channel: master + flutter-version: 5b12b74 # tag, commit or branch - run: flutter --version ``` @@ -94,7 +134,11 @@ steps: - run: flutter build appbundle ``` -Build for **iOS** (macOS runners only): +### Build for iOS + +> [!NOTE] +> +> Building for iOS requires a macOS runner. ```yaml jobs: @@ -112,7 +156,7 @@ jobs: - run: flutter build ios --release --no-codesign ``` -Build for the **web**: +### Build for the web ```yaml steps: @@ -121,13 +165,13 @@ steps: - name: Set up Flutter uses: subosito/flutter-action@v2 with: - channel: "stable" + channel: stable - run: flutter pub get - run: flutter test - run: flutter build web ``` -Build for **Windows**: +### Build for Windows ```yaml jobs: @@ -143,7 +187,7 @@ jobs: - run: flutter build windows ``` -Build for **Linux** desktop: +### Build for Linux desktop ```yaml jobs: @@ -162,7 +206,11 @@ jobs: - run: flutter build linux ``` -Build for **macOS** desktop: +### Build for macOS desktop + +> [!NOTE] +> +> Building for macOS requires a macOS runner. ```yaml jobs: @@ -174,7 +222,7 @@ jobs: - name: Set up Flutter uses: subosito/flutter-action@v2 with: - channel: "stable" + channel: stable - run: flutter build macos ``` @@ -220,7 +268,9 @@ steps: id: flutter-action with: channel: stable - - run: | + - name: Print outputs + shell: bash + run: | echo CACHE-PATH=${{ steps.flutter-action.outputs.CACHE-PATH }} echo CACHE-KEY=${{ steps.flutter-action.outputs.CACHE-KEY }} echo CHANNEL=${{ steps.flutter-action.outputs.CHANNEL }} @@ -228,5 +278,4 @@ 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 }} - shell: bash ``` diff --git a/action.yaml b/action.yaml index 36cd8edd..6594f64f 100644 --- a/action.yaml +++ b/action.yaml @@ -6,14 +6,22 @@ branding: color: blue inputs: - flutter-version: - description: The Flutter version to make available on the path - required: false - default: any channel: description: The Flutter build release channel required: false default: stable + flutter-version: + description: The Flutter version to make available on the path + required: false + default: "" + flutter-version-file: + description: The pubspec.yaml file with exact Flutter version defined + required: false + default: "" + architecture: + description: The architecture of Flutter SDK executable (x64 or arm64) + required: false + default: "${{ runner.arch }}" cache: description: Cache the Flutter SDK required: false @@ -21,31 +29,21 @@ inputs: 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:" + default: "" cache-path: description: Flutter SDK cache path required: false - default: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:" + default: "" + pub-cache-key: + description: Identifier for the Dart .pub-cache cache + required: false + default: "" pub-cache-path: description: Flutter pub cache path required: false default: default - architecture: - description: The architecture of Flutter SDK executable (x64 or arm64) - required: false - default: "${{ runner.arch }}" outputs: - CACHE-KEY: - value: "${{ steps.flutter-action.outputs.CACHE-KEY }}" - description: Key used to cache the Flutter SDK - CACHE-PATH: - value: "${{ steps.flutter-action.outputs.CACHE-PATH }}" - description: Path to Flutter SDK CHANNEL: value: "${{ steps.flutter-action.outputs.CHANNEL }}" description: The selected Flutter release channel @@ -55,6 +53,12 @@ outputs: ARCHITECTURE: value: "${{ steps.flutter-action.outputs.ARCHITECTURE }}" description: The selected Flutter CPU architecture + CACHE-KEY: + value: "${{ steps.flutter-action.outputs.CACHE-KEY }}" + description: Key used to cache the Flutter SDK + CACHE-PATH: + value: "${{ steps.flutter-action.outputs.CACHE-PATH }}" + description: Path to Flutter SDK PUB-CACHE-KEY: value: "${{ steps.flutter-action.outputs.PUB-CACHE-KEY }}" description: Key used to cache the pub dependencies @@ -74,12 +78,13 @@ runs: shell: bash run: | $GITHUB_ACTION_PATH/setup.sh -p \ - -c '${{ inputs.cache-path }}' \ - -k '${{ inputs.cache-key }}' \ - -d '${{ inputs.pub-cache-path }}' \ - -l '${{ inputs.pub-cache-key }}' \ -n '${{ inputs.flutter-version }}' \ + -f '${{ inputs.flutter-version-file }}' \ -a '${{ inputs.architecture }}' \ + -k '${{ inputs.cache-key }}' \ + -c '${{ inputs.cache-path }}' \ + -l '${{ inputs.pub-cache-key }}' \ + -d '${{ inputs.pub-cache-path }}' \ ${{ inputs.channel }} - name: Cache Flutter @@ -105,7 +110,7 @@ runs: shell: bash 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 }}' \ + -c '${{ steps.flutter-action.outputs.CACHE-PATH }}' \ ${{ steps.flutter-action.outputs.CHANNEL }} diff --git a/setup.sh b/setup.sh index b411a2b0..9cce2cc2 100755 --- a/setup.sh +++ b/setup.sh @@ -1,11 +1,12 @@ #!/bin/bash +set -eu check_command() { command -v "$1" >/dev/null 2>&1 } if ! check_command jq; then - echo "jq not found, please install it, https://stedolan.github.io/jq/download/" + echo "jq not found. Install it from https://stedolan.github.io/jq" exit 1 fi @@ -76,8 +77,9 @@ PRINT_ONLY="" TEST_MODE=false ARCH="" VERSION="" +VERSION_FILE="" -while getopts 'tc:k:d:l:pa:n:' flag; do +while getopts 'tc:k:d:l:pa:n:f:' flag; do case "$flag" in c) CACHE_PATH="$OPTARG" ;; k) CACHE_KEY="$OPTARG" ;; @@ -87,19 +89,35 @@ while getopts 'tc:k:d:l:pa:n:' flag; do t) TEST_MODE=true ;; a) ARCH="$(echo "$OPTARG" | awk '{print tolower($0)}')" ;; n) VERSION="$OPTARG" ;; + f) + VERSION_FILE="$OPTARG" + if [ -n "$VERSION_FILE" ] && ! check_command yq; then + echo "yq not found. Install it from https://mikefarah.gitbook.io/yq" + exit 1 + fi + ;; ?) exit 2 ;; esac done [ -z "$ARCH" ] && ARCH="$ARCH_NAME" +if [ -n "$VERSION_FILE" ]; then + if [ -n "$VERSION" ]; then + echo "Cannot specify both a version and a version file" + exit 1 + fi + + VERSION="$(yq '.environment.flutter' "$VERSION_FILE")" +fi + ARR_CHANNEL=("${@:$OPTIND:1}") -CHANNEL="${ARR_CHANNEL[0]}" +CHANNEL="${ARR_CHANNEL[0]:-}" [ -z "$CHANNEL" ] && CHANNEL=stable [ -z "$VERSION" ] && VERSION=any [ -z "$ARCH" ] && ARCH=x64 -[ -z "$CACHE_PATH" ] && CACHE_PATH="$RUNNER_TEMP/flutter/:channel:-:version:-:arch:" +[ -z "$CACHE_PATH" ] && CACHE_PATH="$RUNNER_TOOL_CACHE/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:" [ -z "$PUB_CACHE_PATH" ] && PUB_CACHE_PATH="default" @@ -110,7 +128,7 @@ CHANNEL="${ARR_CHANNEL[0]}" # If `PUB_CACHE` is set already, then it should continue to be used. Otherwise, satisfy it # if the action requests a custom path, or set to the Dart default values depending # on the operating system. -if [ -z "$PUB_CACHE" ]; then +if [ -z "${PUB_CACHE:-}" ]; then if [ "$PUB_CACHE_PATH" != "default" ]; then PUB_CACHE="$PUB_CACHE_PATH" elif [ "$OS_NAME" = "windows" ]; then @@ -170,6 +188,7 @@ if [ "$PRINT_ONLY" = true ]; then if [ "$TEST_MODE" = true ]; then echo "CHANNEL=$info_channel" echo "VERSION=$info_version" + # VERSION_FILE is not printed, because it is essentially same as VERSION echo "ARCHITECTURE=$info_architecture" echo "CACHE-KEY=$CACHE_KEY" echo "CACHE-PATH=$CACHE_PATH" @@ -181,12 +200,13 @@ if [ "$PRINT_ONLY" = true ]; then { echo "CHANNEL=$info_channel" echo "VERSION=$info_version" + # VERSION_FILE is not printed, because it is essentially same as VERSION 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" + } >>"${GITHUB_OUTPUT:-/dev/null}" exit 0 fi @@ -207,10 +227,10 @@ fi { echo "FLUTTER_ROOT=$CACHE_PATH" echo "PUB_CACHE=$PUB_CACHE" -} >>"$GITHUB_ENV" +} >>"${GITHUB_ENV:-/dev/null}" { echo "$CACHE_PATH/bin" echo "$CACHE_PATH/bin/cache/dart-sdk/bin" echo "$PUB_CACHE/bin" -} >>"$GITHUB_PATH" +} >>"${GITHUB_PATH:-/dev/null}" diff --git a/test/pubspec.yaml b/test/pubspec.yaml new file mode 100644 index 00000000..b4d58fad --- /dev/null +++ b/test/pubspec.yaml @@ -0,0 +1,5 @@ +name: flutter_action_test + +environment: + dart: ">=2.18.0 <3.0.0" + flutter: 3.3.10