Format, Static Analyse, and Test a Dart project using GitHub Actions.

Setup GitHub Actions for a Dart project

Ravgeet Dhillon

Ravgeet Dhillon

Updated on Oct 08, 2021 in Automation

⏱ 4 min read

Blog banner for Setup GitHub Actions for a Dart project

When working in a team or even as an individual, humans often break rules. But sometimes breaking rules can result in a poor quality code which over time grows out to be messy. You can take advantage of linting and static analysis to check whether the written code adheres to your code styling rules. This can be automated using GitHub Actions.

In this blog, you’ll see how to set up GitHub Actions workflows for static analyzing your code before merging it with your production codebase.

Contents

Prerequisites

Before getting started, make sure to have a Dart project ready. For this tutorial, you can follow along with:

Basic Analysis Options

Static Analysis helps you to find problems in your code before executing it. It’s a great tool that you can integrate into your development environment to prevent bugs and ensure that your code conforms to styling guidelines especially when you are working in a team.

analysis_options.yaml is a YAML file that you can use to specify the lint rules. Create this file at the root of your project and add the following code to it:

include: package:lint/analysis_options.yaml

analyzer:
  errors:
    avoid_function_literals_in_foreach_calls: ignore
    avoid_print: ignore
    omit_local_variable_types: ignore
    overridden_fields: ignore
    prefer_collection_literals: ignore
    unnecessary_await_in_return: ignore
    unnecessary_this: ignore
    use_setters_to_change_properties: ignore

Setting up GitHub Actions

Next, create a GitHub Actions workflow for doing a static analysis of your project’s source code and even run some tests. This workflow will run on each push and pull request made to the master branch.

Create a ci.yml file in the .github/workflows directory and add the following code to it:

name: CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Repository
        uses: actions/checkout@v2
      
      - name: Set up Dart
        uses: dart-lang/setup-dart@v1

      - name: Install Pub Dependencies
        run: dart pub get

      - name: Verify Formatting
        run: dart format --output=none --set-exit-if-changed .

      - name: Analyze Project Source
        run: dart analyze
      
      - name: Run tests
        run: dart test

In the above workflow, you are using a stable version of Dart. However, if you want to set up a different Dart configuration, you can use sdk input with dart-lang/setup-dart action as in the following code:

- name: Set up Dart
  uses: dart-lang/setup-dart@v1
  with:
    sdk: 2.10.3

# or

- name: Set up Dart
  uses: dart-lang/setup-dart@v1
  with:
    sdk: dev

Results

At this stage, you can push directly to the master branch and check out the execution of your GitHub Action.

Github Actions for Dart
All steps passed successfully

So, you have been able to set up your lint pipeline in less than two minutes. This power of GitHub Actions can help any team to achieve better developer workflow and faster releases.

📫

Loved this post? Join our Newsletter.

We write about React, Vue, Flutter, Strapi, Python and Automation. We don't spam.

Please add a valid email.
By clicking submit button, you agree to our privacy policy.
Thanks for subscribing to our newsletter.
There was some problem while registering your newsletter subscription. Please try again after some time or notify the owners at info@ravsam.in

ABOUT AUTHOR

Ravgeet Dhillon

Ravgeet is a Co-Founder and Developer at RavSam. He helps startups, businesses, open-source organizations with Digital Product Development and Technical Content Writing. He is a fan of Jamstack and likes to work with React, Vue, Flutter, Strapi, Node, Laravel and Python. He loves to play outdoor sports and cycles every day.

TAGGED WITH

Got a project or partnership in mind?

Let's Talk

Contact Us ->