A boilerplate to deploy Sapper (Svelte) applications to Cloud Run (https://cloud.run)
A demo is available at https://sapper-on-cloud-run.mikenikles.com/.
Two corresponding blog posts are available with details:
- Sapper, Google Cloud Run, Continuous Deployment - A boilerplate template
- Firebase Hosting for static assets of a Sapper web app on Cloud Run
- Also check PR #5
The following NPM scripts assist with testing the container image locally:
npm run dev:docker:build
: Builds the docker image.npm run dev:docker:run
: Runs the docker image locally on port 3000.
# Enable the Artifact Registry API
gcloud services enable artifactregistry.googleapis.com
# Create an Artifact Registry repository to host docker images
gcloud beta artifacts repositories create docker-repository --repository-format=docker \
--location=us-central1
# Enable the Cloud Build API
gcloud services enable cloudbuild.googleapis.com
# Create a build trigger
gcloud beta builds triggers create github \
--repo-name=sapper-on-cloud-run \
--repo-owner=mikenikles \
--branch-pattern="^master$" \
--build-config=cloudbuild.yaml
# Obtain the numeric project ID
# Use: gcloud projects list --filter="$(gcloud config get-value project)" --format="value(PROJECT_NUMBER)"
# Grant the Cloud Run Admin role
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member "serviceAccount:[email protected]" \
--role roles/run.admin
# Grant access to Cloud Build to deploy to Cloud Run
gcloud iam service-accounts add-iam-policy-binding \
[email protected] \
--member="serviceAccount:[email protected]" \
--role="roles/iam.serviceAccountUser"
Note: If you have not enabled compute engine, you will see an error like:
Gaia id not found for email [email protected]
where PROJECT_NUMBER is the actual number. To solve this, simply visit the compute engine service and allow the initial setup to complete. This will create the service account called [email protected] and after that, the iam command will run successfully.
Cloud Run (https://cloud.run) is a fully managed serverless compute platform that automatically scales your stateless containers.
The continuous deployment pipeline works as follows:
- Merge a pull request into the
master
branch. - The Cloud Build GitHub app triggers Cloud Build to:
- Build the docker image
- Push the docker image to Artifact Registry
- Deploy the image to Cloud Run
Details on how to verify a domain can be found in the documentation.
Once a domain is verified, the following command maps it to a Cloud Run service:
gcloud beta run domain-mappings create \
--service sapper-on-cloud-run \
--domain sapper-on-cloud-run.mikenikles.com \
--region us-central1 \
--platform managed
Add a CNAME record with name sapper-on-cloud-run
and contents ghs.googlehosted.com
to your DNS.