Implementing a Flutter CI/CD Pipeline in Jenkins: Part 1 (Introduction, Strategy, and Environment Setup)

In order to seriously consider a toolkit for development, be it cross-platform or single platform, it must be possible to implement a Continuous Integration/Continuous Deployment pipeline for that toolkit. Flutter is no exception to this, though Flutter’s suggested CI/CD walkthrough didn’t exactly fit Bottle Rocket’s needs. Flutter has a page on CI/CD that recommends using fastlane as a means to push builds to App stores.

Bottle Rocket’s needs are a bit more involved than just pushing builds to App stores. At minimum, at Bottle Rocket, we prefer to be able to have new pull requests automatically tested and made into test builds for QA. This generally requires a Jenkins server watching our git repositories. However, Flutter is a bit more complex than just a single platform in terms of integration with Jenkins. At minimum, it requires having the Flutter SDK installed on whatever machine or server is running Jenkins.

This complexity coupled with Flutter’s nascent nature, posed a bit of a challenge as there aren’t many articles or sources for setting up this proposed Jenkins environment. However, it is definitely possible to implement even with minimal(or no) Jenkins/Docker experience! Personally, I’d never set up a Jenkins environment before this foray into the world of CI/CD.

If your build environment is fresh/new and without build tools, you’ll need to install those as well. Additionally, I recommend that your build server will be a MacOS system, as iOS building requires MacOS. The rest of this article assumes all libraries, packages, and SDKs are installed correctly(this can be verified with the ‘flutter doctor command’. Below is a quick list of the things to install. I’ll also go into detail on each item below the list.

Homebrew

Installing Homebrew should be the first thing you do, as it’s used in a lot of other steps below. It’s as simple as pasting the below line into terminal and following the prompts.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

XCode and Command Line Tools

If you’re only deploying to Android, you can skip XCode. However, as Flutter’s main strength is that it’s cross-platform, I don’t recommend this. Assuming you’re taking advantage of all Flutter has to offer, you’ll want to install XCode and the appropriate command line tools. This can be done through the Mac App Store.

Flutter SDK

The MacOS Flutter installation page details both installing a specific version of the Flutter SDK as well as using git to install the latest stable build. I recommend using the latest stable build personally, and the git cloning method is very simple (Assuming you’ve already installed git. If not, the steps for doing this on MacOS are detailed here. I’d recommend using the Homebrew method)! The other bit that the installation page glazes over is setting the ‘flutter’ global PATH. Luckily, another page on the official Flutter knowledgebase goes over this! I recommend reading it and following the steps outlined within.

Java

In order for your project to generate an Android app, Java must be installed on your machine. OpenJDK is the best option for this. This can be installed with Homebrew with the below command

brew cask install adoptopenjdk8

Once Java is installed, JAVA_HOME must be set in the path. This can be done with an export command, likely similar to the one below. Your path may be different depending on which version you install and where it’s installed. The important bits are at the end, specifically ‘Contents/Home’.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Android SDK

In order for Flutter to export Android APK’s, you must install the Android SDK. If you have access to the machine physically and can use a GUI, Android Studio a good option. However, I recommend using Homebrew once again, as it’s doable from the terminal.

brew cask install android-sdk

Once the SDK is installed, you should set ANDROID_HOME so Flutter knows where the SDK is. This path may change based on the version of the SDK you install. In my case, the ‘Home’ folder for the SDK was printed out in terminal on completion of the Homebrew installation. After setting the home directory, the next line tells Flutter where it is.

export ANDROID_HOME=/usr/local/Caskroom/android-sdk/4333796/

flutter config --android-sdk $ANDROID_HOME

In order to install the necessary build tools for Flutter(currently a minimum of API level 28), the below commands must be run. Without the build tools, APKs can’t be built.

sdkmanager platform-tools "build-tools;29.0.2"

sdkmanager platform-tools "platforms;android-29"

sdkmanager platform-tools "build-tools;28.0.3"

sdkmanager platform-tools "platforms;android-28"

sdkmanager platform-tools "extras;android;m2repository"

Cocoapods

Many of the packages and libraries used in Flutter are accessed through Cocoapods. To install Cocoapods, it’s recommended to use Ruby. Ruby can once again be installed via Homebrew.

brew install ruby //Installs Ruby

sudo gem install cocoapods //Installs Cocoapods

Fastlane

To build and export iOS IPAs, this Jenkins pipeline will use Fastlane. Fastlane can be installed via the below Homebrew command.

brew install fastlane

With all these libraries and frameworks installed, we can move on to installing Jenkins, running a Jenkins instance on MacOS, and adding the necessary Jenkins plugins for our pipeline! This concludes Part 1 of this 4 part series on Implementing a Flutter CI/CD Pipeline in Jenkins. Part 2 covers Jenkins Instance Setup.

Published by Steven Foskett in Android, iOS, Technology, App Development, Flutter