Environment variables not working with JS project

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:

name: Upload

on:
  release:
    types: [published]

jobs:
  upload:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
      - run: yarn
      - run: yarn build
        env:
          WEB_APP_URL: https://someurl.com/
          WELCOME_URL: https://someurl.com/
      - uses: JasonEtco/upload-to-release@v0.1.1
        with:
          args: chrome-extension.zip application/zip
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - uses: Klemensas/chrome-extension-upload-action@v1.3
        with:
          refresh-token: ${{ secrets.REFRESH_TOKEN }}
          client-id: ${{ secrets.CLIENT_ID }}
          client-secret: ${{ secrets.CLIENT_SECRET }}
          file-name: chrome-extension.zip
          app-id: ${{ secrets.APP_ID }}

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

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:

run: yarn build --all -u $BUILD_TOKEN
      env:
        BUILD_TOKEN: ${{ secrets.BUILD_TOKEN }}

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.

name: Env Test

on: push

jobs:
  upload:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
      - run: yarn
      - run: yarn build $WEB_APP_URL $WELCOME_URL
        env:
          WEB_APP_URL: https://example.com/
          WELCOME_URL: https://example.com/
      - uses: actions/upload-artifact@v1
        with:
          name: example
          path: example.zip

Am I missing something?

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.

- name: Run Tests (JEST)
        run: yarn test --ci --silent --testPathIgnorePatterns=experimental
        env:
          CI: true
          API_KEY: ${{ secrets.API_KEY }}

Please check your js file about the ENV. If the error still persists, please kinldy share a repo/sample repo for further investigation.

Thanks.

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.

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: https://github.com/breadadams/gh-action-env-trial/commit/5d673f3a55e54b14344995d6f5d5f039dc6f56d4