From aa652dc826907a70eae062c5d9084aa06dc2293e Mon Sep 17 00:00:00 2001 From: Lukas Trombach Date: Sun, 16 Jun 2024 12:05:01 +1200 Subject: [PATCH 1/3] migrate env variables to astro:env --- astro.config.mjs | 21 ++++++++++++++++++++- src/env.d.ts | 15 +-------------- src/pages/cv.pdf.ts | 10 +++++++--- src/utils/contact.ts | 7 +++++-- src/utils/contributionsCalendar.ts | 7 +++++-- src/utils/fetchHelper.ts | 5 +++-- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index 8b42d4e3..d307c15c 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,4 +1,4 @@ -import { defineConfig } from "astro/config"; +import { defineConfig, envField } from "astro/config"; import tailwind from "@astrojs/tailwind"; import svelte from "@astrojs/svelte"; import Icons from "unplugin-icons/vite"; @@ -70,5 +70,24 @@ export default defineConfig({ site: "https://www.lukastrombach.dev", experimental: { actions: true, + env: { + schema: { + GH_TOKEN: envField.string({ context: "server", access: "secret" }), + PUBLIC_VERCEL_URL: envField.string({ + context: "server", + access: "public", + optional: true, + }), + RESEND_TOKEN: envField.string({ context: "server", access: "secret" }), + PUBLIC_VERCEL_STORAGE_URL: envField.string({ + context: "server", + access: "public", + }), + PUBLIC_CV_FILE_NAME: envField.string({ + context: "server", + access: "public", + }), + }, + }, }, }); diff --git a/src/env.d.ts b/src/env.d.ts index c7aa3f05..050c6e6e 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -1,22 +1,9 @@ +/// /// /// /// /// -interface ImportMetaEnv { - GH_TOKEN: string; - VERCEL_ENV?: "production" | "preview" | "development"; - VERCEL_URL?: string; - RESEND_TOKEN?: string; - ISR_BYPASS_TOKEN?: string; - VERCEL_STORAGE_URL?: string; - CV_FILE_NAME?: string; -} - -interface ImportMeta { - readonly env: ImportMetaEnv; -} - // Workaround for icon type checking declare module "icons:astro/*" { const component: ( diff --git a/src/pages/cv.pdf.ts b/src/pages/cv.pdf.ts index be01b8cc..02880f05 100644 --- a/src/pages/cv.pdf.ts +++ b/src/pages/cv.pdf.ts @@ -1,18 +1,22 @@ import type { APIRoute } from "astro"; +import { + PUBLIC_VERCEL_STORAGE_URL, + PUBLIC_CV_FILE_NAME, +} from "astro:env/server"; export const prerender = false; export const GET: APIRoute = async () => { - if (!import.meta.env.VERCEL_STORAGE_URL) { + if (!PUBLIC_VERCEL_STORAGE_URL) { return new Response("Vercel storage URL not set", { status: 500 }); } - if (!import.meta.env.CV_FILE_NAME) { + if (!PUBLIC_CV_FILE_NAME) { return new Response("Missing CV file name", { status: 500 }); } const file = await fetch( - new URL(import.meta.env.CV_FILE_NAME, import.meta.env.VERCEL_STORAGE_URL), + new URL(PUBLIC_CV_FILE_NAME, PUBLIC_VERCEL_STORAGE_URL), ); if (file.ok) { diff --git a/src/utils/contact.ts b/src/utils/contact.ts index e997de13..8d632414 100644 --- a/src/utils/contact.ts +++ b/src/utils/contact.ts @@ -1,4 +1,5 @@ import { ActionError } from "astro:actions"; +import { getSecret } from "astro:env/server"; import { Resend } from "resend"; export default async function sendEmail({ @@ -10,7 +11,9 @@ export default async function sendEmail({ email: string; message: string; }) { - if (!import.meta.env.RESEND_TOKEN) { + const RESEND_TOKEN = getSecret("RESEND_TOKEN"); + + if (!RESEND_TOKEN) { const message = "Missing token"; console.error(message); @@ -20,7 +23,7 @@ export default async function sendEmail({ }); } - const resend = new Resend(import.meta.env.RESEND_TOKEN); + const resend = new Resend(RESEND_TOKEN); const response = await resend.emails.send({ from: "homepage-contact@lukastrombach.dev", to: ["contact@lukastrombach.dev"], diff --git a/src/utils/contributionsCalendar.ts b/src/utils/contributionsCalendar.ts index f31d158f..9ed09655 100644 --- a/src/utils/contributionsCalendar.ts +++ b/src/utils/contributionsCalendar.ts @@ -1,5 +1,6 @@ import { z } from "astro/zod"; import fetch from "./fetchHelper"; +import { getSecret } from "astro:env/server"; const GH_API = "https://api.github.com/graphql"; const USERNAME = "Trombach"; @@ -44,7 +45,9 @@ export const schema = z.object({ }); export default async function getGithubContributions() { - if (!import.meta.env.GH_TOKEN) { + const GH_TOKEN = getSecret("GH_TOKEN"); + + if (!GH_TOKEN) { throw new Error("Missing auth token."); } @@ -52,7 +55,7 @@ export default async function getGithubContributions() { method: "POST", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${import.meta.env.GH_TOKEN}`, + Authorization: `Bearer ${GH_TOKEN}`, }, body: JSON.stringify({ query: QUERY, variables: { userName: USERNAME } }), }); diff --git a/src/utils/fetchHelper.ts b/src/utils/fetchHelper.ts index be0acd87..b74798b2 100644 --- a/src/utils/fetchHelper.ts +++ b/src/utils/fetchHelper.ts @@ -1,6 +1,7 @@ /* global URL RequestInit fetch URLSearchParams console */ import type { z } from "astro/zod"; +import { PUBLIC_VERCEL_URL } from "astro:env/server"; export default async function fetchWithSchema( schema: S, @@ -32,8 +33,8 @@ function getURL(url: URL | string, searchParams?: { [key: string]: string }) { if (typeof url === "string" && url.startsWith("http")) { url = new URL(url); } else { - const base = import.meta.env.VERCEL_URL - ? `https://${import.meta.env.VERCEL_URL}` + const base = PUBLIC_VERCEL_URL + ? `https://${PUBLIC_VERCEL_URL}` : "http://localhost:4321"; url = new URL(url, base); } From e174680b514994f3935a746774f6205999dc6fdc Mon Sep 17 00:00:00 2001 From: Lukas Trombach Date: Sun, 16 Jun 2024 12:59:31 +1200 Subject: [PATCH 2/3] add env variables to PR check --- .github/workflows/pr-checks.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index e7b539fc..63a96f41 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -11,6 +11,9 @@ on: jobs: Typescript: runs-on: ubuntu-latest + env: + PUBLIC_VERCEL_STORAGE_URL: ${{ vars.PUBLIC_VERCEL_STORAGE_URL }} + PUBLIC_CV_FILE_NAME: ${{ vars.PUBLIC_CV_FILE_NAME }} steps: - uses: actions/checkout@v4 - run: corepack enable From 1d0ba373376d8edfe70c03d00c1f33559b44464a Mon Sep 17 00:00:00 2001 From: Lukas Date: Sun, 16 Jun 2024 13:00:06 +1200 Subject: [PATCH 3/3] (chore): add changeset --- .changeset/wicked-bananas-shout.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wicked-bananas-shout.md diff --git a/.changeset/wicked-bananas-shout.md b/.changeset/wicked-bananas-shout.md new file mode 100644 index 00000000..b023fd72 --- /dev/null +++ b/.changeset/wicked-bananas-shout.md @@ -0,0 +1,5 @@ +--- +"homepage": minor +--- + +migrate env variables to astro:env