Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

My env variables are not working in production (even while using PAYLOAD_PUBLIC_ prefix)

default discord avatar
benjick.10 months ago
7

and then supply the arg during build time



@170674545965924352

sure


ARG PAYLOAD_PUBLIC_MERLIN_URL=http://localhost:3000
ENV PAYLOAD_PUBLIC_MERLIN_URL=$PAYLOAD_PUBLIC_MERLIN_URL
  • default discord avatar
    augdust11 months ago

    do you have a example of integrating env vars in Dockerfile?

  • default discord avatar
    benjick.last year

    If you're using Docker you will need to provide args for the build step and modify the Dockerfile



    It's not really clear from this imo:

    https://payloadcms.com/docs/admin/environment-vars#environment-variables-in-admin-ui

    had to find this thread

  • default discord avatar
    mrkbrndlast year

    I guess I was looking at it the wrong way, I just looked at it as one build. But there is of course a separation in server and client build.



    Actually this reply sums it nice up:

    https://github.com/payloadcms/payload/issues/1654#issuecomment-1403972303
  • check out this thread:

    https://github.com/payloadcms/payload/issues/1654

    Can you point me to the docs that confused you? Maybe we need to clean a section up

  • default discord avatar
    mrkbrndlast year

    it just feels like this is not what the docs are trying me to tell



    When I add

    dotenv.config()

    it appears to be working

  • default discord avatar
    derosullast year

    I'm pretty sure the

    PAYLOAD_PUBLIC_

    env variables are being replaced when you build payload (probably somewhere in your CI pipeline) Perhaps you need to define the env variables you want to use for production in there as well 🙂

  • default discord avatar
    mrkbrndlast year

    So I want to hook up the preview button with a

    process.env

    variable. I looked around here for similar issues, and all seem to be solved by using the

    PAYLOAD_PUBLIC_

    prefix, which I already am using.



    admin: {
    ...
       preview: (doc) => {
          const page = doc as any as Home;
          
          let pathToUse = '';
          
          if (page.page) {
            pathToUse = page.page.slug === 'home' ? '' : page.page.slug;
          }
    
          return `${process.env.PAYLOAD_PUBLIC_WEB_URL}/api/preview? url=${process.env.PAYLOAD_PUBLIC_WEB_URL}/${pathToUse}`;
        },
    ...
    }


    This works in development mode, but gives me an

    undefined

    for each variable after building it.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.