firebasegithubwebhooksfirebase-hostinggridsome

Firebase Hosting Webhooks


I have this specific use case that I'm trying to solve with Firebase Hosting and it's the following:

  1. I'm using Gridsome to generate static websites
  2. Before building, Gridsome uses Graphql and grabs from a Headless CMS some blog posts, which then are used to create static pages for each of them
  3. Currently, the Gridsome build is automatically deployed to Firebase Hosting whenever I merge into master through Github (this works great already)

The problem that I'm facing is that I want to trigger a Gridsome build + Firebase Hosting deploy whenever I publish a new blog post on my Headless CMS (It has the option to make a POST on new blog post). I know this is possible on other solutions like Vercel, but right now I'm using Firebase and would like to stick to the suite.

I've tried using the REST API without any luck, and Github Webhooks works the other way around and doesn't solve my problem. I'm thinking there might be a chance to call a Firebase function and use the CLI to deploy or something.

What I would like to know is if this feature exists and how to accomplish this scenario.

Thanks.


Solution

  • I assume you use Github Actions to build your project and then deploy it to firebase.

    The easiest solution that I would recommend for you is to set up your Action to trigger not only on a push into your master branch, but also for a request.

    Steps

    Set your Action trigger for the repository_dispatch Event

    on:
      repository_dispatch:
    

    Then create an Access token for your Github Account with repo and workflow permission.

    Now you just have to set your CMS Post Request on the following URL https://api.github.com/repos/<username>/<repo>/dispatches with your Authorization Token in the Header.

    Example:

    curl --url 'https://api.github.com/repos/<USERNAME>/<REPO>/dispatches' \
      --header 'authorization: Bearer <TOKEN>' \
      --data '{"event_type": "content updated"}'
    

    Now your Action should also dispatch whenever you make a POST Request from your CMS to the endpoint.