Skip to content

Instantly share code, notes, and snippets.

@yarabramasta
Last active November 21, 2023 13:34
Show Gist options
  • Save yarabramasta/aaef958d688672396c77b8b15b9160a2 to your computer and use it in GitHub Desktop.
Save yarabramasta/aaef958d688672396c77b8b15b9160a2 to your computer and use it in GitHub Desktop.
Flutter Production Github Action

Flutter Production

A guide to prepare release configuration for flutter.

Covering how to make signed app to publish on PlayStore / using Google related API. It's also cover how to setup secure release automation for Github Action.

Step 1 Generate Keystores

For development / debug

keytool -genkey -v -keystore ~/debug.keystore -storepass android -keypass android -alias androiddebugkey -dname "CN=Android Debug,O=Android,C=US"
keytool -genkey -v -keystore debug-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias androiddebugkey -storetype JKS

For production / release

keytool -genkey -v -keystore ~/release.keystore -alias androidreleasekey
keytool -genkey -v -keystore release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias androidreleasekey -storetype JKS

Step 2 Base64 encode & decode

Encode

# encode .jks file
openssl base64 -in key.jks

Decode

# decode base64 file (windows)
type base64.txt | openssl base64 -d > output.jks
# decode base64 file (UNIX)
base64 -d <<< base64.txt > output.jks

Step 3 Gradle Configuration

Read Keystore Files

// apply from...
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

Signing Configs Separation

// defaultConfig{}...
signingConfigs {
    debug {
        storeFile file('debug-key.jks')
        storePassword 'android'
        keyAlias 'androiddebugkey'
        keyPassword 'android'
    }
    release {
        storeFile file('release-key.jks'
        storePassword "$System.env.KEY_PASSWORD"
        keyAlias 'androidreleasekey'
        keyPassword "$System.env.ALIAS_PASSWORD"
    }
}

Step 4 Automation (Optional)

Github action for flutter release.

  • Build apk and appbundle package
  • Upload artifacts of apk and appbundle
  • Push to "release" branch

Path: /.github/workflows/release.yml

name: ✔ Flutter Release
on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  build_android_mobile:
    name: 🛠 Build Flutter (Android)
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v2
        with:
          distribution: "zulu"
          java-version: "11"
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: "2.8.0"
      - run: flutter clean
      - run: flutter pub get
      - name: 🔧 Build APK
        env:
          KEY_JKS: ${{ secrets.KEY_JKS }}
          KEY_PASSWORD: ${{ secrets.ALIAS_PASSWORD }}
          ALIAS_PASSWORD: ${{ secrets.KEY_PASSWORD }}
         run: base64 -d <<< $KEY_JKS > ./android/app/release-key.jks && flutter build apk --release -v
      - name: 🔧 Build AppBundle
        env:
          KEY_JKS: ${{ secrets.KEY_JKS }}
          KEY_PASSWORD: ${{ secrets.ALIAS_PASSWORD }}
          ALIAS_PASSWORD: ${{ secrets.KEY_PASSWORD }}
        run: base64 -d <<< $KEY_JKS > ./android/app/release-key.jks && flutter build appbundle --release -v
      - name: 📦 Upload apk artifact
        uses: actions/upload-artifact@v2
        with:
          name: apk
          path: build/app/outputs/apk/release/app-release.apk
      - name: 📦 Upload appbundle artifact
        uses: actions/upload-artifact@v2
        with:
          name: appbundle
          path: build/app/outputs/bundle/release/app-release.aab
      - name: 🚛 Upload to Release Branch
        uses: s0/git-publish-subdir-action@develop
        env:
          REPO: self
          BRANCH: release
          FOLDER: build/app/outputs/apk/release
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          MESSAGE: "Build: ({sha}) {msg}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment