Deploying Flutter Apps with GitHub Actions: A Step-by-Step Guide
Introduction to GitHub Actions
In today’s fast-paced software development landscape, companies are releasing software and solutions at an unprecedented rate. To keep up with this pace, developers need to adopt efficient and reliable methods for deploying their applications. One such method is using Continuous Integration (CI) and Continuous Delivery (CD) pipelines.
GitHub Actions is a CI/CD tool that allows developers to automate their build, test, and deployment processes directly from their repository. It uses YAML files to define workflows, which can be triggered by various events, such as code pushes or pull requests.
Setting up a Flutter Project with GitHub Actions
To get started, create a new Flutter project using your preferred IDE or the Flutter command-line tool. Initialize a Git repository in your project directory and link it to your GitHub account. Create a new directory called .github
in the root of your project, followed by a subdirectory called workflows
. This is where you will store your CI/CD workflows.
Creating a Basic Android Workflow
Create a new YAML file called android-release.yml
in the workflows directory. This file will define a basic Android workflow that builds and deploys your Flutter app. The workflow consists of several jobs, including building the app, creating a version number, signing the app, and deploying it to the Google Play Store.
name: Android Release
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Flutter
run: |
flutter channel stable
flutter upgrade
- name: Build app
run: flutter build apk --release
- name: Create version number
run: echo "version=1.0.0" > version.properties
- name: Sign app
uses: r0adkll/sign-android-release@v1
- name: Deploy to Google Play Store
uses: upload-google-play@v1
Optimizing the Workflow
To optimize the workflow, you can cache the Java and Flutter SDKs to reduce the time spent on setup. You can also use artifacts to share files between jobs, reducing the need for redundant work.
steps:
- name: Cache Java and Flutter SDKs
uses: actions/cache@v2
key: $GITHUB_SHA-java-flutter
path: |
~/.gradle/caches
~/.pub-cache
- name: Share files between jobs
uses: actions/upload-artifact@v2
name: android-app
path: build/app/outputs/release/app-release.apk
Deploying to the Google Play Store
To deploy your app to the Google Play Store, you need to create a service account and invite it to your Google Play Console. You then need to update the permissions of the service account to allow it to release apps. Finally, you can use the upload-google.play@v1
workflow to deploy your app to the Google Play Store.
steps:
- name: Deploy to Google Play Store
uses: upload-google-play@v1
args:
service_account_json: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT }}
package_name: com.example.app
default_track: production
release_status: completed
Deploying to GitHub Pages
To deploy your app to GitHub Pages, you can create a new workflow called web-release.yml
. This workflow uses the Flutter web build command and deploys the build to GitHub Pages using the peaceiris/actions-gh-pages@v3
workflow.
name: Web Release
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Flutter
run: |
flutter channel stable
flutter upgrade
- name: Build web app
run: flutter build web --release
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: build/web
- Create a new service account in the Google Cloud Console.
- Invite the service account to your Google Play Console.
- Update the permissions of the service account to allow it to release apps.
Learn more about the upload-google-play workflow. Learn more about the peaceiris/actions-gh-pages workflow.