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:
enable-fb-hosting.sh
#!/bin/bash
# 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
CLOUD_RUN_SERVICE_NAME="hello"
CLOUD_RUN_SERVICE_REGION="us-central1"
ACCESS_TOKEN=$(gcloud auth print-access-token) #Make sure you are logged into gcloud
# Inspired by https://firebase.google.com/docs/hosting/api-deploy
echo "Creating new Firebase Hosting version:"
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'"
}
}]
}
}' \
https://firebasehosting.googleapis.com/v1beta1/sites/$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"}' \
https://firebasehosting.googleapis.com/v1beta1/$version?update_mask=status \
| jq '.status'
echo "Releasing..."
curl --silent -H "Authorization: Bearer $ACCESS_TOKEN" \
-X POST https://firebasehosting.googleapis.com/v1beta1/sites/$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://$PROJECT_ID.web.app/"
echo "Set up a custom domain at https://console.firebase.google.com/project/$PROJECT_ID/hosting/sites"
Also find this script in a GitHub gist