如何消除使用几乎相同阶段的“.gitlab-ci.yml”脚本中的代码重复?

发布于 2025-01-09 04:53:29 字数 1431 浏览 1 评论 0原文

我有一个执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦言归人 2025-01-16 04:53:29

Gitlab 提供了一些机制来防止管道中的重复,即 YAML 锚点扩展关键字,而为了可读性,建议使用 extends 关键字。

应用于您的示例,您的管道可能如下所示:

image: node-karma-protractor

stages:
  - suiteSmoke
  - suiteRegression

.test:suite:
  stage: suiteSmoke
  tags:
    - docker-in-docker
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
  before_script:
    - npm install
  retry: 1
  #saving the HTML-Report
  artifacts:
    when: on_failure
    paths:
      - reporting/
    expire_in: 1 week
  allow_failure: true

# Smoke suite =================================
smoke_suite:
  extends: .test:suite
  script:
    - npm run docker_smoke --single-run --progress false

# Regression suite ============================
regression_suite:
  extends: .test:suite
  script:
    - npm run docker_regression --single-run --progress false

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:

image: node-karma-protractor

stages:
  - suiteSmoke
  - suiteRegression

.test:suite:
  stage: suiteSmoke
  tags:
    - docker-in-docker
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
  before_script:
    - npm install
  retry: 1
  #saving the HTML-Report
  artifacts:
    when: on_failure
    paths:
      - reporting/
    expire_in: 1 week
  allow_failure: true

# Smoke suite =================================
smoke_suite:
  extends: .test:suite
  script:
    - npm run docker_smoke --single-run --progress false

# Regression suite ============================
regression_suite:
  extends: .test:suite
  script:
    - npm run docker_regression --single-run --progress false
风流物 2025-01-16 04:53:29

您可以定义模板,然后用它们扩展您的作业。

文档: https://docs.gitlab.com/ee/ci/yaml/#扩展

示例:

.job-template:
  tags:
    - tag
  allow_failure: true

job1:
  extends:
    - .job-template
  script:
    - do something

job2:
  extends:
    - .job-template
  script:
    - do something

You can define templates and then extend your jobs with them.

Documentation: https://docs.gitlab.com/ee/ci/yaml/#extends

Example:

.job-template:
  tags:
    - tag
  allow_failure: true

job1:
  extends:
    - .job-template
  script:
    - do something

job2:
  extends:
    - .job-template
  script:
    - do something
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文