如何编写 .gitlab-ci.yml 作业以仅在合并请求中运行

Ole*_*leg 1 continuous-integration gitlab gitlab-ci

仅在合并请求中运行时如何在 .gitlab-ci.yml 中正确写入作业?

test_c:
  stage: test
  script:
    - echo "This job tests something. It will only run when all jobs in the"
    - echo "build stage are complete."
  only:
    - merge_requests
Run Code Online (Sandbox Code Playgroud)

此作业不在合并请求中运行,但不在 master 或 develop 中运行和提交。

amB*_*ear 6

Gitlab 文档建议使用“规则”而不是“仅”。您只能通过执行以下操作来完成 merge_requests:

test_c:
  stage: test
  script:
    - echo "This job tests something. It will only run when all jobs in the"
    - echo "build stage are complete."
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
Run Code Online (Sandbox Code Playgroud)

https://docs.gitlab.com/ee/ci/yaml/#workflowrules


far*_*rch 5

您可以使用workflow来控制管道创建。在顶层定义此关键字,使用单个rules. 此示例显示管道仅在创建新的合并请求时才会执行,最后一个when设置never为防止在将新分支推送到服务器或任何其他类型的事件时执行管道。

workflow:
   rules:
     - if: $CI_MERGE_REQUEST_ID
       when: always
     - when: never
Run Code Online (Sandbox Code Playgroud)

注意 正如Gitlab文档中提到的

这些管道在 UI 中被标记为分离,并且它们无权访问受保护的变量。否则,这些管道与其他管道相同。


Ser*_*aka 1

您的代码是正确的,请在测试您的 merge_request 管道之前将其提交给 master。