-->

GitLab CI, monorepo and feature branch

2019-04-12 14:36发布

问题:

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