Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 4

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

3 Replies
Highlighted
GitHub Partner
Message 2 of 4

Re: Environment variables not working with JS project

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 }}
Highlighted
Copilot Lvl 3
Message 3 of 4

Re: Environment variables not working with JS project

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?

Highlighted
GitHub Partner
Message 4 of 4

Re: Environment variables not working with JS project

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.