Static Analysis of Flutter Code With GitHub Actions and Comment on Pull Request With Danger
Summary
- define the lint rules of Flutter / Dart codes with
pedantic
package - define the workflow of GitHub Actions that offers static analysis of the Flutter / Dart codes
- make the errors of the static analysis to be commented in the pull requests using Danger
Define Lint Rules
Create analysis_options.yaml
Create analysis_options.yaml
in the same directory with pubspec.yaml
. You can define the lint rule of the flutter project in analysis_options.yaml
. This time, I use the pedantic
package to define the lint rule. It says, “It documents how Dart static analysis is used internally at Google, including best practices for the code we write”.
Define Lint Rules using pedantic
package
Edit the pubspec.yaml
and add dependency to pedantic
.
dependencies:
pedantic: ^1.9.0
Then, add this line to the analysis_options.yaml
to enable lint rules from pedantic
package.
include: package:pedantic/analysis_options.yaml
Run flutter analyze
Now, when you run flutter analyze
, warnings are displayed in the console following the lint rules from pedantic
(if there is any lint errors).
Define GitHub Actions Workflow to Static Analysis
Secondly, let’s run the flutter analyze
in a workflow of GitHub Actions. To run commands from Flutter in GitHub Actions' workflow, subosito/flutter-action@v1
is useful.
The workflow to run flutter analyze
in GitHub Actions is here. The yaml file must be placed under .github/workflows/
. Any file name would do.
name: lint check
on:
pull_request:
types: [opened, synchronize]
push:
branches:
- main
jobs:
lint:
name: flutter analyze
runs-on: ubuntu-latest
steps:
- name: set up repository
uses: actions/checkout@v1
- name: set up java
uses: actions/setup-java@v1
with:
java-version: "12.x"
- uses: subosito/flutter-action@v1
with:
channel: "stable"
- name: flutter pub get
run: flutter pub get
- name: flutter analyze
run: flutter analyze
After defined this workflow, static analysis is executed when you make a pull request. If there is any lint warnings, the workflow is failed for now.
Setup for Danger
Create Gemfile
To setup Danger and danger-flutter_lint
, which is a Danger plugin for Flutter, create Gemfile
under the root directory of the Flutter project.
Here uses two gems, danger
and danger-flutter_lint
.
source 'https://rubygems.org'
gem 'danger'
gem 'danger-flutter_lint'
I’ll setup the gems in GitHub Actions' workflow, so it’s enough for now.
Create Dangerfile
Now define the rules for Danger. This time, I’d like lint warnings to be commented only modified files and to be commented inline. So I enable the two rules. Also, I set the path to the output file of flutter analyze
, which would be output in the workflow of GitHub Actions.
flutter_lint.only_modified_files = true
flutter_lint.report_path = "flutter_analyze_report.txt"
flutter_lint.lint(inline_mode: true)
Define GitHub Actions Workflow to Danger
Now we can define the GitHub Actions’s workflow to make lint warnings commented in pull requests. In the step of flutter analyze
, save the result to the path defined in Dangerfile
. If there is any lint error from flutter analyze
, setup and execute Danger. The yaml file would be in .github/workflows/lint.yaml
.
name: lint check
on:
pull_request:
types: [opened, synchronize]
push:
branches:
- main
jobs:
lint:
name: flutter analyze
runs-on: ubuntu-latest
steps:
- name: set up repository
uses: actions/checkout@v1
- name: set up java
uses: actions/setup-java@v1
with:
java-version: "12.x"
- uses: subosito/flutter-action@v1
with:
channel: "stable"
- name: flutter pub get
run: flutter pub get
- name: flutter analyze
run: flutter analyze > flutter_analyze_report.txt
- name: setup ruby
if: ${{ failure() }}
uses: actions/setup-ruby@v1
with:
ruby-version: "2.6"
- name: setup danger
if: ${{ failure() }}
run: |
gem install bundler
bundle install
- name: execute danger
if: ${{ failure() }}
uses: MeilCli/danger-action@v5
with:
plugins_file: "Gemfile"
install_path: "vendor/bundle"
danger_file: "Dangerfile"
danger_id: "danger-pr"
env:
DANGER_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
That’s all. Now there’d be comments in pull requests if there is any static analysis error.
#Flutter #GitHub Actions #Dart
About me 😎

茨城県つくば市在住のソフトウェアエンジニアです。専門領域はAndroidアプリ開発で、特にアーキテクチャに興味があります。某社でAndroidアプリ開発のテックリードをしています。
Jetpack ComposeによるAndroid MVVMアーキテクチャ入門の著者です。
👉 もっと詳しく
Writing 📝
Android MVVMアーキテクチャ入門
Androidアプリ開発の初学者に向けた、MVVM(Model-View-ViewModel)アーキテクチャの入門書を書きました。初学者の方を確実にネクストレベルに引き上げる技術書です。
👉 もっと詳しく