DEV Community

Cover image for Run checks for Flutter projects with GitLab CI
Arthur Denner
Arthur Denner

Posted on • Updated on

Run checks for Flutter projects with GitLab CI

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 merge request will be formatted and all tests are passing.

Let's see how to achieve this using GitLab CI.

  • Create the file .gitlab-ci.yml on the root directory;
  • Define the stage on the file:
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
Enter fullscreen mode Exit fullscreen mode

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 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 - and add it as the CODECOV_TOKEN environment variable on GitLab through Settings > CI/CD > Variables - see documentation.

As the last step, we update the workflow with:

    - ... # same as before
    - flutter analyze
    - flutter test --coverage
    - bash <(curl -s https://codecov.io/bash)
  only:
    - merge_requests
Enter fullscreen mode Exit fullscreen mode

Make sure the CODECOV_TOKEN environment variable is set.

Check out the Codecov documentation for more information.

Check out an example repo here.

Notes

  • The example runs only on merge requests but GitLab CI supports other triggers - see documentation;
  • The example uses the stable channel of Flutter but there are images for other channels and specific versions as well - see list and GitHub;
  • The example uses GitLab CI but the workflow can be achieved using other pipeline services too - TravisCI, CircleCI, etc.
    • Example with GitHub Actions 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.

Top comments (0)