Continuous Integration and Delivery (CI/CD) for Flutter Apps
As you embark on the dynamic journey of Flutter app development, streamlining your workflow becomes paramount. The automation of repetitive tasks, such as testing, building APK files, and deploying to platforms like Firebase Hosting, is where Continuous Integration (CI) and Continuous Deployment (CD) shine. These practices enable the rapid and reliable delivery of code changes, ensuring the efficiency and quality of your Flutter applications.
Before we dive into the practical implementation using GitHub Actions, let's briefly introduce GitHub Actions.
What is GitHub Actions?
GitHub Actions is a CI/CD platform embedded within the GitHub repository. It empowers developers to automate build, test, and deployment processes directly from their repositories.
Understanding the Workflow
This workflow consists of several jobs:
flutter_test: Runs Flutter tests and performs analysis.
build_apk: Generates APK files for Android.
build_appbundle: Generates app bundle files for Android.
build_web: Builds the Flutter app for the web.
deploy: Deploys the web app to Firebase Hosting.
Ensure you replace ${{ secrets.FIREBASE_SERVICE_ACCOUNT}} with the appropriate secret variable in your GitHub repository.
Setting Up a CI/CD Pipeline with GitHub Actions for Flutter
Let's take a step-by-step approach to creating a comprehensive CI/CD pipeline for a Flutter application using GitHub Actions.
1. Project Initialization
Assuming you have Flutter installed, create a new Flutter project and navigate to its directory.
flutter create flutter_ci_cd
cd flutter_ci_cd
2. GitHub Workflow Setup
For GitHub to recognize your workflows, create a .yml file in the .github/workflows folder. The example below illustrates a workflow for building, testing, and deploying a Flutter app.
name: Flutter Build, Release And Deploy
on:
push:
branches:
- master
jobs:
flutter_test:
name: Run flutter test and analyze
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v1
with:
java-version: "18.x"
- uses: subosito/flutter-action@v2
with:
flutter-version: "2.10.4"
channel: "stable"
- run: flutter pub get
- run: flutter analyze
- run: flutter test
build_apk:
name: Generate apk files
needs: [flutter_test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v1
with:
java-version: "11.x"
- uses: subosito/flutter-action@v2
with:
flutter-version: "2.10.4"
channel: "stable"
- name: Install dependencies
run: flutter pub get
- name: Build apk
run: flutter build apk --split-per-abi --release
- uses: actions/upload-artifact@master
with:
name: app-armeabi-v7a-release
path: build/app/outputs/apk/release/app-armeabi-v7a-release.apk
build_appbundle:
name: Generate appbundle files
needs: [flutter_test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v1
with:
java-version: '11.x'
- uses: subosito/flutter-action@v2
with:
flutter-version: '2.10.4'
channel: "stable"
- name: Install dependencies
run: flutter pub get
- name: Build appbundle
run: flutter build appbundle
- uses: actions/upload-artifact@master
with:
name: release-appbundle
path: build/app/outputs/bundle/release/app-release.aab
build_web:
name: Build Flutter (Web)
needs: [flutter_test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v1
with:
java-version: "18.x"
- uses: subosito/flutter-action@v2
with:
channel: "stable"
- run: flutter pub get
- run: flutter config --enable-web
- run: flutter build web
- name: Archive Production Artifact
uses: actions/upload-artifact@master
with:
name: web-build
path: build/web
deploy:
name: Deploy to Firebase Hosting
runs-on: ubuntu-latest
needs: [build_web]
steps:
- uses: actions/checkout@v3
- name: Checkout Repo
uses: actions/checkout@master
- name: Download Artifact
uses: actions/download-artifact@master
with:
name: web-build
path: build/web
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: "${{ secrets.GITHUB_TOKEN }}"
firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT }}"
channelId: live
projectId: projectId
Conclusion
GitHub Actions provides a powerful and versatile platform for automating your Flutter CI/CD pipeline. By integrating these workflows into your repository, you enhance development efficiency and ensure the consistent and reliable delivery of your Flutter applications. Feel free to explore and customize these workflows based on your specific project requirements.
Happy coding!