CI-CD pipeline for Android via Github workflow

HareshVaghela
Mobile App Development Publication
4 min readMar 20, 2022

Continuous Integration/Continuous Delivery Automates building operations in order to bridge the gap between development and operations

Photo by me on Unplash

It’s sometimes necessary to provide an Android APK to a client or to another team member, for example, the QA team. APKs are created, and then you upload some cloud services in order to download others.

Your plan to provide just an APK appears to be a long task. Wouldn’t it be great if it happened automatically?

The following is a method for creating a pipeline in which GitHub builds an APK when your code is pushed to your branch.

For more about Github workflow actions.

Step One: create a folder and YAML file

  • In the Root folder of your project — create a .github folder
  • As well as create another folder inside workflows
  • Inside the workflows folder — create an app-job-on-push.yaml file
Folders should be arranged as follows

Step Two: write an action to perform an operation for creating a build of your project.

Here branches: will be your branch name (main, master), and based on the java version you have to set java-version as well.

name: android-paging-coroutine

on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest

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

- name: Build with Gradle
run: ./gradlew build

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

- uses: actions/upload-artifact@v2
with:
name: Package
path: app/build/outputs/apk/debug/

Step Three: execution permission — before pushing code to your branch

You need to update the execution permission for gradlew file.

  1. You can do this by using Git Bash or Terminal, CMD as well.

Locally: chmod +x gradlew

Git:
git update-index --chmod=+x gradlew
git add .
git commit -m "Changing permission of gradlew"
git push
You should see:
mode change 100644 => 100755 gradlew

2. When you push your code to the branch, you will see the workflow run actions as shown below

In the Action tab, you will see the workflow name as follow

The workflow will automatically build the project and produce an output artifact package that contains APK files.

Once the job is completed, you will be able to download your package.

Those who need the APK can now access just that workflow package link.

BUT — this will create a build every time when you push your code to a branch.

Alternatively — we can give a manual action as well. that will allow us to build APKs whenever we want.

  1. To accomplish this, you must create a manual job in your YAML file as follows, which will dispatch your workflow when you manually run it.
name: android-paging-coroutine

on:
workflow_dispatch:
inputs:
branch:
description: 'Branch'
required: true
default: 'master'

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: "${{ github.event.inputs.branch }}"
submodules: 'true'
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8

- name: Build with Gradle
run: ./gradlew build

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

- uses: actions/upload-artifact@v2
with:
name: Package
path: app/build/outputs/apk/debug/
#path: app/build/outputs/
#path: [MODULE]/build/outputs/
retention-days: 2

Retention-days will delete your build in the numbers of the days you set

→ Once you have pushed to the branch, find the action tab. There will be another workflow that will not run unless it is manually started by you.

You can now run the workflow by clicking the Run workflow button
Afterward, it will start the build process to create an artifact.

Hope you find it usefull 🤝

from Giphy

--

--

HareshVaghela
Mobile App Development Publication

Passionate about crafting high-performance mobile applications and actively sharing insights to elevate the collective expertise of the developer community.