CI/CD pipeline for Android via Github workflow

Haresh VaghelaHaresh Vaghela
3 min read

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

1_NZ3JkgWZM5t8RwMvv-bHiA.jpeg

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

1_8SvD7qGobmPiHzXvO76FjQ.png

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

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
  1. When you push your code to the branch, you will see the workflow run actions as shown below

1_UgPmHlCoDCC0vM7pqhDjlg.png

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.

1_bBxkJSBKSNGXfhjvpepNkQ.png

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

1_IUMnaRlWMLCCx_O88CInoA.png

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.

1_63sc4n705W4l_broW9FDxw.png You can now run the workflow by clicking the Run workflow button

1_bsbHxoEZTLXK2PhTASbCBw.png Afterward, it will start the build process to create an artifact.

Hope you find it usefull 🤝

0
Subscribe to my newsletter

Read articles from Haresh Vaghela directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Haresh Vaghela
Haresh Vaghela