在合并请求的流水线中,GitLab CI没有执行某些作业,而在主分支的流水线中它们都在运行

后端开发 2026-07-10

我正在使用GitLab Community Edition v18.9.1。我创建了一个管道,用于构建Docker镜像并为主分支和合并请求执行特定任务。

我的 .gitlab-ci.yml 看起来像这样:

---
include:
  - project: sentinel
    file: ci/pipeline.yml
    ref: main

我的 pipeline.yml 看起来像这样:

---
include:
  ...
  - local: ci/triggers.yml

default:
  timeout: 10m

variables:
  ..

stages:
  - triggers
  - ...

这是我的 lint.yml

---
include:
  local: ci/_base.yml

stages:
  - lint

.linter-base:
  stage: lint
  extends: .sentinel-standard-rules
  image: ${SENTINEL_LINTER_IMAGE}
  needs: [ ]

.linter-report:
  extends: .linter-base
  variables:
    RUFF_ARGS: ''
  script:
    - |
      set -x
      ruff check $RUFF_ARGS --output-format=gitlab --output-file=gl-code-quality-report.json .
  artifacts:
    when: always
    reports:
      codequality: gl-code-quality-report.json

python-code-style-check:
  allow_failure: false
  extends: .linter-report

python-code-todo-and-fixme-finder:
  extends: .linter-report
  allow_failure: true
  script:
    - |
      ruff check --isolated --select FIX,TD006,ERA001 --output-format=gitlab  . > report.json || true
      jello '[item.update({"severity": "info"}) or item if item.get("severity") == "major" else item for item in _]' < report.json > gl-code-quality-report.json

python-format-check:
  extends: .linter-base
  allow_failure: true
  script:
    - ruff format --check .

以下是来自 _base.yml 的我的标准规则:

---
.sentinel-standard-rules:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_PIPELINE_SOURCE == "parent_pipeline"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_PIPELINE_SOURCE == "web"
    - if: $CI_PIPELINE_SOURCE == "pipeline"

我在一个自定义的 trigger-阶段触发作业,作业作为下游管道启动:

triggers.yml

.trigger-base:

trigger-lint:
  stage: triggers
  trigger:
    include:
      - local: ci/lint.yml
    strategy: depend
  needs:
    - job: some-build-job
    - job: some-other-job
      optional: true

在我的 main 分支上,它运行得非常顺利:

main branch pipeline that shows all three jobs have run

然而在合并请求管道运行时,突然只执行了一个 python-format-check。我本来希望它们三个都能运行,但我不明白为什么没有被触发。

Merge Request Pipeline Run, but only one instead of all three jobs are executed

如何确保在合并请求的管道中也能够运行我的这三个lint作业?

解决方案

在从远端导入 main 分支的管道配置时:

---
include:
  - project: sentinel
    file: ci/pipeline.yml
    ref: main

你的其他includes仍然引用 local 文件。这意味着

include:
  local: ci/_base.yml

将从你的合并请求所在的分支获取这些文件,而不是从你的 main 分支获取。你很可能获取到了一个过时的配置。

要么放弃从远端的 main 分支获取的方式,要么将所有includes也改为从同一远端获取。

假设你已经有一个最新的 main 分支的CI配置,你可以在对你的功能分支进行变基(rebase)之后尝试重新运行管道。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章