Run checks for Flutter projects with GitHub Actions

Run checks for Flutter projects with GitHub Actions

Β·

3 min read

On a previous post, I demonstrated how to run checks during a git commit with Lefthook.

That workflow is good, however, it's very easy to bypass the checks with the --no-verify flag.

An improvement to that workflow is to run those checks on a pipeline to make sure that every PR will be formatted and all tests are passing.

Let's see how to achieve this using GitHub Actions.

  • Create a file under the folder .github/workflows, e.g: checks.yml;
  • Define the job on the file:
name: Run analyze and tests
on: pull_request
jobs:
  analyze-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-java@v1
        with:
          java-version: '12.x'
      - uses: subosito/flutter-action@v1
        with:
          channel: 'stable'
      - run: flutter pub get
      - run: flutter build aot
      - run: flutter analyze
      - run: flutter test

And that's it! Now, whenever you open and update a PR, flutter build aot, flutter analyze and flutter test will run after Java and Flutter are installed in the pipeline. If any of the commands fail, the PR will get a failed check. ❌

Bonus - Test Coverage report

Another improvement is to report code coverage on every PR. The example below uses Codecov but there are other services for it too - although the usage will be probably different.

To integrate it, update the workflow with:

      - ... # same as before
      - run: flutter analyze
      - run: flutter test --coverage
      - uses: codecov/codecov-action@v1
        with:
          token: ${{secrets.CODECOV_TOKEN}} # not required for public repos
          file: ./coverage/lcov.info

Check out the codecov-action documentation for more information.

Check out an example repo here.

Notes

  • The example uses the pull_request event, but GitHub Actions support many others - see documentation;
  • The example uses the stable channel of Flutter, but the action supports other channels and specific versions as well - see documentation;
  • The example uses GitHub Actions, but the workflow can be achieved using other pipeline services too - TravisCI, CircleCI, etc.
    • Example with GitLab CI here.

If you're using a different solution or have any suggestions to improve this example, feel free to share it in the comments.


I hope you enjoyed this post and follow me on any platform for more.