How to use Gitlab CI to upload Json data to Firebase Realtime Database
Firebase makes it very easy to deploy web apps and other platform native apps for Android and iOS. However, if you are using the Real-time database of Firebase, you will find it difficult to upload a huge json data file using the Firebase Web console.
One solution is to use Gitlab CI with Firebase to upload the json file.
Step 1 : Generate suitable credentials by creating a service account
You need to visit https://console.developers.google.com/project/_/apiui/credential
Step 2 : Create Gitlab CI config file in Gitlab
Gitlab requires you to have a config file as .gitlab-ci.yml in the repository for Pipelines to work. I have created a simple config file as below:
# Node docker image on which this would be run
image: node:11.12.0
#This command is run before actual stages start running
before_script:
- npm install firebase-import
- export PATH=$PATH:`npm bin`
stages:
- deploy
# lint and test are two different jobs in the same stage.
# This allows us to run these two in parallel and making build faster
deployToFirebase:
only:
- master
stage: deploy
script:
- bash deploy/deploy.sh
I use a module called firebase-import to do the heavy lifting.
I have created the above folder structure for the Gitlab repository. The json file to upload is kept inside json folder. The service json file (generated in Step 1)is kept in deploy folder along with a shell script called deploy.sh. The shell script contains the following line:
echo -ne '\n' | firebase-import --database_url https://XXXXXX.firebaseio.com --path / --json /builds/<gitlab-username>/<gitlab-repo-name>/json/<json-filename>.json --service_account /builds/s/builds/<gitlab-username>/<gitlab-repo-name>/deploy/<servicefilename>
Please replace values according to your username and repository name in Gitlab.
Step 3 : Make a Merge request to Master
When you change the json file and make a merge to master, the file is uploaded. It gives a nice version history of file to revert things if anything goes wrong quickly. (Also enables code reviews etc.)