Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins multibranch pipeline only for subfolder

I have git monorepo with different apps. Currently I have single Jenkinsfile in root folder that contains pipeline for app alls. It is very time consuming to execute full pipeline for all apps when commit changed only one app.

We use GitFlow-like approach to branching so Multibranch Pipeline jobs in Jenkins as perfect fit for our project.

I'm looking for a way to have several jobs in Jenkins, each one will be triggered only when code of appropriate application was changed.

Perfect solution for me looks like this:

I have several Multibranch Pipeline jobs in Jenkins. Each one looks for changes only to given directory and subdirectories. Each one uses own Jenkinsfile. Jobs pull git every X minutes and if there are changes to appropriate directories in existing branches - initiates build; if there are new branches with changes to appropriate directories - initiates build.

What stops me from this implementation

  1. I'm missing a way to define commit to which folders must be ignored during scan execution by Multibranch pipeline. "Additional behaviour" for Multibranch pipeline doesn't have "Polling ignores commits to certain paths" option, while Pipeline or Freestyle jobs have. But I want to use Multibranch pipeline.

  2. Solution described here doesnt work for me because if there will be new branch with changes only to "project1" then whenever Multibranch pipeline for "project2" will be triggered it will discover this new branch anyway and build it. Means for every new branch each of my Multibranch pipelines will be executed at least once no matter if there was changes to appropriate code or not.

Appreciate any help or suggestions how I can implement few Multibranch pipelines watching over same git repository but triggered only when appropriate pieces of code changed

like image 555
mrzodiak Avatar asked Feb 12 '18 16:02

mrzodiak


People also ask

What is the use of Multibranch pipeline in Jenkins?

The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.

What is difference between pipeline and Multibranch pipeline?

Jenkins Pipeline Vs. Multibranch Pipeline. A multibranch pipeline is meant for building multiple branches from a repository and deploy to multiple environments if required. A pipeline job supports both pipeline steps to be added in Jenkins configuration and form SCM.

How do I add a branch to Multibranch pipeline Jenkins?

Head over to your Jenkins instance and create a new item. Enter a name for the job, and select the “Multibranch Pipeline” option at the end of the screen. Then, click on the OK button. In the next screen, go to the “Branch sources” tab, click on the “Add source” button, and choose “Git” from the dropdown menu.

What is the purpose of a Multibranch pipeline?

A multibranch pipeline is a pipeline that has multiple branches. The main advantage of using a multibranch pipeline is to build and deploy multiple branches from a single repository. Having a multibranch pipeline also allows you to have different environments for different branches.


1 Answers

This can be accomplished by using the Multibranch build strategy extension plugin. With this plugin, you can define a rule where the build only initiates when the changes belong to a sub-directory.

  1. Install the plugin
  2. On the Multibranch pipeline configuration, add a Build strategy
  3. Select Build included regions strategy
  4. Put a sub-folder on the field, such as subfolder/**

Sample image of the configuration here

This way the changes will still be discovered, but they won't initiate a build if it doesn't belong to a certain set of files or folders.

This is the best approach I'm aware so far. But I think the best way would be a case where the changes doesn't even get discovered.

Edit: Gerrit Code Review plugin configuration

In case you're using the Gerrit Code Review plugin, you can also prevent new changes to be discovered by using a custom query:

Gerrit Code Review configuration example

like image 58
felipecrs Avatar answered Sep 23 '22 03:09

felipecrs