如何消除使用几乎相同阶段的“.gitlab-ci.yml”脚本中的代码重复?
我有一个执行 2 个测试脚本的 gitlab-ci/cd.yaml 文件。正如你所看到的,有很多重复发生。事实上,除了“脚本”值之外,两个阶段都是相同的。
对于smoke-suite,值为
- npm run docker_smoke --single-run --progress false
对于regression-suite,值为
- npm run docker_regression --single-run --progress false
image: node-karma-protractor
stages:
- suiteSmoke
- suiteRegression
before_script:
- npm install
# Smoke suite =================================
smoke_suite:
stage: suiteSmoke
tags:
- docker-in-docker
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
script:
- npm run docker_smoke --single-run --progress false
retry: 1
#saving the HTML-Report
artifacts:
when: on_failure
paths:
- reporting/
expire_in: 1 week
allow_failure: true
# Regression suite ============================
regression_suite:
stage: suiteRegression
tags:
- docker-in-docker
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
script:
- npm run docker_regression --single-run --progress false
retry: 1
#saving the HTML-Report
artifacts:
when: on_failure
paths:
- reporting/
expire_in: 1 week
allow_failure: true
该脚本需要遵守以下规则:
- 测试需要连续运行(因为如果同时执行它们可能会出现问题)。不过,执行顺序并不重要。
- 如果其中一个测试失败,它将获得第二次机会并再次执行。
- 如果其中一项测试在两次尝试中均失败并被标记为“失败”,则无论如何仍将执行另一项测试。
有没有办法通过抽象消除所有这些重复?如何才能实现这一目标?
I have a gitlab-ci/cd.yaml-file that executes 2 test scripts. As you can see there is a lot of repetition going on. As a matter of fact, both stages are identical, except for their "script" value.
For the smoke-suite the value is
- npm run docker_smoke --single-run --progress false
For the regression-suite the value is
- npm run docker_regression --single-run --progress false
image: node-karma-protractor
stages:
- suiteSmoke
- suiteRegression
before_script:
- npm install
# Smoke suite =================================
smoke_suite:
stage: suiteSmoke
tags:
- docker-in-docker
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
script:
- npm run docker_smoke --single-run --progress false
retry: 1
#saving the HTML-Report
artifacts:
when: on_failure
paths:
- reporting/
expire_in: 1 week
allow_failure: true
# Regression suite ============================
regression_suite:
stage: suiteRegression
tags:
- docker-in-docker
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
script:
- npm run docker_regression --single-run --progress false
retry: 1
#saving the HTML-Report
artifacts:
when: on_failure
paths:
- reporting/
expire_in: 1 week
allow_failure: true
The script needs to adhere to the following rules:
- The tests need to run consecutively (as problems may occur if they're executed in a simultaneous fashion). The order of execution doesn't matter, though.
- If either of the test fails, it gets a second chance and will be executed one more time.
- If one of the tests fails on both attempts and is marked as a 'failed', the other one will still be executed regardless.
Is there a way to eliminate all this repetition via abstraction? How can this be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Gitlab 提供了一些机制来防止管道中的重复,即 YAML 锚点 和扩展关键字,而为了可读性,建议使用
extends
关键字。应用于您的示例,您的管道可能如下所示:
Gitlab provides a couple of mechanisms to prevent duplications in your pipelines namely YAML anchors and the extends keyword while the
extends
keyword is recommended for readability.Applied to your example your pipeline can look like this:
您可以定义模板,然后用它们扩展您的作业。
文档: https://docs.gitlab.com/ee/ci/yaml/#扩展
示例:
You can define templates and then extend your jobs with them.
Documentation: https://docs.gitlab.com/ee/ci/yaml/#extends
Example: