Skip to content

Commit

Permalink
Merge pull request #269 from Trombach/feature/astro-env
Browse files Browse the repository at this point in the history
migrate env variables to astro:env
  • Loading branch information
Trombach committed Jun 16, 2024
2 parents a9b66a2 + 1d0ba37 commit 07fc879
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-bananas-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"homepage": minor
---

migrate env variables to astro:env
3 changes: 3 additions & 0 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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",
}),
},
},
},
});
15 changes: 1 addition & 14 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
/// <reference path="../.astro/env.d.ts" />
/// <reference path="../.astro/types.d.ts" />
/// <reference path="../.astro/actions.d.ts" />
/// <reference types="astro/client" />
/// <reference types="svelte-gestures" />

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: (
Expand Down
10 changes: 7 additions & 3 deletions src/pages/cv.pdf.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions src/utils/contact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ActionError } from "astro:actions";
import { getSecret } from "astro:env/server";
import { Resend } from "resend";

export default async function sendEmail({
Expand All @@ -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);

Expand All @@ -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"],
Expand Down
7 changes: 5 additions & 2 deletions src/utils/contributionsCalendar.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -44,15 +45,17 @@ 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.");
}

return await fetch(schema, GH_API, {
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 } }),
});
Expand Down
5 changes: 3 additions & 2 deletions src/utils/fetchHelper.ts
Original file line number Diff line number Diff line change
@@ -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<S extends z.ZodTypeAny>(
schema: S,
Expand Down Expand Up @@ -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);
}
Expand Down

1 comment on commit 07fc879

@vercel
Copy link

@vercel vercel bot commented on 07fc879 Jun 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

astro-homepage – ./

astro-homepage-git-main-trombachs-projects.vercel.app
astro-homepage-trombachs-projects.vercel.app

Please sign in to comment.