diff --git a/README.md b/README.md index f8a6711..b125024 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,23 @@ steps: - run: flutter --version ``` +### Use a custom GitHub token + +By default, the action authenticates GitHub API requests with `github.token`. +You can pass a different token if needed: + +```yaml +steps: + - name: Clone repository + uses: actions/checkout@v6 + - name: Set up Flutter + uses: subosito/flutter-action@v2 + with: + channel: master + token: ${{ secrets.MY_GITHUB_PAT }} + - run: flutter --version +``` + ### Apply a patch Sometimes you find a bug in Flutter and fix it yourself (you're a rockstar!), diff --git a/action.yaml b/action.yaml index 644170e..93090f1 100644 --- a/action.yaml +++ b/action.yaml @@ -54,6 +54,10 @@ inputs: description: Git clone source required: false default: "https://github.com/flutter/flutter.git" + token: + description: "GitHub token for authenticated GitHub API requests to avoid rate limiting" + required: false + default: ${{ github.token }} outputs: CHANNEL: @@ -106,6 +110,8 @@ runs: - name: Set action inputs id: flutter-action shell: bash + env: + GITHUB_TOKEN: ${{ inputs.token }} run: | $GITHUB_ACTION_PATH/setup.sh -p \ -n '${{ inputs.flutter-version }}' \ @@ -137,6 +143,8 @@ runs: - name: Run setup script shell: bash if: ${{ inputs.dry-run != 'true' && inputs.dry-run != true }} + env: + GITHUB_TOKEN: ${{ inputs.token }} run: | $GITHUB_ACTION_PATH/setup.sh \ -n '${{ steps.flutter-action.outputs.VERSION }}' \ diff --git a/setup.sh b/setup.sh index b3c0b7b..d24ace7 100755 --- a/setup.sh +++ b/setup.sh @@ -71,6 +71,7 @@ ARCH="" VERSION="" VERSION_FILE="" GIT_SOURCE="" +TOKEN="${GITHUB_TOKEN:-}" while getopts 'tc:k:d:l:pa:n:f:g:' flag; do case "$flag" in @@ -224,7 +225,13 @@ fi if [ ! -x "$CACHE_PATH/flutter/bin/flutter" ]; then if [ "$CHANNEL" = "master" ] || [ "$CHANNEL" = "main" ]; then - git clone -b "$CHANNEL" "$GIT_SOURCE" "$CACHE_PATH/flutter" + if [ -n "$TOKEN" ]; then + GIT_EXTRAHEADER="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$TOKEN" | base64 | tr -d '\n')" + git -c "http.${GITHUB_SERVER_URL:-https://github.com}/.extraheader=$GIT_EXTRAHEADER" \ + clone -b "$CHANNEL" "$GIT_SOURCE" "$CACHE_PATH/flutter" + else + git clone -b "$CHANNEL" "$GIT_SOURCE" "$CACHE_PATH/flutter" + fi if [ "$VERSION" != "any" ]; then git config --global --add safe.directory "$CACHE_PATH/flutter" (cd "$CACHE_PATH/flutter" && git checkout "$VERSION")