Skip to content
TypeScript
3.3 KB
Raw Blame
---
title: Astro
description: Deploy an Astro site to Cloud Run with GCP.Website.Astro — server-rendered pages, prerendered pages, and Astro's own dev server under alchemy dev.
sidebar:
  order: 11
---

import PackageManagers from "../../../../components/PackageManagers.astro";

export const devDeps = "@alchemy.run/frontend-frameworks";

`GCP.Website.Astro` deploys an [Astro](https://astro.build) project to
Cloud Run. Pages render on the server by default. Pages that
`export const prerender = true`, and everything in `public/`, are
served as static files from the same container.

## Install

The build integration ships separately. It only runs at deploy time,
so a dev dependency is enough:

<PackageManagers dev={devDeps} />

Docker has to be running when you deploy. The image is built on your
machine.

## Deploy the site

```typescript
// alchemy.run.ts
import * as Alchemy from "alchemy";
import * as GCP from "@alchemy.run/gcp";
import * as Effect from "effect/Effect";

export const Website = GCP.Website.Astro("Website");

export default Alchemy.Stack(
  "MySite",
  { providers: GCP.providers(), state: GCP.state() },
  Effect.gen(function* () {
    const site = yield* Website;
    return { url: site.url };
  }),
);
```

Run `alchemy deploy`. `site.url` is the Cloud Run service URL.

## Pass environment variables

```diff lang="typescript"
// alchemy.run.ts
-export const Website = GCP.Website.Astro("Website");
+export const Website = GCP.Website.Astro("Website", {
+  env: {
+    GREETING: "Hello from Cloud Run!",
+  },
+});
```

Server-rendered pages read it from `process.env`:

```astro
---
// src/pages/index.astro
const greeting = process.env.GREETING ?? "hello";
---

<h1>{greeting}</h1>
```

## Configure Astro

Your `astro.config.*` loads as-is: integrations, Vite plugins, and
everything else. Don't add an adapter. Alchemy supplies its own.

```typescript
// astro.config.ts
import tailwindcss from "@tailwindcss/vite";
import { defineConfig } from "astro/config";

export default defineConfig({
  vite: { plugins: [tailwindcss()] },
});
```

The `astro` prop overrides a few options per deploy, such as `site`,
`base`, and `output`.

## Fully static sites

With `output: "static"` every page is built ahead of time:

```typescript
export const Website = GCP.Website.Astro("Website", {
  astro: { output: "static" },
  assets: { notFoundHandling: "404-page" },
});
```

`404-page` serves your built `404.html` for unknown paths.

## Tune Cloud Run

```typescript
export const Website = GCP.Website.Astro("Website", {
  scaling: { minInstanceCount: 1 },
  resources: { limits: { cpu: "1", memory: "1Gi" } },
});
```

One warm instance means no cold start for the first visitor. See
[Websites](/gcp/frontend/websites#tune-cloud-run) for the other
settings.

## Local development

`alchemy dev` runs the framework's dev server with hot reload and
creates nothing in the cloud. Wrap the site in `Alchemy.remote()` to
deploy it for real during dev.

## Example

[`examples/gcp-website-astro`](https://github.com/alchemy-run/alchemy/tree/main/examples/gcp-website-astro)
has a server-rendered page, a prerendered page, Tailwind, and a live
test.

## Where next

- [Websites](/gcp/frontend/websites) — the settings every site shares.
- [Website reference](/providers/gcp/website#astro) — every prop.