Home > other >  cypress-io/github-action does not accept all environment variables
cypress-io/github-action does not accept all environment variables

Time:10-19

my purpose is simple, run cypress e2e test using Github actions upon Pull Request. I used cypress-firebase for testing and all my test should run with Firebase Emulator. And I also used cypress-io/github-action for CI.

My problem is, when using cypress-io/github-action, I need to pass some environment variables for my react app to work with Firebase emulator, and all environment variables can not be recognized by the whole entire app. See my workflow file to understand.

Here are my related part of my Github action workflow file:

- name: Cypress run
  uses: cypress-io/github-action@v2
  with:
    browser: chrome
    headless: true
    record: true
    start: yarn run ci:start:emulator
    wait-on: "http://localhost:3000"
    wait-on-timeout: 300 # wait for 5 minutes
  env:
    # Add debugger
    # https://github.com/cypress-io/github-action#debugging
    DEBUG: "@cypress/github-action"
    FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
    PROJECT_ID: ${{ secrets.REACT_APP_PROJECT_ID }}
    FIREBASE_CONFIG: ${{ secrets.FIREBASE_CI_CLOUD_FUNCTIONS_CONFIG }}
    # Branch settings
    GITHUB_HEAD_REF: ${{ github.head_ref }}
    GITHUB_REF: ${{ github.ref }}
    # pass the Cypress Dashboard variables
    CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
    CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }}
    # UID of user to login during test
    CYPRESS_BASE_URL: http://localhost:3000
    CYPRESS_TEST_UID: ${{ secrets.CYPRESS_TEST_UID }}
    # Service Account (used for creating custom auth tokens)
    SERVICE_ACCOUNT: ${{ secrets.CYPRESS_SERVICE_ACCOUNT }}
    # Environment variables
    REACT_APP_ANY_KEY: ${{ secrets.REACT_APP_ANY_KEY }}

And here is my ci:start:emulator command in package.json file:

"ci:start:emulator": "firebase emulators:exec 'yarn start'"

The thing is yarn start command does not recognize my above REACT_APP_ANY_KEY environment variable. It seems cypress-io/github-action did not pass them to my yarn start command.

What I tried is changing above command into

"ci:start:emulator": "cross-env REACT_APP_ANY_KEY=<SOME_HARD_CODE_VALUE> firebase emulators:exec 'yarn start'"

It works perfectly! But of course, we don't want to pass tons of environment variables via this command like this.

Any help will be highly appreciated!

CodePudding user response:

According to the docs You can define environment variables for a step, job, or entire workflow, so here you defined those env variables only for this step Cypress run and not for the entire job, to solve this you should define env variables using this, an example:

jobs:
  job1:
    env:
    # Add debugger
    # https://github.com/cypress-io/github-action#debugging
    DEBUG: "@cypress/github-action"
    FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
    PROJECT_ID: ${{ secrets.REACT_APP_PROJECT_ID }}
    FIREBASE_CONFIG: ${{ secrets.FIREBASE_CI_CLOUD_FUNCTIONS_CONFIG }}
    # Branch settings
    GITHUB_HEAD_REF: ${{ github.head_ref }}
    GITHUB_REF: ${{ github.ref }}
    # pass the Cypress Dashboard variables
    CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
    CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }}
    # UID of user to login during test
    CYPRESS_BASE_URL: http://localhost:3000
    CYPRESS_TEST_UID: ${{ secrets.CYPRESS_TEST_UID }}
    # Service Account (used for creating custom auth tokens)
    SERVICE_ACCOUNT: ${{ secrets.CYPRESS_SERVICE_ACCOUNT }}
    # Environment variables
    REACT_APP_ANY_KEY: ${{ secrets.REACT_APP_ANY_KEY }}

env variables should be accessible across this Job's steps, if you wanted to set global environment variables, You should define them before the jobs.

  • Related