How to trigger a build only if changes happen on particular set of files How to trigger a build only if changes happen on particular set of files jenkins jenkins

How to trigger a build only if changes happen on particular set of files


The Git plugin has an option (excluded region) to use regexes to determine whether to skip building based on whether files in the commit match the excluded region regex.

Unfortunately, the stock Git plugin does not have a "included region" feature at this time (1.15). However, someone posted patches on GitHub that work on Jenkins and Hudson that implement the feature you want.

It is a little work to build, but it works as advertised and has been extremely useful since one of my Git trees has multiple independent projects.

https://github.com/jenkinsci/git-plugin/pull/49

Update: The Git plugin (1.16) now has the 'included' region feature.


If you are using a declarative syntax of Jenkinsfile to describe your building pipeline, you can use changeset condition to limit stage execution only to the case when specific files are changed. This is now a standard feature of Jenkins and does not require any additional configruation/software.

stages {    stage('Nginx') {        when { changeset "nginx/*"}        steps {            sh "make build-nginx"            sh "make start-nginx"        }    }}

You can combine multiple conditions using anyOf or allOf keywords for OR or AND behaviour accordingly:

when {    anyOf {        changeset "nginx/**"        changeset "fluent-bit/**"    }}steps {    sh "make build-nginx"    sh "make start-nginx"}


Basically, you need two jobs. One to check whether files changed and one to do the actual build:

Job #1

This should be triggered on changes in your Git repository. It then tests whether the path you specify ("src" here) has changes and then uses Jenkins' CLI to trigger a second job.

export JENKINS_CLI="java -jar /var/run/jenkins/war/WEB-INF/jenkins-cli.jar"export JENKINS_URL=http://localhost:8080/export GIT_REVISION=`git rev-parse HEAD`export STATUSFILE=$WORKSPACE/status_$BUILD_ID.txt# Figure out, whether "src" has changed in the last commitgit diff-tree --name-only HEAD | grep src# Exit with success if it didn't$? || exit 0# Trigger second job$JENKINS_CLI build job2 -p GIT_REVISION=$GIT_REVISION -s

Job #2

Configure this job to take a parameter GIT_REVISION like so, to make sure you're building exactly the revision the first job chose to build.

Parameterized build string parameterParameterized build Git checkout