fresh icon indicating copy to clipboard operation
fresh copied to clipboard

Can't get environment variables when prebuilding by Github worflow

Open ieiekk opened this issue 1 year ago • 6 comments

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/[email protected]/esnext/supabase-js.mjs:2:2168)
    at nt (https://esm.sh/v132/@supabase/[email protected]/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.

ieiekk avatar Oct 03 '23 10:10 ieiekk

Did you set up variables or secrets?

martinrempel avatar Oct 03 '23 21:10 martinrempel

@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

marvinhagemeister avatar Oct 04 '23 13:10 marvinhagemeister

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 avatar Oct 04 '23 15:10 ieiekk

@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 🙏

ieiekk avatar Oct 04 '23 15:10 ieiekk

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

avalero avatar Jul 08 '24 06:07 avalero