← Back to Home

Using environment variables in Astro when deploying to Cloudflare

tags: astro, cloudflare, astro ssg, astro ssr, env variables

Overview

In this article we discuss handling environment variables in Astro projects when Deploying to Cloudflare workers
using Github integration with Cloudflare to have auto deployment of your site on on pushing to a branch
this also will act as a way to have multiple workers for different branches each will have its own envitonment variables (dev, stagin, production)

So to list what we need to achieve:


Demo Project

find here a demo project on Github
demo github repo

this Astro project has main configurations needed to deploy to Cloudflare worker:

astro.config.mjs
// @ts-check
import { defineConfig } from "astro/config";
import cloudflare from "@astrojs/cloudflare";
// some code ...
// https://astro.build/config
export default defineConfig({
output: "server",
adapter: cloudflare()
// some code ...
});


Accessing environment variables

there are multiple scenarios where we need to access any environment variables:


For astro.config.mjs

we use loadEnv from 'vite'

astro.config.mjs
// @ts-check
import { defineConfig } from "astro/config";
import cloudflare from "@astrojs/cloudflare";
import { loadEnv } from "vite";
const { SECRET_VAR_ONE } = loadEnv(
process.env.TEST_SECRET || "",
process.cwd(),
""
);
console.log("SECRET_VAR_ONE in astro.config.mjs", SECRET_VAR_ONE);
// https://astro.build/config
export default defineConfig({
output: "server",
adapter: cloudflare()
// somde code ...
});

then we can use the environment variables in any needed actions such as:


For SSG pages we can use two methods

astro.config.mjs
// @ts-check
import { defineConfig, envField } from "astro/config";
import cloudflare from "@astrojs/cloudflare";
import { loadEnv } from "vite";
const { SECRET_VAR_ONE } = loadEnv(
process.env.TEST_SECRET || "",
process.cwd(),
""
);
console.log("SECRET_VAR_ONE in astro.config.mjs", SECRET_VAR_ONE);
// https://astro.build/config
export default defineConfig({
output: "server",
adapter: cloudflare(),
env: {
schema: {
SECRET_VAR_ONE: envField.string({ context: "server", access: "secret" }),
},
},
});

For SSR Pages we can use two methods:

for getEnv() function:

helpers/getEnv.ts
export const getEnv = (ASTRO_GLOBAL:any) =>{
const AstoEnv = ASTRO_GLOBAL?.locals?.runtime?.env;
return {
SITE_TITLE: AstoEnv?.SITE_TITLE || import.meta.env.SITE_TITLE,
};
}
somepage.astro
---
// some code
import { getEnv } from "@/helpers/getEnv";
const envHelper = getEnv(Astro).SECRET_VAR_ONE;
// some code
---
<p>
Build time environment variable (using getEnv helper this is the same as
Astro.locals but moved to a helper function)<br />
env variable: {envHelper || "not found"}
</p>

for astro:env/server:

somepage.astro
---
// some code
import { getSecret } from "astro:env/server";
const envGetSecret = getSecret("SECRET_VAR_ONE");
// some code
---
<p>
Build time environment variable (using getSecret from astro:env/server)<br />
env variable: {envGetSecret}
</p>

Some points regarding Cloudflare setup: