denoland / fresh

The next-gen web framework.
https://fresh.deno.dev
MIT License
12.14k stars 616 forks source link

Can't get environment variables when prebuilding by Github worflow #1843

Open ieiekk opened 10 months ago

ieiekk commented 10 months ago

This is what I wrote in utils.ts:

export const supabase = createClient<Database>(
  Deno.env.get("SUPABASE_URL") as string,
  Deno.env.get("SUPABASE_KEY") as string,
);

After I push the commit, I got this error message on GIthub workflow page:

Build step >
Run deno task build
Task build deno run -A dev.ts build
.
.
.
.
error: Uncaught (in promise) Error: supabaseUrl is required.
    at new p (https://esm.sh/v132/@supabase/supabase-js@2.4.0/esnext/supabase-js.mjs:2:2168)
    at nt (https://esm.sh/v132/@supabase/supabase-js@2.4.0/esnext/supabase-js.mjs:2:5286)
    at file:///home/runner/work/a-pp-greeting-cards-platform/a-pp-greeting-cards-platform/utils/utils.ts:20:25
Error: Process completed with exit code 1.

And I've setup the env var on deno deploy dashboard and this works on local .env file.

martinrempel commented 10 months ago

Did you set up variables or secrets?

marvinhagemeister commented 10 months ago

@ieiekk Do you happen to use a setup with a fresh.config.ts or without one? Asking because before we introduced fresh.config.ts the dev.ts would always load main.ts which in turn imports every file of the application. In doing so it will encounter the supabase call and complain that the environment variables are not set. That's the main reason we introduced the fresh.config.ts which allows us to properly split the development mode internally so that you can create a build without loading main.ts

ieiekk commented 10 months ago

Did you set up variables or secrets?

This works! I add SUPABASE_URL and SUPABASE_KEY as secrets:

Screenshot 2023-10-04 at 11 40 42 PM

And add these lines in my deploy.yml:

name: Deploy
on:
  push:
    branches: [master, develope]
  pull_request:
    branches: master

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest

    permissions:
      id-token: write
      contents: read

    steps:
      - name: Clone repository
        uses: actions/checkout@v3

      - name: Install Deno
        uses: denoland/setup-deno@v1
        with:
          deno-version: v1.x

      - name: Build step
+        env: 
+            SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
+            SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
        run: "deno task build"

      - name: Upload to Deno Deploy
        uses: denoland/deployctl@v1
        with:
          project: "pp-greeting-cards-platform"
          entrypoint: "./main.ts"

Hope this helps anyone encountered the same issue.

ieiekk commented 10 months ago

@ieiekk Do you happen to use a setup with a fresh.config.ts or without one? Asking because before we introduced fresh.config.ts the dev.ts would always load main.ts which in turn imports every file of the application. In doing so it will encounter the supabase call and complain that the environment variables are not set. That's the main reason we introduced the fresh.config.ts which allows us to properly split the development mode internally so that you can create a build without loading main.ts

I leave that file as default:

import { defineConfig } from "$fresh/server.ts";
import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "./twind.config.ts";
export default defineConfig({
  plugins: [twindPlugin(twindConfig)],
});

So is there a way to avoid import some files during build time? Thanks in advance 🙏

avalero commented 1 month ago

Same here, no way to make the build step in github actions without env variables. That is somehow unexpected as they are server side variables @marvinhagemeister

JakeAve commented 20 hours ago

I think there needs to be a way to know which variables will be needed. The only way I could verify which variables are needed for the build to not crash was by renaming my local .env and trying a local build. It appears the secrets are overwritten once the server is running and the environment variables are loaded in anyway.