Setup CI/CD Pipeline for your Flutter App using CircleCI

Parth Jansari
Flutter Community
Published in
3 min readApr 11, 2018

--

Continuous integration systems let you automatically build and test your app every time you check in updates to your source control system. You can use any continuous integration tool that can run tests and generate apk. We at Zypher use CircleCI to setup our build pipeline that does linting, runs tests and uploads generated build apk to Google Cloud Storage.

Setup

We are using CircleCI 2.0 and https://hub.docker.com/r/cirrusci/flutter as a base image. cirrusci/flutter comes with android sdk and flutter pre installed to our circleci.yml would be just about running flutter doctor, and then all the stuff like building apk, running tests, running linter etc. Our circle.yml will look like below

The first line defines that we want to use version 2 of circleCI

version: 2

Then we define jobs and pull the docker image via

docker:
- image: cirrusci/flutter

Then we run the tests and build the apk with verbose.

- run: flutter test- run: flutter -v build apk

If you just want to run tests you can remove the build commands or vice varsa

Bonus: Upload build apk to Google Cloud Storage

The generated apk can be later consumed by QE team, Project Manager or the customer directly. This allows us the stakeholders to easily install, review and give feedback. Google Cloud Platform provides $300 free credits so signup today.

The first step in establishing connection with GCP is to generate service account key which can be generated by visiting this link

Please keep in mind that the Service Account is a credential that can be used to interact with the project on your behalf, so keep it secret along with any other credentials.

After generating service_account.json follow the below steps to configure CircleCI to use the credentials

  1. Copy the JSON file you downloaded to the clipboard.
  2. In the CircleCI application, go to your project’s settings by clicking the gear icon in the top right.
  3. In the Build Settings section, click Environment Variables, then click the Add Variable button.
  4. Name the variable. In this example, the variable is named $GCLOUD_SERVICE_KEY.
  5. Paste the JSON file from Step 1 into the Value field.
  6. Click the Add Variable button.

Dump Credentials from Environment Variables to the file

-run: echo $GCLOUD_SERVICE_KEY > ${HOME}/gcloud-service-key.json

Configure google cloud sdk

The last step would be configure google sdk

Gcloud sdk can be downloaded via command

- run:   wget -qO- 'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-195.0.0-linux-x86_64.tar.gz' | tar xvz

Above command will download and extract google cloud sdk. Configuring google cloud sdk involves two steps they are as follows:

- run: ./google-cloud-sdk/bin/gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json- run: ./google-cloud-sdk/bin/gcloud config set project 'your project id'

The first step will configure your gcloud sdk to use the service account json that you just created in your build container and second command will set the project id for the project.

Lastly we need to copy the generated apk to Google Cloud Storage by Running ./google-cloud/bin/gsutil cp filename gs://bucket-name

- run: ./google-cloud-sdk/bin/gsutil cp ./build/app/outputs/apk/release/app-release.apk gs://built-apk-store

Above command will upload the apk to your storage bucket named built-apk. There you have it a CI which builds apk and uploads it to Google cloud storage. If you want to keep versions of apk you can assign a date stamp or hash of the commit to the name and too keep track of the pull requests and builds from different branches you can also include branch name in the apk. For that you will need a separate upload.sh and you can call it from CI config file.

Adding everything up our config.yml looks like below:

config.yml
upload.sh

--

--