Skip to content

Instantly share code, notes, and snippets.

@steren
Last active March 26, 2022 19:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steren/03d3e58c58c9a53fd49bb78f58541872 to your computer and use it in GitHub Desktop.
Save steren/03d3e58c58c9a53fd49bb78f58541872 to your computer and use it in GitHub Desktop.
Set up Firebase Hosting in front of a Cloud Run service
#!/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 behing 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment