Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

flutter
travisci
community creator

Travis CI for Flutter apps

Amey Sunu

What is CI and why do we need it?

As a Flutter developer, you would obviously make an app on your local environment and make it as fully functional as possible on your environment, but what about other environments? We don’t know.

Okay, so imagine, we upload our Flutter app to GitHub and then clone the same repo on a different machine and run our app there. There is a chance that your app might not run/come up with multiple breakages and weak links, especially with Flutter, a cross-platform development framework. So, how do we make our apps run perfectly in all sorts of environments? That’s where CI/CD comes in.

What is CI/CD?

CI/CDContinous Integration, Continous Deployment) is used to bridge the gap between development and operation using automation to build, test, and deploy applications. In other words, it makes our lives easier through simpler workflows. We can use tons of tools to perform a simple CI/CD that tests your app in a completely different environment and informs you of where overflows and memory leaks persist.

Some of the best CI/CD tools are:

  • Jenkins
  • Circle CI
  • Azure CI/CD
  • Travis CI
  • GitLab
  • Codemagic
  • Team City by Jet Brains

There are very many more tools out there, but the above are some of my favorites. Personally, for Flutter, I prefer Codemagic.

What is Travis CI?

Travis CI is a simple CI/CD tool used to build apps hosted on GitHub and Bitbucket (just like the other CI/CD tools).

Getting started

  • Create a Flutter App and push it to your Git repository.
  • Go to Travis CI and log in with GitHub.
  • Give access to all repositories or select the ones you want to give access to.
  • Now, select your Flutter app repo where you would like to proceed with CI/CD testing.
  • Navigate back to your code on your local machine, create a .travis.yml file in your project root, add the following code, and push it to your git repo:
os:
  - linux
sudo: false
addons:
  apt:
    sources:
      - ubuntu-toolchain-r-test 
    packages:
      - libstdc++6
      - fonts-droid-fallback
before_script:
  - git clone https://github.com/flutter/flutter.git -b beta
  - ./flutter/bin/flutter doctor
script:
  - ./flutter/bin/flutter test
cache:
  directories:
    - $HOME/.pub-cache

Now, let’s see wexactly your .yml file is doing:

We are choosing our Operating System as Linux and installing packages libstdc++6 and fonts-droid-fallback because Flutter is dependent on these packages. In our sources, we need to mention ubuntu-toolchain-r-test to get the correct version of libstdc++6. We have 2 types of scripts to run here once our environment is set up. These are before_script and is script.

As the name suggests, before_script is executed before the main scriptinvolves installing Flutter from GitHub onto the test platform and running flutter doctor to test if Flutter runs fine. After this, your program is run with the command flutter test. If your project runs without any errors, then congrats, it works fine on all environments! However, if it fails, you can check your error and fix it on your local machine with the automated workflow. Any changes will be automatically triggered to run a fresh build.

Once Travis CI detects a .travis.yml file in your repo, it begins the CI as per the .yml file. Also, anytime you make a new commit to your repo, Travis CI automatically begins another CI test without you having to run it manually.

It’s that easy and simple to use Travis CI. You can also update your build status on your repo by adding the badge from Travis CI to your README file.

RELATED TAGS

flutter
travisci
community creator
RELATED COURSES

View all Courses

Keep Exploring