Continuously releasing Flutter app to Play store using Github Actions
In this tutorial, we will be setting up an automated release process for flutter application. Using github actions workflow, we will build android application and release it to google play store.
To follow along with this, you need a google developers account, flutter application and a repository in github.
We will be creating a workflow in github actions which will test application, build appbundle, and publish it to google play. This tutorial is more focused on releasing apps to playstore. If you are interested in understanding the workflow please have a look at this one: https://medium.com/@iqan/ci-cd-for-flutter-android-app-using-github-actions-ea065180a3be
If you have already prepared your app for release following the method, showed in flutter official documents, then jump directly to Add a new Release workflow section.
Preparing application for release
There are several tutorials out there using different methods of preparing an application for release. We will use the simplest option here. You can also follow https://flutter.dev/docs/deployment/android if you need more details for each step. We will update application to sign release builds with or own keystore.
- Generate app signing key
Navigate to directory where java binaries are present or just find a directory where keytool
is and run this.
Once that is done, copy the key1.jks or whatever the keystore name is to android/app directory of flutter app.
Create a key.properties
file in android directory of your flutter project. And add below properties.
Verify the files are in right place as they are seen here.
2. Configure app to use custom signing certificate on release builds
Open your app’s android/app/build.gradle. Add below lines before android section. This will tell gradle to load key.properties file which contains keystore and key information.
And add below lines before buildTypes. These lines will use our own keystore to sign the app.
3. Add `whatsnew` directory and update pubspec.yaml
Publishing app to playstore needs a note about the changes included in that release. We have created a folder distribution/whatsnew in application root directory. In here, we can add files for different languages.
We will update version in pubspec.yaml to 99.99.99+99
The reason for this is to replace this value with actual version number on build time. I have chosen this value so that it does not conflict with any package versions. You could also replace it with a token for example #{app_version}#
and replace it in workflow. But this will cause error in debugging. So better go with a valid version.
Add a new Release workflow
We will be creating a workflow in github actions to perform various tasks. The workflow contains 3 jobs.
- Generate a version number using GitVersion tool
- Replace some keys and version number, Test and build app bundle
- Publish app bundle to play store
Please note that, here, we are replacing 3 tokens. One is in pubspec.yaml file which is used to determine the version number and version code. We are using version number generated from gitversion tool and version code from number of build run. Rest two are keystore passwords.
To publish it to playstore we are using action “r0adkll/upload-google-play@v1”. You can modify the parameters as per your requirement. We have used internal track here.
Important and not to forget, I have added below secrets. If you mess this up, it will create build errors.
GH_TOKEN is your github token to calculate version number and also to create release in github.
KEYSTORE_PASS and KEYSTORE_KEY_PASS are your keystore passwords to sign application.
PLAYSTORE_ACCOUNT_KEY is the key of a service account which will be used to push app to playstore. To create a new account and get this key follow below steps.
Create a service account
We will need a google service account which will be used in github action to publish application in playstore. To create that, go to https://console.cloud.google.com/iam-admin/serviceaccounts/project and select your project.
Click on Create service account. Fill the form and you will have an account added. Copy the key for this account and store it somewhere for now.
Grant permissions to service account
Once service account is created, you will need to grant that account access to release applications. Go to Google play console and navigate to Users and Permissions.
Click on Invite User and add the user email address of the service account.
Now select Role: Release Manager for the application. This role will allow that service account to publish apps.
Once everything is done, push the changes in your repository and it should trigger the build. Once buid is finished, you should see similar logs. And this version is now published in play store.
There is a known issue for a new app that sometimes app is not being published. So if you have a new application which is not yet released in playstore, upload an apk built from this pipeline and roll-out for internal users. After that this workflow will be able to release apps without any issues.
Wrapping up
Setting up automated release process will help you focus on adding features to code instead of spending time in building a new app that your team can test before promoting to public.
Please have a look at this demo repository if you are stuck or want to refer.
Have a lovely day!