Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Environment variables not recognized in Cloud Run

Hello,

I have a NextJs project and am trying to deploy the frontend in Cloud Run. 

My environment variables aren't being recognized at runtime. The only way I can get them to work is by removing .env from my .gitignore which I'd prefer not to do. 

I can see that the variables are set in the revision:

jsnyder_0-1682906978252.png

I am logging process.env.TEST to console and it's returning the undefined result. The same goes for the other variables (except NODE_ENV which is set as production by default). 

jsnyder_1-1682907064013.png

NEXT_PUBLIC_BASE_URL is undefined while you can see it is set in the first screenshot, as is TEST.

Everything else in the app appears to be working correctly, but I can't work with my backend since the URL isn't coming through in the environment variable. 

I've tried redeploying on github, redeploying the revisions, and nothing seems to be working. 

Any ideas on how I could troubleshoot this further? 

Thank you!

1 1 6,142
1 REPLY 1

Hi @jsnyder ,

Welcome to the Google Cloud Community!

You can try the following troubleshoot options:

  1. Use @beam-australia/react-env to fetch ENV variables . You can refer to this documentation.
  2. Use getServerSideProps to get the ENV variables and put them in the client. Reference 1 Reference 2.

Let me know if it helped, thanks!