Run checks for Flutter projects with GitHub Actions
Tags: flutter, github, workflow, automation
Published: 2020-04-18 09:32:38

---
title: Run checks for Flutter projects with GitHub Actions
published: true
description: How to setup code and test checks for Flutter projects
tags: flutter, github, workflow, automation
cover_image: https://res.cloudinary.com/arthurdenner/image/upload/v1594144361/posts/github_actions_flutter.png
---

On a [previous post](https://dev.to/arthurdenner/git-hooks-in-flutter-projects-with-lefthook-52n), I demonstrated how to run checks during a `git commit` with [Lefthook](https://github.com/Arkweid/lefthook).

That workflow is good, however, it's very easy to bypass the checks with the [`--no-verify` flag](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---no-verify).

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](https://github.com/features/actions).

- Create a file under the folder `.github/workflows`, e.g: `checks.yml`;
- Define the job on the file:

```yml
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](https://codecov.io/) but there are other services for it too - although the usage will be probably different.

To integrate it, update the workflow with:

```yml
      - ... # 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](https://github.com/codecov/codecov-action) for more information.

Check out an example repo [here](https://github.com/arthurdenner/flutter_checks_github_actions).

### Notes

- The example uses the `pull_request` event, but GitHub Actions support many others - see [documentation](https://help.github.com/en/actions/reference/events-that-trigger-workflows);
- The example uses the `stable` channel of Flutter, but the action supports other channels and specific versions as well - see [documentation](https://github.com/subosito/flutter-action#usage);
- The example uses GitHub Actions, but the workflow can be achieved using other pipeline services too - TravisCI, CircleCI, etc.
  - Example with GitLab CI [here](https://dev.to/arthurdenner/run-checks-for-flutter-projects-with-gitlab-ci-1i42).

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.