CI/CD Flutter with GitHub Actions

Pradyot Prakash
4 min readFeb 10, 2021

Continuous integration (CI) and continuous delivery (CD) embody a culture, set of operating principles, and collection of practices that enable application development teams to deliver code changes more frequently and reliably. The implementation is also known as the CI/CD pipeline.

Continuous integration is a coding philosophy and set of practices that drive development teams to implement small changes and check in code to version control repositories frequently. Because most modern applications require developing code in different platforms and tools, the team needs a mechanism to integrate and validate its changes.

CI/CD will make your life easy after its successfully integrated. Check for rules and test code will be done by someone else not by you or any other developer.

There are many other ways by which CI/CD can be achieved. One is by the official Flutter team, here.

Or, there is CodeMagic, you can check it through.

But personally, GitHub action was easy to learn and easy to implement.

So now, Create a .github folder, inside that workflows folder. Then create a .yml file. For starters name it as main.yml.

File structure

Copy paste the below code,

on:
push:
branches:
- main
name: Code checker
jobs:
build:
name: Code checker
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1
- run: flutter doctor
- run: flutter clean
- run: flutter pub upgrade
- run: flutter pub get
- run: flutter pub run build_runner build
- run: flutter analyze

That’s it.

on:
push:
branches:
- main

This tells, on push in branch main run the below code.

name: Code checker

The name of the work flow which will be showed in the GitHub action, this will help you to understand what kind of work is going on.

build:
name: Code checker
runs-on: ubuntu-latest

This will tell that a code checker workflow will run now.

steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1
- run: flutter doctor
- run: flutter clean
- run: flutter pub upgrade
- run: flutter pub get
- run: flutter pub run build_runner build
- run: flutter analyze

These are the steps which will run for Code checker. These are mainly flutter commands which you would be running in the terminal and checking if the code you wrote was following the lint rules or not.

- run: flutter pub run build_runner build

Remove this line if you are not using build_runner. This will help in generating the .g.dart file. Or if you are pushing the generated the file to the repository then also this command is not required.

After, doing all the changes and then push the code. And go to Actions section in your GitHub repository. You will see that the workflow is running. After about 2–3 minutes you will find it was successful (If everything is correct).

Success Image
Error Image

You might also find sometimes the workflow gets error. So check the error logs you will get the idea what went wrong.

Syntax error in yml file.

Or there might be error in the analyze option or the test command. It throws an error same as flutter so it would be easy to understand.

To push build on firebase for android,

on:
push:
branches:
- main
name: Code checker
jobs:
build:
name: Code checker
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '8.x'
- uses: subosito/flutter-action@v1
- run: flutter doctor
- run: flutter clean
- run: flutter pub upgrade
- run: flutter pub get
- run: flutter pub run build_runner build
- run: flutter analyze
- run: flutter build apk
- uses: actions/upload-artifact@master
with:
name: apk-release
path: build/app/outputs/flutter-apk/app-release.apk
- name: Deploy
uses: wzieba/Firebase-Distribution-Github-Action@v1.2.1
with:
appId: ${{secrets.FIREBASE_ANDROID_APPID}}
token: ${{secrets.FIREBASE_TOKEN}}
groups: <Tester_Group_Name>
releaseNotes: <Relase_Note>
file: build/app/outputs/flutter-apk/app-release.apk
- uses: geekyeggo/delete-artifact@v1
with:
name: apk-release

Copy and paste the above code in the yml file.

FIREBASE_ANDROID_APPID will be the app id you can get it from the Firebase setting page, in your application.

FIREBASE_TOKEN can be taken from the Firebase CLI.

Hope everything is cleared and running. For more details, can check this link, or this one.

Flutter : Love at first build.

--

--