4

The conditionals of Jenkins Declarative Pipeline is built around branches namely. I would like to evaluate whether a specific folder is changed within any branch and then run the stages.

Something like:

stage {
   when {
      folderX changed
   }
}

To give you a better idea of why I need this feature, let me elaborate. The project I am working on exists out of a few modules (let's say micro services). Although every module can have its own branch or even repository, we have chosen to put them together in their own folders, so we can always keep everything clean in the master. Our Jenkins pipleine has a stage for every module. However, we do not want to rebuild every module if nothing is changed in that folder.

Ashqary
  • 61
  • 6

2 Answers2

2

I finally solved the problem by a script block which I found here:

when{ expression {
                    def changeLogSets = currentBuild.changeSets
                    for (int i = 0; i < changeLogSets.size(); i++) {
                        def entries = changeLogSets[i].items
                        for (int j = 0; j < entries.length; j++) {
                            def entry = entries[j]
                            def files = new ArrayList(entry.affectedFiles)
                            for (int k = 0; k < files.size(); k++) {
                                def file = files[k]
                                if(file.path.contains("FolderOfInterest")){
                                    return true
                                }
                            }
                        }
                    }
                    return false
                }   
            }
Ashqary
  • 61
  • 6
0

There are couple problems here:

  1. The feature you're proposing does not exist. You could submit a ticket in the jenkins issue tracker for that.

  2. Also, I think you're suggesting that you want to look at code changes across branches. This is not a standard use case for pipelines. Usually, you want to build a specific branch of a project (when it changes, for example), so you are going to feel moderate to intense pain trying to do what you're suggesting.

If you want to see what has changed on the current branch from within a Jenkisfile, you can use currentBuild.changeSets (docs). You could combine that with if statements and whatnot, within a script block if you want to use a declarative pipeline like you seem to be suggesting.

Keep at it and you'll figure it out. Good luck!

burnettk
  • 13,557
  • 4
  • 51
  • 52