首页 > 解决方案 > 在 GitLab 的 CI/CD 系统中,如果作业在我的 fork 中,我如何防止它运行,并且只有在它合并到主仓库时才允许它运行?

问题描述

我有一份gitlab-ci.yml喜欢的工作

build and push:
  stage: push
  only:
  - master
  script:
  - gcloud docker -- push $IMAGE_TAG

这个想法是它应该只在分支是 master 时运行,但我也只希望它在 repo 是app/而不是repo 时运行dave/

文档

存储库路径可用于仅为父存储库而不是 fork 执行作业:

job:
  only:
    - branches@gitlab-org/gitlab-ce

但我不能让它工作。如果我们的 gitlab 服务器是gitlab.myco.io并且应用程序是app/my-project-name我想我可以把

job:
  only:
    - branches@gitlab-myco-io/app/my-project-name/master

但这似乎不起作用。

这样做的正确方法是什么?

标签: gitcontinuous-integrationgitlabbranchgit-fork

解决方案


基于privateomega 的答案,因为only / except 已被弃用,现代替代方案 with rules,也支持通用主分支名称,将类似于:

job:
  rules:
    - if: '$CI_PROJECT_NAMESPACE == "app" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'

另请参阅https://about.gitlab.com/releases/2020/04/22/gitlab-12-10-released/#auto-devops-and-secure-configuration-templates-are-changing-to-%60rules% 60-instead-of-%60only/except%60


推荐阅读