如果测试作业在另一个存储库中失败,则 gitlab 多作业管道失败

时间:2021-04-01 17:26:02

标签: gitlab gitlab-ci

我的应用程序存储库 A 中有一个 gitlab ci 管道,它调用端到端测试存储库 T 来运行其测试。回购 A 管道成功触发了来自回购 T 的测试,但如果测试作业在 T 中失败,则从 A 调用 T 中测试作业的作业仍然通过。如何让 repo A 跟踪 Repo T 的测试作业的结果,并根据 T 中的测试作业通过/失败其管道?

.gitlab-ci.yml 用于测试 Repo T:

stages:
  - test

test:
  stage: test
  image: markhobson/maven-chrome:jdk-11
  artifacts:
    paths:
      - target/surefire-reports
  script:
    - mvn test
  only:
    - triggers

.gitlab-ci.yml 来自应用程序仓库 A:

job1:
    stage: unit-tests ...
job2:
    stage: build ...
...

trigger-e2e-repo:
  stage: e2e-testing
  image: markhobson/maven-chrome
  script:
    - "curl -X POST -F token=repo-T-token -F ref=repo-T-branch https://repo-A/api/v4/projects/repo-T-id/trigger/pipeline"
  only:
    - repo-A-branch

2 个答案:

答案 0 :(得分:1)

从 GitLab 11.8 开始,您可以通过 bridge job 触发管道。

<块引用>

在 GitLab 11.8 中,GitLab 提供了新的 CI/CD 配置语法,使这项任务更容易,并避免需要 GitLab Runner 来触发跨项目管道。

使用桥接作业可以mirror the status of the trigger pipeline到调用管道。

<块引用>

您可以使用策略:depend 将管道状态从触发的管道镜像到源桥接作业。

你的例子:

trigger-e2e-repo:
  stage: e2e-testing
  trigger: repo-T
  strategy: depend

如果带有测试作业的触发管道失败,调用管道也会失败。

如果您只想在由桥接作业执行时执行存储库“Repo T”中的特定作业,那么您应该使用 only: pipeline (only) 或 rules: -if '$CI_PIPELINE_SOURCE == "pipeline"' ({ {3}}) 而不是 only: triggers

答案 1 :(得分:1)

我无法使用桥接作业属性来镜像下游作业结果,因为我的 gitlab 版本在 11.8 之前。我确实通过为 repo A 创建触发器并使用这个新的第二个触发器从 repo T 调用 repo A 来使其工作。存储库 A 中的其余作业将仅通过触发器和变量设置(在本例中为 JOB )激活,如下所示:

.gitlab-ci.yml 用于 repo T:

test:
  stage: test
  script:
    - mvn test
    - "curl -X POST -F token=repo-A-token -F ref=$BRANCH -F variables[JOB]=build https://project.com/api/v4/projects/project_id/trigger/pipeline"

.gitlab-ci.yml 在 A

job1:
  ...
  except:
    - triggers
job2:
  ...
  except:
    - triggers
...
trigger-e2e-repo:
  stage: e2e-testing
  script:
    - "curl -X POST -F token=repo-B-token -F ref=repo-B-branch -F variables[BRANCH]=$CI_COMMIT_REF_NAME -F https://project-B/api/v4/projects/project-B-id/trigger/pipeline"
  except:
    - triggers

build_application_for_prod:
  stage: build_prod
  script:
    - "curl -X POST -F token=repo-A-token -F ref=$CI_COMMIT_REF_NAME -F variables[JOB]=deploy -F variables[SEND]=true https://foo/api/v4/projects/proj_A_id/trigger/pipeline"
  only:
    variables:
      -  $JOB == "build"

deploy_production_environment:
  stage: deploy_prod
  script:
    - ...
  only:
    variables:
      - $JOB == "deploy"

请注意,我还必须在存储库 A 中的端到端测试之前为作业添加 except 语句,以便在稍后调用存储库 A 的 API 触发器时它们不会重新运行和循环。