Github Actions: Automize Your Android Build & Release Workflow

Ronak Ukani
Simform Engineering
6 min readApr 5, 2021

GitHub Actions make it easy to automate your software workflows like build, test, and release. It is one of the important practices that every developer should follow to optimize their tasks. Before starting Github actions, you must have little bit knowledge of CI/CD.

Basic Introduction of CI/CD

With the CI/CD (Continuous Integration and Continuous Delivery), you can make your software development process smaller, faster and reliable. Continuous Integration is a development practice where developers integrate code into a shared repository several times a day. Each integration can then be verified by an automated build, security checks, code coverage, and automated tests. Continuous Delivery, on the other hand, automatically deploys all code changes to a testing or production environment.

What is Github Actions?

Github Actions is set up a CI system with your projects. Say, an event like push occurs in a repository. It triggers a workflow and checks every step defined in a workflow file.

Why use GitHub Actions?

Github Actions is fully integrated into Github and therefore doesn’t require an external site or tool. It reduces the feedback loop and notifies you as soon as the system encounters a problem. With the use of GitHub actions, You no longer need to worry about manually uploading the build to your production environment.

Start Workflow With Android

Let’s begin with creating a YAML file for setup workflow. First, go to Android Studio and under the project section create a new directory .github. Now create a new directory called workflows under the .github directory. Then create a new file with the name of build_release_workflow.yml

It looks like this,

Core Concepts of the YAML file:
Here are some core concepts you need to understand before writing a YAML file:

Actions
Actions are the smallest portable building block of a workflow. You can find an action available in the GitHub Actions marketplace. It is a repository published by the community and accepted by GitHub if it meets their standards. What’s more, you can also create your actions.

Event
Events are specific activities that trigger a workflow. For example, commit/PR made to a repo, creating a new release, forking a repo, etc..

Runner
Runner is a machine which has the GitHub Actions runner application installed which looks for jobs, runs actions, reports the progress, and generates the results.

Job
Job is made up with multiple steps that run and executes on the runner which defined into the workflow file. You can see a sequence of every single job when thDonee workflow run.

Step
A step is a list of tasks that can be executed by a job. Steps can run commands or actions.

Workflow
It is a YAML file. It's a set of jobs that trigger and runs each and every event. The YAML file is defined in the .github/workflows directory.

Let’s take a look at our full .yml file step-by-step:

name: Android Build Release Workflow

on:
push:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8

- name: Unit tests
run: bash ./gradlew test --stacktrace

- name: Run Tests
run: ./gradlew test

- name: Build Project
run: ./gradlew assemble

- name: Build Debug APK
run: bash ./gradlew :app:assembleDebug

- name: build release
run: ./gradlew assembleRelease

- name: Upload APK
uses: actions/upload-artifact@v2
with:
name: App
path: ${{ github.workspace}} /app/build/outputs/apk/release/app-release.apk
- name: upload artefact to App Center
uses: wzieba/AppCenter-Github-Action@v1
with:
appName: ronakukani/Github-Actions-Demo
token: ${{secrets.APP_CENTER_TOKEN}}
group: Testers
file: app/build/outputs/apk/release/app-release.apk
notifyTesters: true
debug: false
releaseNotes: "here is your release note"

- name: Send message to ms teams
uses: dhollerbach/github-action-send-message-to-ms-teams@1.0.10
with:
webhook: 'Here is your Microsoft Teams Webhook URL'
message: 'Here is your message'

First, we add the name of workflow like this,

name: Android Build Release Workflow

Here, we define the triggers like when the workflow runs. Here when you push your code to the develop branch then it will trigger your action.

on:
push:
branches: [ master ]

You can also add different triggers like when you do pull request and merge the specific brach etc.

Now, you can define a single job inside our workflow that will run a command or invoke another action. Here the job is run on Linux Machine. You also can run on windows or mac. Click to know more about runs-on.

jobs:
build:
runs-on: ubuntu-latest

Inside steps, you can define the list of steps that our job will execute, Here we invoke, an external action and set java version 8 to run some tests by actions/setup-java@v1

actions/checkout andactions/setup-java@v1 are predefined actions with the Github server.

steps:
- uses: actions/checkout@v2
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8

Now let’s perform and run some unit tests.
First, create debug and release build and upload them on GitHub’s server from there you can download the build. To get your build goto the Github repository -> Actions tab -> Workflow.

- name: Unit tests
run: bash ./gradlew test --stacktrace
- name: Run Tests
run: ./gradlew test
- name: Build Project
run: ./gradlew assemble
- name: Build Debug APK
run: bash ./gradlew :app:assembleDebug
- name: build release
run: ./gradlew assembleRelease
- name: Upload APK
uses: actions/upload-artifact@v2
with:
name: App
path: ${{ github.workspace}} /app/build/outputs/apk/release/app-release.apk

Upload the build over Appcenter:

Now we can upload the app to the App Center for internal sharing purposes. We can use wzieba/AppCenter-Github-Action@v1 action to upload the build over Appcenter. By using this action we can upload .apk & .ipa file to the appcenter. You can also find similar actions from Github Marketplace. Furthermore, you’ll need a token to upload your application. We can define it by going to repository -> Settings -> secrets. Click on New Secret and add a secret name that’s same as .yml and value.

- name: upload artefact to App Center
uses: wzieba/AppCenter-Github-Action@v1
with:
appName: ronakukani/Github-Actions-Demo
token: ${{secrets.APP_CENTER_TOKEN}}
group: Testers
file: app/build/outputs/apk/release/app-release.apk
notifyTesters: true
debug: false
releaseNotes: "here is your release note"

You can upload the app to the play store with this action r0adkll/upload-google-play@v1

After Releasing the build, you can notify your testers, team members, clients, etc.. via Microsoft Teams Channel. We have used dhollerbach/github-action-send-message-to-ms-teams@1.0.10 action to send a message to the Teams channel. We need to create a webhook to send a message. To know more about sending messages to connectors and webhooks click here.

- name: Send message to ms teams
uses: dhollerbach/github-action-send-message-to-ms-teams@1.0.10
with:
webhook: 'Here is your Microsoft Teams Webhook URL'
message: 'Here is your message'

After complete all the steps, when you push your code to the master branch then workflow start running under the action tab. If the workflow fails due to any reason, you can see the log there. You will also receive a notification via mail after successfully completing the workflow.

Here’s what you’ll see on your screen:

Congratulations! You’ve now cleared all steps and published your build.

Conclusions :

The CI/CD flow by GitHub Actions provides easy integration with your code. Since it’s highly customizable, you can make your own actions and publish to Github MarketPlace. It makes your software development faster and reliable.

Don’t forget to clap and share your feedback in the comment. let’s connect on Twitter, Linkedin, and Github.

--

--

Ronak Ukani
Simform Engineering

Software Engineer | #AndroidDeveloper | kotlin❤️ | Tech lover | Explorer | Learner