How to deploy flutter package via GitHub Action?

524 Views Asked by At

I am trying to deploy my package in pub.dev via Github Action, while I am able to achieve all the process I am still getting
"Pub needs your authorization to upload packages on your behalf."
How can we avoid that?

Here is my config file where I store my secret key:

# This script creates/updates pub-credentials.json file which is used
# to authorize publisher when publishing packages to pub.dev

# Checking whether the secrets are available as environment
# variables or not.
if [ -z "${PUB_DEV_PUBLISH_ACCESS_TOKEN}" ]; then
  echo "Missing PUB_DEV_PUBLISH_ACCESS_TOKEN environment variable"
  exit 1
fi

if [ -z "${PUB_DEV_PUBLISH_REFRESH_TOKEN}" ]; then
  echo "Missing PUB_DEV_PUBLISH_REFRESH_TOKEN environment variable"
  exit 1
fi

if [ -z "${PUB_DEV_PUBLISH_TOKEN_ENDPOINT}" ]; then
  echo "Missing PUB_DEV_PUBLISH_TOKEN_ENDPOINT environment variable"
  exit 1
fi

if [ -z "${PUB_DEV_PUBLISH_EXPIRATION}" ]; then
  echo "Missing PUB_DEV_PUBLISH_EXPIRATION environment variable"
  exit 1
fi
# ADD THIS LINE TO CREATE THE DIRECTORY
mkdir -p ~/.pub-cache
# Create pub-credentials.json file.
cat <<EOF > ~/.pub-cache/pub-credentials.json
{
  "accessToken":"${PUB_DEV_PUBLISH_ACCESS_TOKEN}",
  "refreshToken":"${PUB_DEV_PUBLISH_REFRESH_TOKEN}",
  "tokenEndpoint":"${PUB_DEV_PUBLISH_TOKEN_ENDPOINT}",
  "scopes":["https://www.googleapis.com/auth/userinfo.email","openid"],
  "expiration":${PUB_DEV_PUBLISH_EXPIRATION}
}
EOF

And here is my yaml file for github action

on:
  release:
    branches:
      - master
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Install Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.3.3'
      - name: Install dependencies
        run: flutter pub get
      - name: Analyze
        run: flutter analyze
      - name: Run tests
        run: flutter test
      - name: Setup Pub Credentials
        shell: bash
        env:
          PUB_DEV_PUBLISH_ACCESS_TOKEN: ${{ secrets.PUB_DEV_PUBLISH_ACCESS_TOKEN }}
          PUB_DEV_PUBLISH_REFRESH_TOKEN: ${{ secrets.PUB_DEV_PUBLISH_REFRESH_TOKEN }}
          PUB_DEV_PUBLISH_TOKEN_ENDPOINT: ${{ secrets.PUB_DEV_PUBLISH_TOKEN_ENDPOINT }}
          PUB_DEV_PUBLISH_EXPIRATION: ${{ secrets.PUB_DEV_PUBLISH_EXPIRATION }}
        run: |
          sh ./pub_login.sh
      - name: Check Publish Warnings
        run: dart pub publish --dry-run
      - name: Publish Package
        run: dart pub publish -f

Here is the Github Action log enter image description here

2

There are 2 best solutions below

0
On

Couldn't find the proper fix to this, so I end up using https://github.com/k-paxian/dart-package-publisher. Check this tutorial for complete example

0
On

Try this:

on:
  release:
    branches:
      - master
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Install Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.3.3'
      - name: Install dependencies
        run: flutter pub get
      - name: Analyze
        run: flutter analyze
      - name: Run tests
        run: flutter test
      - name: Setup credentials
        run: |
          mkdir -p ~/.pub-cache 
          cat <<EOF > ~/.pub-cache/credentials.json
          {
            "accessToken":"${{ secrets.PUB_DEV_PUBLISH_ACCESS_TOKEN }}",
            "refreshToken":"${{ secrets.PUB_DEV_PUBLISH_REFRESH_TOKEN }}",
            "tokenEndpoint":"https://accounts.google.com/o/oauth2/token",
            "scopes": [ "openid", "https://www.googleapis.com/auth/userinfo.email" ],
            "expiration": ${{ secrets.PUB_DEV_PUBLISH_EXPIRATION }}
          }
          EOF
        run: |
          sh ./pub_login.sh
      - name: Check Publish Warnings
        run: dart pub publish --dry-run
      - name: Publish Package
        run: dart pub publish -f