We have a project in a Github repository with multiple Jenkinsfiles:
my-project app Jenkinsfile lib1 Jenkinsfile lib2 Jenkinsfile
We have created 3 Jenkins pipelines each referring to a Jenkinsfile.
Question: How to avoid triggering "app" and "lib1" pipelines when there is a new commit in "lib2"? We don't want to run N jobs every time a commit happens.
I've seen that the issue is addressed in https://issues.jenkins-ci.org/browse/JENKINS-43749, but I haven't found a solution there.
I achieved to have multiple pipelines (Jenkinsfile) in a single repository. It's not hacky but it was not exactly trivial, requires recent versions of Jenkins with updated Lockable Resources plugin and may require additional steps in the build procedure.
A multi-branch pipeline project always includes a Jenkinsfile in its repository root. Jenkins automatically creates a sub-project for each branch that it finds in a repository with a Jenkinsfile . Multi-branch pipelines use the same version control as the rest of your software development process.
Jenkins pipeline allows you to have a flexible Jenkinsfile with stages for your build. So you can have an initial stage where you run linting, tests, etc., and then separate stages for building artifacts or deploying them. This is very useful when you want to do multiple things in your pipeline.
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.
RECENT UPDATE:
I later fixed this issue using following code snippet: If you see the command dir('servicelayer'), using this to move into the directory, executing git command to find the difference between the commits and raising a flag. This way i have managed 3 Jenkins files in a single repository.
stage('Validation') { steps { //Moving in to the directory to execute the commands dir('servicelayer') { script { //Using the git command to check the difference between previous successful commit. ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} is an environment variable comes with GIT Jenkins plugin //There is a drawback though, if it is the first time you are running this job, this variable is not available and fails the build //For the first time i had to use ${env.GIT_COMMIT} itself at both places to pass the build. A hack but worth it for future builds. def strCount = sh(returnStdout: true, script: "git diff --name-only ${env.GIT_COMMIT} ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} | grep servicelayer | wc -l").trim() if(strCount=="0") { echo "Skipping build no files updated" CONTINUE_BUILD = false } else { echo "Changes found in the servicelayer module" } } } } }
OLD ANSWER:
You can do it in 2 ways:
a) Configure your build jobs by adding "Additional Behaviour" -> "Polling ignore commits in certain region" This behaviour will let you add "Whitelisted regions" or "Blacklist the regions" which you do not want to poll for triggering the build job.
b) Write a custom shell script to verify the files changed per commit and verify the location. This shell script can then be added to your Jenkinsfile and be it Declarative or Scripted you can tweak the behaviour accordingly.
I will recommend option a) as it is simpler to configure and maintain as well. Hope this helps.
I'd suggest using the multi-branch pipeline plugin, then specifying the path to the appropriate jenkinsfile. So you'd have three pipeline jobs, one for app, lib1, and lib2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With