[英]GitHub Action [cypress CI integration ]: How to set env variables before running cypress run command
I am writing workflows for my repository, and I want to run test cases whenever there is a code push.我正在为我的存储库编写工作流,并且我想在有代码推送时运行测试用例。
Here is the code snippet from .yml file:这是 .yml 文件中的代码片段:
- name: Run cypress test
with:
env: true
env:
username: ${{secrets.CYPRESS_USERNAME}}
password: ${{secrets.CYPRESS_PASSWORD}}
run: npm run cy:test --env fileConfig=production, username=$username, password=$password
continue-on-error: false
Snippet from JSON :来自 JSON 的片段:
{
"env": {
"userId": "1",
"environment": "production",
"baseUrl": " base URL"
}
}
So, I want to pass the username and password along with the configfile in the cypress run command so that they can be set as the env variable because I am using username and password for my login test module.因此,我想在 cypress run 命令中将用户名和密码与 configfile 一起传递,以便将它们设置为 env 变量,因为我在登录测试模块中使用了用户名和密码。
With the above code i am getting error :使用上面的代码,我收到错误:
The workflow is not valid.工作流无效。 .github/workflows/main.yml (Line: 43, Col: 9): Unexpected value 'run' .github/workflows/main.yml (Line: 36, Col: 9): Required property is missing: uses
.github/workflows/main.yml (Line: 43, Col: 9): Unexpected value 'run' .github/workflows/main.yml (Line: 36, Col: 9): required property is missing: uses
Thanks :)谢谢 :)
First, you need to add the env variables as empty strings on cypress.json :首先,您需要在cypress.json上将 env 变量添加为空字符串:
{
"env": {
"userId": "1",
"environment": "production",
"baseUrl": "base URL",
"username": "",
"password": ""
}
}
In main.yml file, you need to add the CYPRESS_ prefix to the variables:在 main.yml 文件中,您需要为变量添加 CYPRESS_ 前缀:
env:
CYPRESS_username: ${{secrets.CYPRESS_USERNAME}}
CYPRESS_password: ${{secrets.CYPRESS_PASSWORD}}
Also, you should use the setup that can be found on cypress docs ( https://docs.cypress.io/guides/continuous-integration/github-actions#Basic-Setup ), example:此外,您应该使用可以在 cypress docs ( https://docs.cypress.io/guides/continuous-integration/github-actions#Basic-Setup ) 上找到的设置,例如:
name: Cypress Tests
on: [push]
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
# Install NPM dependencies, cache them correctly
# and run all Cypress tests
- name: Cypress run
uses: cypress-io/github-action@v2
with:
build: npm run build
start: npm start
env:
CYPRESS_username: ${{secrets.CYPRESS_USERNAME}}
CYPRESS_password: ${{secrets.CYPRESS_PASSWORD}}
You should also read this article: https://glebbahmutov.com/blog/keep-passwords-secret-in-e2e-tests/您还应该阅读这篇文章: https : //glebbahmutov.com/blog/keep-passwords-secret-in-e2e-tests/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.