如何最大程度地减少Bitbucket管道中的重复?

发布于 2025-01-25 16:09:08 字数 1165 浏览 1 评论 0原文

请参阅下面的示例脚本。某种程度上类似于我们的管道的样子,但并不是很明显。

  steps:
    - step: &test-sonar
        name: test and analyze on SonarCloud
        script:
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
          - pip install pytest
          - pytest --cov=tests/ --cov-report xml:coverage-reports/coverage-report.xml --junitxml=test-reports/report.xml
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - pipe: sonarsource/sonarcloud-scan:1.4.0
    - step: &check-quality-gate-sonarcloud
        name: Check the Quality Gate on SonarCloud
        script:
          - pipe: sonarsource/sonarcloud-quality-gate:0.1.6

每当我们合并到主分支时,我们都会运行此脚本。

这将主要是也是相同的脚本集,但是pytest命令中的标志略有不同。

同样,对于预定的管道,脚本将大部分是相同的,并且有些细微地更改了pytest命令的标志。

我不想3次重复相同的脚本,我不确定如何使其更加重复使用。

我唯一能想到的是使用bitbucket变量来改变Pytest的执行方式,具体取决于管道的类型,但我仍将头缠绕在此方面。

see sample script below. somehow resembles how our pipeline looks like but not really obviously.

  steps:
    - step: &test-sonar
        name: test and analyze on SonarCloud
        script:
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
          - pip install pytest
          - pytest --cov=tests/ --cov-report xml:coverage-reports/coverage-report.xml --junitxml=test-reports/report.xml
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - {some command}
          - pipe: sonarsource/sonarcloud-scan:1.4.0
    - step: &check-quality-gate-sonarcloud
        name: Check the Quality Gate on SonarCloud
        script:
          - pipe: sonarsource/sonarcloud-quality-gate:0.1.6

this script is what we run whenever we merge into the master branch.

this would mostly also be the same set of scripts but the flags in the pytest command are slightly different.

and again for a scheduled pipeline, the scripts would be mostly the same with some slight changes the flags of the pytest command.

I wouldn't want to repeat the same script 3 times and I'm not sure how to make this a bit more reusable.

Only thing I can think of is using bitbucket variables to change how pytest is executed depending on the type of pipeline but I'm still wrapping my head around that as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

那一片橙海, 2025-02-01 16:09:08

我的某些管道共享一个共同的初始化,我将我链接成&&&code>运算符并将其存储在YAML锚点中的单个指令,就像

definitions:

  yaml-anchors:

    - &common-init-script >-
        command1
        && command2
        && command3

    - &aaa-step
        script:
          - *common-init-script
          - some-command

    - &bbb-step
        script:
          - *common-init-script
          - some-different-command

pipelines:
  ...
    my-pipeline:
      - step: *aaa-step
      - step: *bbb-step

希望一样,&&操作员如果有任何故障(如果您的所有命令都会在失败上取消状态代码),则将算法短路(如果授予命令链,则命令链被Bitbucket算作单个指令,以便您将每个指令丢失度量以及控制台输出将被串联,因此有时很难确定每个命令输出何时启动或结束。


理想情况下,您将yaml命令列表存储到锚点中,然后将它们与其他内联命令序列合并,但是上次我检查 https://github.com/yaml/yaml/yaml/issues/48 主要要点

  1. 应该取决于应用程序(bitbucket pipelines parser),以告诉列表是否列表命令列表应扁平。
  2. 目前尚不清楚如何将其引入YAML规范而不会造成严重破坏。

现在我找不到JIRA问题,但是Atlassian也不愿在他们的结尾实施这一点。所以,我们在这里。

Some of my pipelines share a common initialization that I chain into a single instruction with the && operator and store it in a yaml anchor like

definitions:

  yaml-anchors:

    - &common-init-script >-
        command1
        && command2
        && command3

    - &aaa-step
        script:
          - *common-init-script
          - some-command

    - &bbb-step
        script:
          - *common-init-script
          - some-different-command

pipelines:
  ...
    my-pipeline:
      - step: *aaa-step
      - step: *bbb-step

Hopefully, the && operator will shortcircuit if anything failed (granted if all your commands will exit with a non-zero status code on failures), but the drawback is that the command chain is reckoned as a single instruction by Bitbucket so you will loose per-instruction time measures and also the console output will be concatenated so it is sometimes hard to tell when each command output starts or ends.


Ideally, you would store yaml lists of commands into anchors and later merge them with other inline sequences of commands but this idea was having a bad reception last time I checked https://github.com/yaml/yaml/issues/48 the main points being

  1. It should be up to the application (bitbucket pipelines parser) to tell if lists of lists of commands should be flattened.
  2. It is unclear how to introduce this into the yaml spec without causing havoc.

And now I can't find the jira issue but Atlassian was also reluctant to implement this on their end. So, here we are.

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