Setting Up Ci/Cd for flutter desktop applications

Anthony Aniobi
ILLUMINATION-Curated
4 min readSep 30, 2022

--

How to setup github actions for building and releasing windows, macos, and linux applications on flutter

image by created by me

This article walks you through the code setup for building and releasing your flutter desktop applications for macOs, android and Linux using github actions.

This article would explain the steps taken as well as the pitfalls you may encounter when doing it your self. so without further ado, let’s get started.

Nb: This article assumes that you have completed your flutter desktop project and is ready for building on github

These are the steps to setup CI/CD for your desktop deployment

Step 1: Create workflow file

In your flutter project directory create a folder named .github and in the folder create another folder called workflows this is where all your github aciton file would be stored.

For this project, the file would be called deploy.yml (you can give it any name you like but dont forget the .yml extension)

Step2: Add Triggers to workflow

Triggers are events that start up a workflow. For this project, the workflow would be started whenever a push is made to the main branch. The code for the trigger is shown below.

on:
push:
branches:
- main

Find out more about workflow triggers from the github documentation

Step3: Workflow jobs

Jobs are the runs that perform the actual task for your workflows. You can have more than one job on a workflow and they work in parrallel by default.

This project uses three jobs, each for the platform you are building for (ie. macos, windows and linux). The three jobs for this project are simmilar, they do the following steps:

  • setup operating system
  • setup flutter
  • install dependencies
  • build project
  • package project
  • release the packaged file on github

Due to the simillar tasks performed by these jobs. I would use one job to explain what each step does. The job I would use is the job for deploying for linux operating systems.

the first you would have to specify the operating system this job would run on. In this case it would be the linux os

Nb: for windows and macOs you would need to use windows and mac operating systems respectively. See the complete code at the end of this article

linux:
runs-on:
ubuntu-latest

Next is to add the steps for this job and the first tasks for the step is as follows:

     - uses: actions/checkout@v2
- uses: subosito/flutter-action@v1
with:
channel: 'stable'
- name: Install os dependencies
run: sudo apt-get install -y clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev

1. the first line checksout your code to github actions

2. the second line sets up flutter for your project,

3. finally this step install the necessary depedencies that are required to build desktop app on linux. Nb: this step is required for only linux

Now the following steps helps to build the app:

      - name: Install project dependencies
run: flutter pub get
- name: Generate intermediates
run: flutter pub run build_runner build --delete-conflicting-outputs
- name: Enable linux build
run: flutter config --enable-linux-desktop
- name: Build artifacts
run: flutter build linux --release
  1. First line installs dependencies for the flutter project by running flutter pub get on the terminal
  2. The next command makes sure all conflicting build outputs for your project are deleted so as to avoid error in build. Nb: you have to add build_runner to your dev_dependencies in your pubspec.yaml file
  3. Enable build for linux using the next command. Nb: for windows and macos you would enable build for their respective operating systems
  4. The final command builds a flutter release for linux.

Package the application using the command:

        - name: Package Release files
uses: thedoctor0/zip-release@master
with:
type: 'zip'
filename: local_api_server-${{github.ref_name}}-linux.zip
directory: build/linux/x64/release/bundle

This command stores the built application in a zip file. the file name for this zip folder is specified using the filename tag and the directory section tells this step the location of the built files.

This project add${{github.ref_name}} which provides the name of the branch triggering this workflow to the name of the file. This enables the developer to set version names on the zip files.

for example, if you push to the main branch from a branch called v1.0.0 the zip file would have this name added to the file name. As a recommendation, use the following format for your own code:

<your_app_name>-${{github.ref_name}}-linux.zip

replace <your_app_name> with the name of your application.

Finally, Release your Packaged (zip) files using the command

- name: Release to github
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{github.ref_name}}
files: build/linux/x64/release/bundle/local_api_server-${{github.ref_name}}-linux.zip

This step requires your github token , a unique tag_name for your current build as well as the zip files that contains your application. For the unique tag_name we would use the ${{github.ref_name}} which provides the name of the branch triggering this action.

Setp4: Replicate for windows and macos

Replicate this step for macos and linux. to have a complete workflow for all three operating systems. The final result is shown below.

Finally push your code to github and you are set 💃.

Checkout my project on github which uses this workflow, don’t forget to leave a star on the project if it was helpful.

Have a nice day 👋

--

--