2
votes

In one jenkins job, we want to track multiple branches and trigger it only once when there are changes in any of them. Is that possible?

The problem is, that we have to run a bunch of jobs for each branch serially and they should not interfer. What we want to achieve is for example:

[changes on any branch] -> checkout master -> job1 -> job2 -> job3 -> checkout release -> job1 -> job2 -> job3

The current behaviour is, that the job gets triggered for each branch that has changes. So, if the 'build pipeline' has been triggered because of changes in master, it's possible to be triggered again caused by changes in release:

[changes on master]  -> checkout master -> job1 -> ... -> checkout release -> ...
[changes on release] -> checkout master -> job1 -> ... -> checkout release -> ...

Another (even better) option would be, to start one job periodically and do the build stuff for each branch only, if it has changes:

for each branch do
  if branch has changes then
    job1
    job2
    ...
1
I would do also to multiple jobs, each one for a specific branch, like this you are sure that there is no interference and it is easier to debug if there happens something unexpectedly... - sop
@sop I just edited my question to be more clear, hopefully. - SevenEleven

1 Answers

0
votes

Meanwhile, I came up with a solution on my own.

Now, there is one job that is run regularly to perform a git fetch, pipe the output to a file and check for any changes in a given list of branches.

git fetch -v --dry-run > NUL 2> git-fetch.log

If this is the case, a job chain with one 'main'-job for each branch is triggered. These also check the previously saved output for changes in the particular branch and may trigger a list of sub-jobs. After that, they run all the sub-jobs (or skip them when nothing has changed).

It kind of looks like this:

- job1 
  git fetch 
  if changes in any branch then trigger job-master
  - job-master
    if changes in master then trigger 'sub-jobs'
    trigger job-feature1
    - job-feature1 
      if changes in feature1 then trigger 'sub-jobs'
      trigger feature2
      - job-feature2
        if changes in feature2 then trigger 'sub-jobs'
        ...