Skip to content

A boilerplate to deploy Sapper (Svelte) applications to Cloud Run (https://cloud.run)

License

Notifications You must be signed in to change notification settings

mootoday/sapper-on-cloud-run

Repository files navigation

Gitpod Ready-to-Code

sapper-on-cloud-run

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/.

Blog posts

Two corresponding blog posts are available with details:

Docker local testing

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.

Set up the Artifact Registry

# 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

Set up Cloud Build

# 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

Set up Cloud Run

# 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.

Deploy to Cloud Run

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:

  1. Merge a pull request into the master branch.
  2. The Cloud Build GitHub app triggers Cloud Build to:
    1. Build the docker image
    2. Push the docker image to Artifact Registry
    3. Deploy the image to Cloud Run

Mapping a custom domain

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.