有没有办法在Gitlab CI工作中使用或条件有需求

发布于 2025-02-01 21:13:02 字数 897 浏览 2 评论 0原文

我正在尝试使用“需求”。

.gitlab -ci.yml文件 - >

stages:
  - build
  - test
  - deploy


Build_job:      
  stage: build
  script:
    - echo "hello from build job"

Test_job1:
  stage: test
  script:
    - echo "Start test 1"
  when: manual

Test_job2:
  stage: test
  script:
    - echo "Start test 2"
  when: manual

Deploy_job:
  stage: deploy
  script:
    - echo "Start deploying the job"
  when: manual
    needs:
      - job: Test_job1
        optional: true
      - job: Test_job2
        optional: true

我的目的是test_job1test_job2传递deploys_job应该启用。 但是,使用上述代码,我无法这样做,因为deploy_job仅在传递两个测试作业时才能启用。

如果可以使用需要的东西,是否有办法:[test_job1或test_job2]

I am trying to create a job dependency with "OR" condition for previous stage jobs using "needs" in ".gitlab.ci.yml" file but unable to find a solution for this.

.gitlab-ci.yml file ->

stages:
  - build
  - test
  - deploy


Build_job:      
  stage: build
  script:
    - echo "hello from build job"

Test_job1:
  stage: test
  script:
    - echo "Start test 1"
  when: manual

Test_job2:
  stage: test
  script:
    - echo "Start test 2"
  when: manual

Deploy_job:
  stage: deploy
  script:
    - echo "Start deploying the job"
  when: manual
    needs:
      - job: Test_job1
        optional: true
      - job: Test_job2
        optional: true

My aim is either of Test_job1 or Test_job2 is passed Deploy_job should be enabled.
But with the above code, I am unable to do so as Deploy_job is getting enabled only when both previous two test jobs are passed.
Pipeline Status

Is there a way if something can be used like needs: [Test_job1 or Test_job2]?

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

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

发布评论

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

评论(2

罪#恶を代价 2025-02-08 21:13:02

需求:可选: 关键字很混乱。 。它是用于设置需求:要求,由于其他管道/作业逻辑,作业可能不存在时(作业可以可选地存在 )。
但是,任何可选的需求:作业: do 的名称都将全部都需要。

我看到了一些可以在所需管道上获取关闭的选项:

自动化路径

您可以使用 规则: 您的test_job作业中的关键字以有条件地将其添加到管道中。

正在确定要运行哪个test_job的人(基于您使用的何时:手动)的假设可能遵循某种决策逻辑 - 或可以确定预设的决策逻辑在团队上。然后,您将在您的需求中构建该逻辑:if:条件以确定您的test_job# jobs 在管道中存在 deploy_job:仅需要添加到管道中的test_jobs。

例如,如果一个测试应在您的默认分支上运行,另一个测试可以在功能分支上进行,则可以使用以下测试来自动运行相关的test_job,并且Deploy_job依赖于它:

Test_job1:
  stage: test
  script:
    - echo "Start test 1"
  # this job is only added for pipelines on the default branch
  rules:
    - if $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
      when: on_success
    - when: never

Test_job2:
  stage: test
  script:
    - echo "Start test 2"
  # this job is only added for pipelines NOT on the default branch
  rules:
    - if $CI_COMMIT_REF_NAME != $CI_DEFAULT_BRANCH
      when: on_success
    - when: never

Deploy_job:
  stage: deploy
  script:
    - echo "Start deploying the job"
  # this is the default value, it's added here to show the automation difference from the question's pipeline
  rules:
    - when: on_success
  # depend on Test_job1 AND Test_job2 if they individually exist in the pipeline
  needs:
    - job: Test_job1
      optional: true
    - job: Test_job2
      optional: true

手动路径

您可以交换wher wher:MANUAL < /code>回到时,何时:on_success如果您仍然需要循环中的人来触发测试作业或部署作业。

真正的手动路径

或者,如果您真的只希望您的人手动制作test_job:临时决定,则只需删除需求:关键字来自您的decloby_job:并使用现有阶段依赖性结构。 何时:手动密钥隐式设置allow_failure:true,因此您的作业已经是可选的。如果您的人类无法信任您通过手动工作来管理“在部署之前进行测试”,请将人类从循环中取出并查看上面的“自动化路径”。

The needs:optional: keyword is confusingly named. It is for setting up needs: requirements when jobs might not exist in the pipeline due to other pipeline/job logic (the jobs can optionally exist).
However, any optional needs:job: names that do exist in the pipeline will all be required.

I see a few options to get close to your desired pipeline:

automation path

You could use the rules: keyword in your Test_job jobs to conditionally add them to the pipeline.

The humans that are deciding which Test_job to run (an assumption based on the fact that you're using when: manual) are likely following decision logic of some sort--or a preset decision logic can be decided on by the team. You would then build that logic into your needs:if: condition to determine whether or not your Test_job# jobs exist in the pipeline--then only the Test_jobs that are added to the pipeline will be required by the Deploy_job:.

For example, if one test should run on your default branch and the other on feature branches, you could use the following to automatically run the relevant Test_job and have the Deploy_job depend on it:

Test_job1:
  stage: test
  script:
    - echo "Start test 1"
  # this job is only added for pipelines on the default branch
  rules:
    - if $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
      when: on_success
    - when: never

Test_job2:
  stage: test
  script:
    - echo "Start test 2"
  # this job is only added for pipelines NOT on the default branch
  rules:
    - if $CI_COMMIT_REF_NAME != $CI_DEFAULT_BRANCH
      when: on_success
    - when: never

Deploy_job:
  stage: deploy
  script:
    - echo "Start deploying the job"
  # this is the default value, it's added here to show the automation difference from the question's pipeline
  rules:
    - when: on_success
  # depend on Test_job1 AND Test_job2 if they individually exist in the pipeline
  needs:
    - job: Test_job1
      optional: true
    - job: Test_job2
      optional: true

manual path

You could swap when:manual back in place for when:on_success if you still need humans in the loop on triggering the test jobs or deploy job.

really manual path

Alternatively, if you really just want your humans to manually make the Test_job: decisions ad-hoc, then just remove the needs: keyword from your Deploy_job: and use the existing Stage dependency structure. The when: manual key implicitly sets allow_failure: true, so your jobs are already optional as is. If your humans can't be trusted to manage the "Test before Deploy" dependency on their own with manual jobs, then take the humans out of the loop and see the "automation path" above.

人海汹涌 2025-02-08 21:13:02

除了现有答案。如果您想使用工件和依赖的作业,则可以像以下那样使用它。因此,在这种情况下,您无需使用依赖项

needs:
    - job: job1
      optional: true
      artifacts: true
    - job: job2
      optional: true
      artifacts: true

In addition of existing answers. If you want to use artifacts as well of dependant jobs, you can use it like below. Hence you do not need to use dependencies in such scenarios.

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