4

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?

jBravo
  • 873
  • 1
  • 9
  • 28

1 Answers1

3

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

Amityo
  • 5,635
  • 4
  • 22
  • 29
  • It seems good. For now, I can't affirm that it solve my problem because I have difficulties for implementing this with windows batch : https://stackoverflow.com/questions/55105278/gitlab-runner-on-windows-and-dealing-with-errorlevel?noredirect=1#comment96958039_55105278 – jBravo Mar 12 '19 at 14:18
  • It works. The problem is that all my 126 jobs are launched, 125 are skipped but still appeared on my pipeline... – jBravo Mar 12 '19 at 15:58
  • that's the problem with this solution - it's not native - so the jobs will start but will be skipped. I am also waiting for the native solution – Amityo Mar 12 '19 at 15:59
  • `only: changes` combined with `only: refs: - merge_requests` appears to allow me to have code specific pipelines but has strange behavior because "The behavior of the only: merge_requests parameter is such that only jobs with that parameter are run in the context of a merge request; no other jobs will be run." from https://docs.gitlab.com/ee/ci/merge_request_pipelines/, so I can't have some jobs apply to all code changes and other jobs that are filepath specific. – mmacvicar Jul 23 '19 at 18:17
  • @mmacvicar you are right. In my case I have the branch master and feature branches. If I want for some stage to run for PR and for master I add - master after the merge_request line – Amityo Jul 23 '19 at 18:19