Cypress Github Actions 像 env-cmd 一样加载 env 文件

Dar*_*ert 0 environment-variables cypress env-file github-actions env-cmd

Github Action 类似功能的等效配置是什么,如下命令:

env-cmd -f .env cypress run --component
Run Code Online (Sandbox Code Playgroud)

我尝试将一个环境变量导入到 Github Action 中,但不起作用:

env:
  CYPRESS_PUBLIC_PATH: /public/
Run Code Online (Sandbox Code Playgroud)

因此,我更喜欢加载 env 文件并直接由应用程序/cypress 使用,而不是一一定义 env 变量。

我找不到任何可以在 Github Action 中加载一个 env 文件的文档。或者也许有一种方法可以在 Github Action 中运行上面完全相同的代码?

更新:我按照@Maddie.Squerciati的指示在Cypress配置上定义env文件,它在我的本地工作,但是github操作仍然无法识别/使用env文件。

这是我的 github 操作配置:

name: Cypress
uses: cypress-io/github-action@v5
with:
      config-file: cypress.config.js
      command: npm run cy:run-unit
      component: true
      record: false
      parallel: false
      browser: chrome
Run Code Online (Sandbox Code Playgroud)

Mad*_*ati 5

可以读到.env里面的内容cypress.config.js

您将安装dotenv作为开发依赖项,然后.envCypress 在启动时将读取该文件。

const { defineConfig } = require("cypress")

// read in .env file
const dotenv = require('dotenv')
const env = dotenv.config('./.env').parsed

module.exports = defineConfig({
  'e2e': {
    ...
  },
  env: {
    email: 'abc@123',   // example hard-coded var
    ...env,                        
  },
})
Run Code Online (Sandbox Code Playgroud)

或者如果没有硬编码变量

const { defineConfig } = require("cypress")

const dotenv = require('dotenv')
const env = dotenv.config('./.env').parsed  // read in .env file

module.exports = defineConfig({
  'e2e': {
    ...
  },
  env,
})
Run Code Online (Sandbox Code Playgroud)