Running Android unit testing on GitHub action with a project that contains firebase dependency
Hello folks!! These days I’ve faced the problem to run my unit testing on the Github Actions once while the build process was started it throws an exception not letting the tests start.
So, I thought of some solutions to solve this problem.
The first one is to push the file google-services.json as the first solution, but it brings the problem of exposing the sensitive project data on the repository. So, it is not a good approach.
The second approach is to figure out how to inject the file google-services.json before the build starts without generating any security issues.
It sounds like a good solution, right? but how to do it?
Well, the GitHub gives us a great feature called Secrets. It is responsible to encrypt the data allowing anyone with collaborator access to use it on workflow by environment variables. So, Let’s go further and use it in this post.
Contextualization
This post is separated into two sections. The first one
Configuring environment variables in GitHub
First of all, you need the collaborator access to configure a new secret on your Github project.
To configure a new secret, go to your Github’s project and click on Settings-> secret
and finally, go to a new secret option.
The option called Name is the environment variable and Value must contain the raw google-service.json content. So, fill out these fields with your project information and click on the button Add Secret.
For our post, I going to create the environment variable called FIREBASE_SECRET.
Using the environment variable
To use it, go to your GitHub Actions and insert a new step into your job called Decode google-services.json as will be shown below
- name: Decode google-services.jsonenv:FIREBASE_SECRET: ${{ secrets.FIREBASE_SECRET }}run: echo $FIREBASE_SECRET > YOUR_PROJECT_FOLDER/app/google-services.json
The first line name is being used to name the step into the job section. The second line env is going to create a new section to get the secret value created in the first section. Finally, the run section is going to use this value to retrieve the content provided by FIREBASE_SECRET environment variable. Finally, after you have done this step your project can run unit testing properly.
Running the unit testing
To run your unit testing into GitHub action you must create a new step on your job calling the job task as will be shown below.
- name: Run unit testingrun:./gradlew test
Finally, If all unit testing ran successfully the GitHub will show something like that.
You can check it out the full GitHub Action on my github.