cypress-io/github-action 不接受所有环境变量

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

我的目的很简单,运行 赛普拉斯 e2e 测试使用 Github 对合并请求的操作。我用 cypress-firebase for testing and all my test should run with Firebase Emulator. And I also used cypress-io/github-action 代替 CI。

我的问题是,当使用 cypress-io/github-action 时,我需要为我的 React 应用程序传递一些环境变量才能与 Firebase 模拟器一起工作,而所有环境变量都无法被整个应用程序识别。查看我的工作流程文件以了解。

这是我的 Github 操作工作流文件的相关部分:

- 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 }}

这是我在 package.json 文件中的 ci:start:emulator 命令:

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

问题是 yarn start 命令无法识别我上面的 REACT_APP_ANY_KEY 环境变量。似乎 cypress-io/github-action 没有将它们传递给我的 yarn start 命令。

我尝试将上面的命令改成

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

效果很好!但当然,我们不想像这样通过此命令传递大量环境变量。

非常感谢任何帮助!

根据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,举例:

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 变量应该可以在这个 Job 的步骤中访问,如果你想设置全局环境变量,你应该在作业之前定义它们。