Publishing a Flutter package with Travis CI

Are you’re thinking of building a Flutter package or have you already built one but you don’t know how to set Travis-CI up? If that’s the case, you’ve come to the right place!

Building a Flutter package

The best way to learn how to build your own Flutter package is reading the Flutter documentation. The process is pretty straightforward so I don’t think it’s worth it to replicate it here.

One of the important things to take into account is that all Flutter packages are rated depending on the quality of the package. This means that if you provide an Example you will get more points. But don’t worry, whenever you publish your package you will be presented with some information on how to improve this score.

Composing the Travis-CI configuration file

All you have to do is create a new file called .travis.yml in the root of your project and copy the content below:

Note that we’re only publishing our package whenever we create a new tag.

Publication script

Once you have your .travis.yml file ready and set up, you must create a publication script.

Let’s create a new folder called .travis and a file called publish.sh in it. Copy the script below:

As you can see, the publication script is expecting some environment variables. But, how do we get them?

Unfortunately, the only way to get this information is by publishing your package manually so you can authenticate and get all the necessary tokens.

This should be an easy one-time process so let’s do that!

First, let’s test that everything is working fine and let’s do a “fake publication” by executing:

pub publish --dry-run

If you see something wrong, fix it. If everything is fine then just:

pub publish

You will be immediately required to open your browser and authenticate with your Google credentials and once you authenticate, a new file will be available to you in your computer: ~/pub-cache/credentials.json.

Open it and there you will find all the information you need to set up the Travis environment variables.

Things to take into account

There are a few things that I found out the hard way:

  1. If you provide an example folder make sure that you have a pubspec.yaml in it because otherwise flutter packages get command will fail. Make sure that everything works in this folder too before pushing any changes.
  2. Check that your publish.sh script has execution permissions. If you don’t do this you will get a cryptic 127 error.
  3. pub get is called automatically by Travis when you select Dart as the default language. This will lead you to receive an error (it seems to be a known issue in Travis). The only way to get rid of it is to completely override the install step. This is already managed for you in our previous .travis.yml script.
  4. The publication tokens will eventually expire so we’ll have to reset the Travis environment variables when this happens (and hence do a manual publication again :( ).

Trying it out

If you have completed all the steps, you’re project will be analyzed, tested and built without publishing every time you push to your git repository.

Whenever you’re ready to publish, just create and push a new tag and Travis will take care of it!

Of course, feel free to customize the scripts according to your needs.

Enjoy!


Originally published at robertohuertas.com on January 20, 2019.