Methods to Use GitHub Actions to Automate Android App Improvement

There are numerous repetitive duties that we now have to do day-after-day. And they could be a bit boring, troublesome, and monotonous.
However as a substitute of laboring away at these each day duties, you’ll be able to delegate them so somebody or one thing else does them for you. That method, you’ll be able to have extra time to do belongings you wish to do. You possibly can have time to loosen up.
If you happen to’ve ever developed an Android utility, you understand how tedious some duties can get:
- Working assessments
- Ensuring the applying compiles when merging new code
- Constructing and publishing the applying.
So to whom ought to we go alongside these duties? One other coworker? They will simply go it alongside to another person and it gained’t liberate anybody’s time. Plus, we don’t wish to bum out our colleagues. The answer?
Say hey to GitHub Actions.
What Are GitHub Actions?
GitHub Actions are instructions we will set off when one thing occurs in our repository. At its core, an motion is a configuration file that has an inventory of instructions that describe:
- What must occur
- When it ought to occur
This configuration file is in YAML format (.yml) and an instance appears like this:
identify: My GitHub Motion
on: pull_request
jobs:
construct:
runs-on: ubuntu-latest
steps:
- makes use of: actions/checkout@v1
Let’s break down the instance above:
- We give a reputation to our motion (My GitHub Motion) [Optional]
- We signify when this motion ought to run (when a pull request is opened)
- We begin an inventory of duties (jobs) that ought to occur as soon as this motion is triggered
- The primary one is a construct motion
- The runs-on command tells GitHub which runner will execute this job (it is a digital server and you’ll select between Home windows/Mac/Linux)
- Every job can have many phases that are grouped collectively by the steps key phrase
- The makes use of key phrase tells the script what motion to enact
It is a very brief instance which doesn’t showcase all the options of GitHub Actions, however it gives a peek into the construction of the configuration file.
Within the subsequent sections, we are going to create actions that may assist hold our improvement cycle environment friendly and efficient.
Observe that each one GitHub Actions recordsdata must reside beneath your challenge’s principal folder within the path .github/workflows:
Methods to Create a GitHub Motion For Pull Requests
Whether or not you’re engaged on a challenge alone or a part of a crew, ensuring that your utility is steady is essential. So it makes complete sense to make sure that your utility is compiling correctly and all assessments are passing everytime you think about merging a pull request.
We’ve already proven in our instance how we will checkout the code in our repository. On this motion, we are going to embody the next steps:
- Organising the JDK model
- Altering permissions for the digital surroundings
- Working assessments (if we now have any)
- Constructing the applying
identify: Android Construct
on: pull_request
jobs:
construct:
runs-on: ubuntu-latest
steps:
- makes use of: actions/checkout@v1
- identify: Set Up JDK // 1
makes use of: actions/setup-java@v1
with:
java-version: 1.8
- identify: Change wrapper permissions // 2
run: chmod +x ./gradlew
- identify: Run Exams // 3
run: ./gradlew check
- identify: Construct Challenge // 4
run: ./gradlew assemble
You possibly can see that above, every step has its personal properties and attributes which might be particular to it.
I gained’t go into every considered one of them, since you are able to do that by yourself by the documentation. What’s widespread for a lot of the steps is the run key phrase. This attribute states what command to execute.
We’d like the second step in order that the digital surroundings can run the gradle instructions. With out it, it gained’t be capable to.
Methods to make a GitHub Motion For Publishing an Software
After getting printed your utility for the primary time, republishing it turns into kind of like a chore.
It’s important to be sure the model is upgraded, construct the APK, submit it through the Google Play Console and different tedious duties.
We are able to automate this course of with one other GitHub motion. This motion is a bit more sophisticated than the earlier as soon as because it requires using GitHub Secrets.
In a nutshell, GitHub Secrets and techniques are a option to retailer delicate data as surroundings variables of your repository. We shall be needing to make use of them as a result of:
- We might want to signal our utility
- We’re going to give this motion permission to submit our constructed utility to the Google Play Retailer
Let’s learn the way we will create GitHub Secrets and techniques first.
- Inside the principle web page of your repository, click on on the Settings tab
- On the left hand facet menu, there shall be an possibility titled Secrets and techniques
- To create a secret, press the New repository secret button
Now that we obtained that out of the best way, let’s take a look at the script for publishing an utility:
identify: Android Publish
on:
workflow_dispatch:
jobs:
construct:
runs-on: ubuntu-latest
steps:
- makes use of: actions/checkout@v1
- identify: Set Up JDK
makes use of: actions/setup-java@v1
with:
java-version: 1.8
- identify: Change wrapper permissions
run: chmod +x ./gradlew
- identify: Run Exams
run: ./gradlew check
- identify: Construct Challenge
run: ./gradlew construct
- identify: Construct Launch AAB // 1
run: ./gradlew bundleRelease
- identify: Signal AAB // 2
makes use of: r0adkll/sign-android-release@v1
with:
releaseDirectory: app/construct/outputs/bundle/launch
signingKeyBase64: ${{ secrets and techniques.SIGN_KEY }}
alias: ${{ secrets and techniques.ALIAS }}
keyStorePassword: ${{ secrets and techniques.STORE_KEY_PASSWORD }}
keyPassword: ${{ secrets and techniques.KEY_PASSWORD }}
- identify: Deploy to Play Retailer // 3
makes use of: r0adkll/upload-google-play@v1
with:
serviceAccountJsonPlainText: ${{secrets and techniques.SERVICE_ACCOUNT}}
packageName: com.tomerpacific.laundry
releaseFiles: app/construct/outputs/bundle/launch/app-release.aab
observe: manufacturing
You’ll have observed that this motion will run on workflow_dispatch. What does that imply? Principally it permits this motion to be triggered manually from GitHub itself.
You possibly can after all resolve you’d moderately run this motion when a push occurs on the principle department (for instance).
The step marked with 1 within the snippet above triggers constructing an .aab of our utility. Then, like we might do if we had been constructing it inside Android Studio, we now have to signal this .aab file.
That is the primary time GitHub Secrets and techniques come into play. We have to create secrets and techniques for:
- The Signing Key (secrets and techniques.SIGN_KEY)
- The Key Alias (secrets and techniques.ALIAS)
- The Retailer Key Password (secrets and techniques.STORE_KEY_PASSWORD)
- The Key Password (secrets and techniques.KEY_PASSWORD)
As soon as we now have signed the .aab file we will deploy it to the Google Play Retailer. At this step there’s a little bit extra work to do since we have to permit this GitHub Motion the permission to deploy purposes for us on Google Play. However, wait, how can we do this? We use a Service Account.
Methods to Create a Service Account
A service account is an entity that you simply create that tells companies or purposes it interacts with that it’s working in your behalf.
In our case, our GitHub Motion goes to work together with the Google Play Retailer so it might probably add a brand new model of our utility.
To create a service account, go to the Google Cloud Console. When you have no account there, be sure to create one. Then, on the principle web page, within the left hand facet menu, there shall be an inventory merchandise titled Service Accounts.
As soon as you have clicked it, on the best hand facet of the window you will notice any service accounts you have already got.
We wish to create a brand new one and within the prime a part of the window there’s a button to just do that.
Within the window that opens, you’ll have to enter within the service’s identify and you may as well enter an outline.
The identify given right here would be the distinctive identifier of this service account.
Within the second step you’ll be requested to offer this service account a task. From the Choose A Position dropdown, select Fundamental → Editor.
Lastly, within the third step, fill out your e-mail in each locations beneath the “Grant customers entry to this service account” part:
After urgent the carried out button, you’ll need to create a key for this service account. The motion will use this key to be recognized by Google Play.
To create the important thing, click on the three horizontal dots beneath the Actions label in the principle service account display. Within the menu that seems, choose Handle keys.
On this window, we are going to create a key by choosing the New Key button and selecting “Create new key” from the menu that seems.
Now we now have the choice of selecting the format of our new key – the default is JSON and we are going to go away it chosen. Click on create.
After getting carried out so, a file shall be downloaded to your pc. Make sure that to maintain this file because it has all the info related on your service account and also you gained’t be capable to obtain it once more.
We are going to take the contents of this file after which create a GitHub secret with it (secrets and techniques.SERVICE_ACCOUNT).
Final however not least, we have to make Google Play conscious of this service account. Doing so requires us to login to our Google Play Console account and heading over to Setup →API Entry.
If you happen to scroll down the web page, you will notice a piece titled Service Accounts. You need to be capable to see the service account you created beforehand. Click on the Grant Entry hyperlink
Within the settings that open, head over to App permissions. Right here you’ll select which utility this service account interacts with.
Underneath Account permissions, every thing beneath the releases part needs to be checked. I extremely advise you to have a look at all the opposite settings and resolve for your self what you wish to go away checked or what you wish to test off.
As soon as you’re carried out, click on the Invite consumer button situated within the backside proper nook.
After the invitation is distributed, we will run the publishing to retailer motion.
Methods to Monitor Our Actions in GitHub
To see what actions are outlined on your repository, click on on the Actions tab. This tab showcases all workflows outlined and people who have already run.
On the left hand facet you’ll be able to see all of the actions which have been outlined and on the best hand facet you’ll be able to see all of the actions which have been run. If you wish to take a look at a particular motion, you’ll be able to click on on it.
If the motion is outlined to run on workflow_dispatch, you will notice a button enabling you to run it (like within the image above).
If you wish to see a particular run of a workflow, you may as well do this from the principle Workflows web page by clicking on one of many runs. If one of many actions fails to run, this might be the place to analyze and see what went mistaken.
Our first motion is meant to be triggered when a pull request is opened. If it really works accurately, you have to be seeing this:
And there you have got it!
Wrapping Up
It’s been a protracted learn up up to now, however we now have gone by every thing that you must get began making a Steady Integration and Steady Deployment pipeline on your purposes.
In case you are concerned about seeing how GitHub Actions are arrange, you’ll be able to test them out in considered one of my repositories right here:
To learn extra about GitHub Actions, head over right here: