Build & Release APK via GitHub Action
- 4 minsRecently, I was wondering: is it possible to push a new android apk release via Firebase App Distribution using GitHub Action as CI/CD on every new tag release? Well, turns out you can and it is more simple than you can imagine! There are GitHub Actions written for this already, but if you go ahead and try it in default state, it fails and the lack of documented explanation makes it difficult for a new user to understand why. This is what led me to document this process step by step, so it can easily be understood, and edited as per the requirement.
Process
All this just by pushing one new tag to your GitHub!
Result
How to achieve that?
Follow the below process step by step to build and release APK via Github Actions when you push a new tag to your repository and it will automatically be attached to the release under this tag in your repo.
Prerequisite
- Create a GitHub repository and push your current Android Project to the GitHub repository
- In your repository settings add the necessary secrets and environments variables as mentioned below.
Secrets
You’ll need to provide this secret token for this action to publish the APK to your own repo and to attach it with the release of the created tag (more about this below). I am not sure as to why using the default GITHUB_TOKEN
provided universally will fail to authorize the user.
This is the workaround and it works.
How to get Secret?
- TOKEN: Create a new access token with
repo
access - Enter these secrets in the android project’s GitHub repository’s Settings > Secrets
Environment Variables
You’ll need to provide these environment variables to specify exactly what information is needed to build the APK.
- APP_FOLDER: main folder to search for the apk. Default is the
app
and you don’t need to change anything as of now.
YML
name: Build & Publish Release APK
on:
push:
tags:
- '*'
jobs:
Gradle:
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v2
- name: setup jdk
uses: actions/setup-java@v1
with:
java-version: 11
- name: Make Gradle executable
run: chmod +x ./gradlew
- name: Build Release APK
run: ./gradlew assembleRelease
- name: Releasing using Hub
uses: sangatdesai/release-apk@main
env:
GITHUB_TOKEN: $
APP_FOLDER: app
Steps
- Provide the required Secrets and Environment variables in your GitHub Repo
- To use this action simply create the below YML file at this specified path in your Android Project Directory’s Root Folder.
For e.g.:
.github/workflows/android.yml
- Copy & paste the above yaml code in your YML file
- After that, whenever you are ready, make a commit and push it to Github Repo
- Create and push the tag (for e.g. 1.0) against which the apk will be attached
- As soon as you push the tag this github action will be initiated and generated apk will be released under releases with the same tag, which you can check in your github > code > releases (e.g v1.0)
How to push a new tag?
Git add .
Git commit -m "new release"
git push
git tag 1.0
git push origin 1.0
Further Customization
By default this action will create the ‘release’ flavor and If you want to change the flavor of the apk being built to let’s say ‘debug’ then change the below line in your yml file
YML
...
- name: Build Debug APK
run: ./gradlew assembleDebug
...
What Next?
- The generated apk at this stage is unsigned won’t be able to get installed. To do that we need to sign this for which we need to add few more lines to the current yml which we’ll see in the next part along with how the generated apk can be directly passed to the firebase’s app distribution list so your testers will have this automatically available.
- While this will initiate GitHub action on a push of each new tag in your master branch, you can easily make changes in the yml to accomplish this on a different branch or initiate on a pull request.
GitHub Action Marketplace
I have Published this on GitHub Marketplace for anyone to use this and by default it will copy the release apk with any name from the ‘release’ folder of outputs.
But if for some reason you want make changes, do create a fork and edit the necessary files.
Credits
Based on ShaunLWM & kyze8439690