GitLab CI, monorepo and feature branch

末鹿安然 提交于 2019-12-12 08:55:59

问题


I have a monorepo in GitLab with a Feature Branch approach.

What I'm trying to achieve is to launch the part of the pipeline associated to the directory containing the altered files. So my .gitlab-ci.yml looks like :

job1:
  stage: build
  script:
    - ...
  only:
    changes:
      - myparentdir/dir1/*

job2:
  stage: build
  script:
    - ...
  only:
    changes:
      - myparentdir/dir2/*
  1. Create a new branch from develop
  2. Commit myparentdir/dir2/test.txt on this branch
  3. The pipeline launch every build jobs !

It seems like GitLab is considering every files as altered when working with a new feature branch.

Do you know any workaround?


回答1:


Gitlab ci always treats changes for new branches as true. The reason is that they can't decide what to compare against.

what this means is that for the first pipeline of a new branch, everything will be built.

See the feature request for more details.

But, there is a fairly new feature called Pipelines for merge requests - that runs a stage on merge requests. Here is the feature request that implements changes with merge_requests. It's merged, but I'm not sure if it's already released. (the milestone is 11.9 - the next release)

In the meantime you can implement it yourself - You can add a stage that compares the changes (git diff) and decides whether to run the next stage:

.store_diff_from_main: &store_diff_from_main |
  git diff --name-only origin/master...HEAD > "${DIFF_FILE}"
  git diff --name-only HEAD~1 >> "${DIFF_FILE}"

.skip_stage: &skip_stage_condition |
  echo Checking for changes in ${STAGE_PATHS}, changed files
  # https://coderwall.com/p/gecfwa/git-diff-vs
  cat .diff-from-master
  # also cover merge squash cases
  if ! (cat ${DIFF_FILE} | grep -E "${STAGE_PATHS}"); then
    echo "Skipping stage ..."
    exit 0
  fi



来源:https://stackoverflow.com/questions/55067521/gitlab-ci-monorepo-and-feature-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!