Run checks for Flutter projects with GitLab CI
Tags: flutter, gitlab, workflow, automation
Published: 2020-05-08 14:12:25

---
title: Run checks for Flutter projects with GitLab CI
published: true
description: How to setup build and test checks for Flutter projects
tags: flutter, gitlab, workflow, automation
cover_image: https://res.cloudinary.com/arthurdenner/image/upload/v1594144443/posts/gitlab_ci_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 merge request will be formatted and all tests are passing.

Let's see how to achieve this using [GitLab CI](https://docs.gitlab.com/ee/ci).

- Create the file `.gitlab-ci.yml` on the root directory;
- Define the stage on the file:

```yml
image: cirrusci/flutter:stable

before_script:
  - flutter pub get

stages:
  - analyze-and-test

analyze-and-test:
  stage: analyze-and-test
  script:
    - flutter build aot
    - flutter analyze
    - flutter test
  only:
    - merge_requests
```

And that's it! Now, whenever you open and update a merge request, `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 merge request will get a failed check. ❌

### Bonus - Test Coverage report

Another improvement is to report code coverage on every merge request. 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, we need to generate a token in Codecov after linking our repository - see [documentation](https://docs.codecov.io/docs/about-the-codecov-bash-uploader#upload-token) - and add it as the `CODECOV_TOKEN` environment variable on GitLab through *Settings > CI/CD > Variables* - see [documentation](https://docs.gitlab.com/ee/ci/variables/#create-a-custom-variable-in-the-ui).

As the last step, we update the workflow with:

```yml
    - ... # same as before
    - flutter analyze
    - flutter test --coverage
    - bash <(curl -s https://codecov.io/bash)
  only:
    - merge_requests
```

> Make sure the `CODECOV_TOKEN` environment variable is set.

Check out the [Codecov documentation](https://docs.codecov.io/docs/about-the-codecov-bash-uploader) for more information.

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

### Notes

- The example runs only on merge requests but GitLab CI supports other triggers - see [documentation](https://docs.gitlab.com/ee/ci/triggers);
- The example uses the `stable` channel of Flutter but there are images for other channels and specific versions as well - see [list](https://hub.docker.com/r/cirrusci/flutter/tags) and [GitHub](https://github.com/cirruslabs/docker-images-flutter);
- The example uses GitLab CI but the workflow can be achieved using other pipeline services too - TravisCI, CircleCI, etc.
  - Example with GitHub Actions [here](https://dev.to/arthurdenner/run-checks-for-flutter-projects-with-github-actions-7ld).

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.