英文:
Cypress Github Actions load env file like env-cmd
问题
GitHub Action中类似下面命令的等效配置是什么:
env-cmd -f .env cypress run --component
我尝试在GitHub Action中导入一个环境变量,但不起作用:
env:
CYPRESS_PUBLIC_PATH: /public/
所以,我更喜欢加载.env文件,然后由应用程序/Cypress直接使用它,而不是逐个定义环境变量。
我找不到任何关于如何在GitHub Action中加载.env文件的文档。或者也许有一种方法在GitHub Action中运行上面完全相同的代码吗?
更新:
我按照@Maddie.Squerciati的指示在Cypress配置中定义了.env文件,在我的本地工作正常,但GitHub Actions仍然不识别/使用.env文件。
以下是我的GitHub Action配置:
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
英文:
What is the equivalent configuration for Github Action similar function like the command below:
env-cmd -f .env cypress run --component
I tried with one env var imported into Github Action, but not working:
env:
CYPRESS_PUBLIC_PATH: /public/
So, instead of defining the env variables one by one, I prefer to load the env file and directly consumed by the application/cypress.
I can't find any documentation that will load one env file in Github Action. Or perhaps there is a way to run the exact same code above in Github Action?
Update:
I followed @Maddie.Squerciati's instruction to define env file on Cypress config, it works on my local, however github actions still doesn't recognize/use the env file.
Here is my github action config:
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
答案1
得分: 5
可以在cypress.config.js
内读取.env
文件。
您需要将dotenv安装为开发依赖项,然后Cypress将在启动时读取.env
文件。
const { defineConfig } = require("cypress")
// 读取 .env 文件
const dotenv = require('dotenv')
const env = dotenv.config('./.env').parsed
module.exports = defineConfig({
'e2e': {
...
},
env: {
email: 'abc@123', // 例子中的硬编码变量
...env,
},
})
或者如果没有硬编码变量:
const { defineConfig } = require("cypress")
const dotenv = require('dotenv')
const env = dotenv.config('./.env').parsed // 读取 .env 文件
module.exports = defineConfig({
'e2e': {
...
},
env,
})
英文:
It's possible to read the .env
inside cypress.config.js
.
You would install dotenv as a dev dependency, then the .env
file will be read by Cypress upon startup.
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,
},
})
or if there are no hard-coded vars
const { defineConfig } = require("cypress")
const dotenv = require('dotenv')
const env = dotenv.config('./.env').parsed // read in .env file
module.exports = defineConfig({
'e2e': {
...
},
env,
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论