Environment variables not working with JS project #25930
-
I’m not sure if this is really the best place to post this, but going to do so anyway in case it helps anyone else facing the same issue. So we have a JS project, and a github workflow that runs a build script, creates a zip, and uploads it to the Chrome web store. All parts of this have been working fine until we introduced environment variables. When we run build locally process.env.X is replaced correctly with the contents of our .env file, however in the files generated from the action it displays as e.env.X (and the application doesn’t work). We are using dotenv via the dotenv-webpack plugin. Maybe we need to set the path to the env file with this, if so, what would said path be? I looked into it but didn’t find any information. Here’s the action file:
Note that the variables for the upload-to-release & chrome-extension-upload-action steps work fine, it’s just those on the yarn build command. If I can share anything else that would be of use to debug this let me know! Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Command ‘yarn build’ is not an existing command by default, ‘build’ is a command which can be specified in your package.json file on the scripts property. Typically we can use env in workflow like below:
|
Beta Was this translation helpful? Give feedback.
-
Hi @weide-zhou, thanks for the response, and yes I’m aware that build isn’t a default command/script. I’m not too sure about the “–all -u” flags in your example, is that something specific to your build? (I can’t seem to find those flags in the yarn docs). Anyway, so I’ve tried your suggestion with the following workflow yaml, but still the zip file that’s generated is missing the env variables like I described in my initial post.
Am I missing something? |
Beta Was this translation helpful? Give feedback.
-
Hi @breadadams , Thanks for your reply! Please ignore ‘–all -u’ flags. After further investigation, ‘yarn build’ should be able to accept env values. Sample as below, In Node.js you can access it via process.env.API_KEY. More details here.
Please check your js file about the ENV. If the error still persists, please kinldy share a repo/sample repo for further investigation. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Hey @weide-zhou, excuse the delay, not been able to prepare a sample repo. I’ve stripped down the code & dependencies to absolute basics from our project and created this repo: https://github.com/breadadams/gh-action-env-trial The instructions in the README should be clear, let me know if you have any questions about running it. |
Beta Was this translation helpful? Give feedback.
-
The issue was with our configuration for the dotenv-webpack plugin, given that the environment variables aren’t coming from a real |
Beta Was this translation helpful? Give feedback.
The issue was with our configuration for the dotenv-webpack plugin, given that the environment variables aren’t coming from a real
.env
file we needed to enable the systemvars setting, as seen here: breadadams/gh-action-env-trial@5d673f3