如何将 Bitbucket 管道存储库变量传递给 npm run 脚本?
如何将 Bitbucket 存储库变量传递到我在管道中使用 npm run
运行的 Cypress.io 测试脚本中?
管道和测试工作正常,但我无法将 Bitbucket 变量放入测试文件本身。我可以按照 Bitbucket 在存储库变量页面上提供的说明从 bitbucket-pipeline.yml
访问存储库变量,但无法访问 cypress/integration/example.js< 内部的变量/代码>。我想存储测试脚本用作 Bitbucket 存储库变量的凭据。
这是我的代码...
bitbucket-pipeline.yml
image: cypress/included:3.2.0
pipelines:
custom:
robot:
- step:
script:
- printenv
- npm ci
- npm run e2e
- 使用 Cypress 提供的图像
- 我可以通过
printenv
package.json
{
...
"scripts": {
"e2e": "cypress run"
},
...
"dependencies": {
"cypress": "^9.4.1"
}
}
< strong>cypress/integration/example.js
describe('A', () => {
it('should B', () => {
cy.visit('https://google.com');
});
});
- 我想在
it('should B' ...)
方法中使用 Bitbucket 存储库变量。
预先感谢您的帮助。
How do I pass a Bitbucket repository variable into the Cypress.io test script that I am running with npm run
in a pipeline?
The pipeline and tests work, but I am unable to get a Bitbucket variable into the test file iteself. I can access the respository variable from bitbucket-pipeline.yml
following the instructions provided by Bitbucket on the repository variable page, but I cannot access the variable inside of cypress/integration/example.js
. I want to store credentials the test scripts use as Bitbucket repository variables.
Here's my code...
bitbucket-pipeline.yml
image: cypress/included:3.2.0
pipelines:
custom:
robot:
- step:
script:
- printenv
- npm ci
- npm run e2e
- uses an image provided by Cypress
- I can see my repository variables via
printenv
package.json
{
...
"scripts": {
"e2e": "cypress run"
},
...
"dependencies": {
"cypress": "^9.4.1"
}
}
cypress/integration/example.js
describe('A', () => {
it('should B', () => {
cy.visit('https://google.com');
});
});
- I want to use the Bitbucket repository variable inside of the
it('should B' ...)
method.
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了 Cypress.io 关于环境变量的文档:
https://docs.cypress.io/guides/guides/environment-variables
bitbucket-pipeline.yml
其中 example 是 Bitbucket 存储库变量的名称。可能区分大小写。
添加 $ 以引用
bitbucket-pipeline.yml
文件中的 Bitbucket 存储库变量。使用 CYPRESS_ 将其标识为 Cypress 环境变量。
https://docs.cypress.io/guides/guides/ environment-variables#Option-3-CYPRESS_
然后你可以通过 Cypress.env("example") 在测试规范中使用它
我实际上必须进行类型转换,因为 Bitbucket 提供了意外的数据类型...
I found the Cypress.io documentation on environment variables:
https://docs.cypress.io/guides/guides/environment-variables
bitbucket-pipeline.yml
Where example is the name of the Bitbucket repo variable. Might be case sensitive.
Add $ to reference the Bitbucket repository variable in the
bitbucket-pipeline.yml
file.Use CYPRESS_ to identify it as a Cypress environment variable.
https://docs.cypress.io/guides/guides/environment-variables#Option-3-CYPRESS_
Then you can use it in the test spec via
Cypress.env("example")
I actually had to typecast because Bitbucket was providing unexpected data types...
您可以使用该插件为每个部署提供一个配置文件,并且在运行脚本时,您可以设置运行脚本的环境,例如,如果插件在管道内正确设置,则下面的示例将起作用。
npx cypress run --env version =“qa”
npx cypress run --env version =“prod”
You can use the plugin to have a profiles for each deployment and when run the script you can set which env to run the script for example below would work if plugin setup properly inside the pipeline.
npx cypress run --env version="qa"
npx cypress run --env version="prod"