Automate Firebase hosting with Github Actions and Ruby.

set up CI/CD pipeline

Featured image

Before start, let’s understand why do I need this ? Since I have been working on my blog and deploying it over firebase and again pushing them back to github for version control and keeping source code hosted to reduce the chance of deletion from my location system , which is very common to me! Being a lazy and troubleshooter guy I planned to reduce my workload , and minimalise them. That’s how journey of CI/CD starts.


# USE CASE:-

1. Setup firebase hosting 
2. Integrate hosting with Github Repository 
3. Setup Workflow inside project for github actions with firebase token permission 
4. Setup auto mere PR for main branch with all dependecies check. 

Expected Result

Here I have been using Ruby VS 2.7.4, since my blog is based on Jekyll open source template.

Why Firebase?

STEPS

I already have github repo with branch:main configured so skipping step 0 , in case if you don’t have then make a github repo with any branch (name it as per ur requirement)and connect it with local codebase.

firebase init hosting

Hosting with Github

In case firebase hosting is already initiated, then run below command

firebase init hosting:github

service account

Now inside local codebase .github/workflows folder will be automatically created.

githubflows

Code for hosting-merge.yml

name: Deploy to Firebase Hosting on merge
'on':
  push:
    branches:
      - main
jobs:
  build_and_deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: 'sudo gem install bundler && sudo bundle install && bundle exec jekyll build '
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: '$'
          firebaseServiceAccount: '$'
          channelId: live
          projectId: shubhendu-shubham //replace it with your project ID

Code for hosting-pull-request.yml

name: Deploy to Firebase Hosting on PR
'on': pull_request
jobs:
  build_and_preview:
    if: '$'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: 'bundle install && bundle exec jekyll build '
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: '$'
          firebaseServiceAccount: '$'
          projectId: shubhendu-shubham
sudo gem install bundler && sudo bundle install && bundle exec jekyll build


Explanation :

  1. Since we have fresh installation of jobs , so neet to install first bundler by running gem isntaller bundler with sudo permission.

  2. second command sudo bundle install will install all the project dependencies inside the ubuntu .
  3. bundle exec jekyll build command will build our webapp under public folder called _site.
  4. Since inside firebase hosting we already have set _site as public folder.

Jobs in Action :-

As soon as you push your code to the main branch automatically github actions will trigger and it will deploy your production ready webapp.

Deploay action

Finally I don’t need to worry about this command , after every github push.

firebase deploy 

Our task become easy with CI/CD setup. Thanks for reading !