我的目的很简单,在Pull Request上使用Github动作运行cypress e2e测试。我使用cypress-firebase进行测试,我的所有测试都应该在Firebase模拟器上运行。我还使用了cypress-io/github-action for 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'"
它工作完美!当然,我们不希望像这样通过这个命令传递大量的环境变量。
任何帮助将非常感激!
根据文档You can define environment variables for a step, job, or entire workflow
,所以这里您只定义了这一步Cypress run
的环境变量,而不是整个作业,要解决这个问题,您应该使用以下方法定义环境变量,一个例子:
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 }}
环境变量应该可以在这个Job
的步骤中访问,如果您想设置全局环境变量,您应该在作业之前定义它们。