Set up Firebase Hosting in front of a Cloud Run service, without using the firebase CLI

Cloud Run serves pretty much anything and has a pay-per-use billing model, making it very economical notably for side projects.
However, requests sent to a Cloud Run service are not subject to any caching, to so so, one must use a CDN on top of Cloud Run.
It's custom domain feature is also not available in all regions.

Firebase Hosting is a quick and easy way to cache requests sent to your Cloud Run service, and to add a custom domain to it. However, setting up Firebase Hosting usually requires the use of the Firebase command line tool and the creation of a firebase.json file. This is not a problem when hosting static assets or when using a more advanced Firebase configuration, but sometimes, all I need it a layer of caching and a custom domain for my Cloud Run service.

I created a simple bash script to enable Firebase Hosting in front of a Cloud Run service:


# Set up Firebase Hosting in front of a Cloud Run service, without using the firebase CLI
# The following commands must be installed:
# - gcloud
# - curl
# - jq

# Update these variables
PROJECT_ID="enable-fb-hosting" # Make sure you have enabled Firebase on this Google Cloud project

ACCESS_TOKEN=$(gcloud auth print-access-token) #Make sure you are logged into gcloud

# Inspired by

echo "Creating new Firebase Hosting version:"

    curl --silent -H "Content-Type: application/json" \
        -H "Authorization: Bearer $ACCESS_TOKEN" \
        -d '{
            "config": {
                "rewrites": [{
                    "glob": "**",
                    "run": {
                        "serviceId": "'$CLOUD_RUN_SERVICE_NAME'",
                        "region": "'$CLOUD_RUN_SERVICE_REGION'"
        }' \$PROJECT_ID/versions \
| jq -r '.name')

echo "New version created: $version"

echo "Finalizing..."

curl --silent -H "Content-Type: application/json" \
        -H "Authorization: Bearer $ACCESS_TOKEN" \
        -X PATCH \
        -d '{"status": "FINALIZED"}' \$version?update_mask=status \
| jq '.status'

echo "Releasing..."

curl --silent -H "Authorization: Bearer $ACCESS_TOKEN" \
      -X POST$PROJECT_ID/releases?versionName=$version \
| jq '.version.status'

echo "Cloud Run service $CLOUD_RUN_SERVICE_NAME ($CLOUD_RUN_SERVICE_REGION) is serving behind Firebase Hosting at https://$"

echo "Set up a custom domain at$PROJECT_ID/hosting/sites"

Also find this script in a GitHub gist