khromov / self-hosted-sveltekit-demo

17 stars 5 forks source link

How to use env variables from caprover app? #3

Closed anhtuank7c closed 9 months ago

anhtuank7c commented 10 months ago

Hi Khromov,

Have a nice day.

Thank you so much for bring up a very nice tutorial.

I would like to reuse Caprover Environment variables instead of defining a new .env file as local environment.

How can I achieve this?

Without .env file Sveltekit will not able to build since I imported these variables somewhere in my code.

Thank you so much.

AristideBH commented 9 months ago

Hello, I'd also love to find a solution for this ! I've got some Public and Private environement variables that my Sveltekit app needs in order to build the Docker image.

Is there a way to serve securely them while the 'Publish Docker Image' action is running ?

I saw that matootie/github-docker action allows for build arguments, but I guess we would have to then use them in the DockerFile and store the values in Github Secrets ? I am a bit lost there :/

khromov commented 9 months ago

Hi! To add environment variables at build time it should work just as @AristideBH mentioned - add buildArgs config to the build.yml workflow. It'd look something like this:

#  inside matootie/github-docker step
buildArgs: |
      API_KEY=${{ secrets.API_KEY }}
      PUBLIC_NOT_SECRET="not secret variable"

You add the secrets the same way as described in the video tutorial for the CapRover deploy key.

Then you can consume them in SvelteKit using $env/static/public for the PUBLIC_ prefixed vars and $env/static/private for the private vars.

khromov commented 9 months ago

To clarify the initial question, there is no way to use environment variables from CapRover during the Docker build, you'll have to duplicate the env vars you need at build time. However, I suggest using dynamic env vars from $env/dynamic/* as those can be managed in CapRover and don't require a full app rebuild every time you want to change them.

AristideBH commented 9 months ago
#  inside matootie/github-docker step
buildArgs: |
      API_KEY=${{ secrets.API_KEY }}
      PUBLIC_NOT_SECRET="not secret variable"

Hi Stanislas, and thanks for your answer 👍 However, I just tried it with a two public variables, and I still got the error telling me that no variable are exported :

...
#13 2.036 vite v5.1.3 building SSR bundle for production...
#13 2.107 transforming...
#13 14.32 ✓ 2116 modules transformed.
#13 14.32 x Build failed in 12.25s
#13 14.32 error during build:
#13 14.32 RollupError: "PUBLIC_URL" is not exported by "virtual:$env/static/public", imported by ...
...

Is it something you would have more experience on ? Thanks in advance

khromov commented 9 months ago

@AristideBH Hi, I added a better example of using env vars in the repo. Check the workflow: https://github.com/khromov/self-hosted-sveltekit-demo/blob/main/.github/workflows/build.yml#L20

The missing part on your end is that you also need to add ARG PUBLIC_URL inside your Dockerfile: https://github.com/khromov/self-hosted-sveltekit-demo/blob/main/Dockerfile#L5

Also of course you need to add PUBLIC_URL inside buildArgs, not the example vars I posted.

AristideBH commented 9 months ago

Thanks again Stanislas ! That did the trick to pass the env variables in the build ! 🎊

I however still encounter a problem at the 'Publish Docker Image' stage.

------
 > [stage-1 7/7] COPY --from=sk-build /usr/src/app/build /usr/src/app/build:
------
Dockerfile:23
--------------------
  22 |     COPY --from=sk-build /usr/src/app/package-lock.json /usr/src/app/package-lock.json
  23 | >>> COPY --from=sk-build /usr/src/app/build /usr/src/app/build
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 625296d6-9be1-4356-a58b-91c3a8df5f0b::jbf059lhqsci6mtff1l8sm5a7: "/usr/src/app/build": not found

It that's not too much to ask, shall I open another issue to give more details, and not polute this post ?

anhtuank7c commented 9 months ago

To clarify the initial question, there is no way to use environment variables from CapRover during the Docker build, you'll have to duplicate the env vars you need at build time. However, I suggest using dynamic env vars from $env/dynamic/* as those can be managed in CapRover and don't require a full app rebuild every time you want to change them.

Thank you so much