Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple jenkinsfile in one repository

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. enter image description here

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.

like image 974
Amrit Gill Avatar asked Mar 23 '18 10:03

Amrit Gill


People also ask

Can a repo have multiple Jenkinsfile?

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.

Can a Jenkinsfile have multiple pipelines?

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.

Can we have multiple Jenkins file?

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.

What is multi branching 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.


2 Answers

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.

Additional Behaviour

like image 157
Sorabh Mendiratta Avatar answered Sep 19 '22 09:09

Sorabh Mendiratta


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.

enter image description here

like image 30
iheanyi Avatar answered Sep 19 '22 09:09

iheanyi