How to Setup CI/CD for Flutter Mobile on Github
In app development, we often encounter repetitive activities like testing and deployment, which can be time-consuming and exhausting. Therefore, we will explore how to leverage CI/CD tools to improve efficiency and quality in the app development process.
What is CI/CD?
CI/CD is an automated process for building, testing, and releasing applications quickly and securely.
CI: Continuous Integration
Every time you write new code, it will be automatically tested to ensure it can be integrated with other code without issues.
CD: Continuous Delivery/Deployment
After the code is tested, the application is automatically prepared for release to users (Delivery) or directly deployed to production (Deployment).
So, how do we implement CI/CD in a mobile app? To get started with CI/CD implementation in our project, the first thing we need is a Github account. As stated in the title, we will use the version control service that is widely adopted by developers, offering many features like the ease of performing continuous integration and development in our projects.
Steps to Implement CI on GitHub:
Create a .github/workflows folder in your current project.
Create a file named ci.yml, and then paste the following YAML script:
name: Continuous Integration Test
on:
push:
branches:
- prod
jobs:
ci-test:
runs-on: ubuntu-latest
steps:
# Step 1: Check out the repository code
- name: Checkout repository
uses: actions/checkout@v1
# Step 2: Set up the Java environment
- name: Set up Java environment
uses: actions/setup-java@v1
with:
java-version: '21.x'
# Step 3: Set up the Flutter environment
- name: Set up Flutter environment
uses: subosito/flutter-action@v1
with:
channel: 'stable'
flutter-version: '3.24.4'
# Step 4: Get Flutter dependencies
- name: Install Flutter dependencies
run: flutter pub get
# Step 5: Run Flutter tests
- name: Run Flutter tests
run: flutter test
Steps to Implement CD on GitHub:
Create a file named cd.yml to github/workflows/ and then paste the following YAML script:
name: CD Build APK
on:
push:
branches:
- main
jobs:
build:
# This job will run on ubuntu virtual machine
runs-on: ubuntu-latest
steps:
# Setup Java environment in order to build the Android app.
- uses: actions/checkout@v1
- uses: actions/setup-java@v1
with:
java-version: "21.x"
# Setup the flutter environment.
- uses: subosito/flutter-action@v1
with:
channel: "stable"
flutter-version: "3.24.4"
# Get flutter dependencies.
- run: flutter pub get
# Build apk.
- run: flutter build apk --debug
- name: Upload debug APK
uses: actions/upload-artifact@v4
with:
name: app-debug.apk
path: build/app/outputs/apk/debug/app-debug.apk