<ol>
<li><p><a href="https://stackoverflow.com/a/9473207/1824868">Get a list of changed files</a> and use that to determine which tests to run.</p></li>
<li><p><a href="https://stackoverflow.com/a/38108449/1824868">Load external Groovy scripts at run time</a> in order to run your deploys.</p></li>
</ol>

CC BY-SA 4.0
2019-07-27T09:53:22.873Z
Please copy the relevant bits from the link and post them inside a quote block in order to prevent that information would be lost if the link will be deprecated. Perhaps you could also add your personal view: what option would you choose and why?
030
CC BY-SA 4.0
2021-02-07T03:51:45.637Z
@030 I didn't provide two competing options.  OP had two separate issues/questions labeled "1" and "2" so I gave one answer to each labeled "1" and "2".  Furthermore each of those questions is duplicated by questions over at SO, which is what I linked to.  I don't see the point in copy-pasting content from other SE sites since those questions and answers will be updated over time but my copy-paste will eventually become stale.
jayhendren
CC BY-SA 4.0
2018-06-25T17:58:43.730Z
2018-06-25T17:58:43.730Z
jayhendren
<p>You can use the "<a href="https://jenkins.io/doc/book/pipeline/syntax/#when" rel="noreferrer">when</a>" block combined with the built in "changeset" condition to conditionally run only certain stages of your monorepo's pipeline. From the when.changeset documentation:</p>

<p>changeset- 
Executes the stage if the build’s SCM changeset contains one or more files matching the given string or glob. Example: when { changeset "**/*.js" }</p>

<p>Here is an example Jenkinsfile using this strategy:</p>

<pre><code>pipeline {
    agent any
    stages {
        stage('build matchengine') {
            when {
                changeset "**/matchengine/*.*"
            }
            steps {
                echo 'building match engine'
            }
        }
        stage('build posttrade') {
            when {
                changeset "**/posttrade/*.*"
            }
            steps {
                echo 'building post trade'
            }
        }
    }
}
</code></pre>

<p>, applicable to the monorepo project structure shown below:</p>

<pre><code> .(my-project)
   |-- Jenkinsfile
   |-- matchengine
   |-- posttrade
   |-- serverless
   |-- ui
</code></pre>

<p>This strategy won't scale past small codebases because it would be hard to keep track of which modules depend one each other. You would be better off using a build system like Bazel. Your CI job would simply issue a bazel build //... (build everything), and Bazel would calculate what actually needs to be built, and what needs to be tested. Further, there even exist bazel rules such as rules_docker and rules_k8s which can calculate which of your containers need to be rebuilt and pushed to a container registry, and which of your applications need to be redeployed to Kubernetes.</p>

CC BY-SA 4.0
2020-02-16T09:25:47.270Z
Unfortunatelly changeset doesn't contain all changes from change requests, only delta between last two commits. However custom check can be easily implemented.
Dima
CC BY-SA 4.0
2021-02-23T20:55:29.910Z
the problem is if the build fails, the diff looks clean and a retry won't rebuild
FlavorScape
CC BY-SA 4.0
2021-02-24T14:22:56.123Z
yep I suggest Bazel in my answer, a build system built for this exact use case of only rebuilding / testing / deploying code which is affected by the commit
mancini0
CC BY-SA 4.0
2019-03-22T02:09:00.207Z
2019-07-31T22:04:21.493Z
2019-07-31T22:04:21.493Z
mancini0
<p>I'm in the process of converting multiple repositories into a single repository, our CI tool of choice is Jenkins due to the conversion of multiple repository structures into a single one 2 main issues have arisen.</p>

<ol>
<li><p>Build/test times have increased significantly as all the builds/tests have to be run for every single commit. This is partially alleviated by using a build tool, in our case we have gone with using Buck.</p></li>
<li><p>After all the tests associated with the committed code are run, I have a deployment Jenkinsfile for each project. How will I be able to only trigger the Jenkinsfiles for projects that need to be re-deployed? And if I am able to do so, <a href="https://devops.stackexchange.com/questions/2133/what-are-best-practices-for-creating-and-triggering-various-pipelines-regards-mo">is this a correct practice</a>?</p></li>
</ol>

CC BY-SA 4.0
2018-06-22T13:49:56.883Z
How are your build hooks being implemented? Do you poll the repository with Jenkins? Do you have git hooks in place on each commit?
Preston Martin
CC BY-SA 4.0
2018-06-25T01:38:13.450Z
We have githooks in place on each commit
YellowPillow
<jenkins><jenkins-pipeline><jenkinsfile>
Triggering specific pipeline builds for monorepos in Jenkins
YellowPillow
# Triggering specific pipeline builds for monorepos in Jenkins

(Asked by: username_0 on 2018-06-22)

I'm in the process of converting multiple repositories into a single repository, our CI tool of choice is Jenkins due to the conversion of multiple repository structures into a single one 2 main issues have arisen.

1. Build/test times have increased significantly as all the builds/tests have to be run for every single commit. This is partially alleviated by using a build tool, in our case we have gone with using Buck.
2. After all the tests associated with the committed code are run, I have a deployment Jenkinsfile for each project. How will I be able to only trigger the Jenkinsfiles for projects that need to be re-deployed? And if I am able to do so, [is this a correct practice](https://devops.stackexchange.com/questions/2133/what-are-best-practices-for-creating-and-triggering-various-pipelines-regards-mo)?

**Tags:** jenkins, jenkins-pipeline, jenkinsfile

> **Comments:**
> **username_1**: How are your build hooks being implemented? Do you poll the repository with Jenkins? Do you have git hooks in place on each commit?  
> **username_0**: We have githooks in place on each commit  

---

## 2 answers

### Answer by username_2 on 2018-06-25. Score: 5 | Accepted answer

1. [Get a list of changed files](https://stackoverflow.com/a/9473207/1824868) and use that to determine which tests to run.
2. [Load external Groovy scripts at run time](https://stackoverflow.com/a/38108449/1824868) in order to run your deploys.

> **Comments:**
> **username_3**: Please copy the relevant bits from the link and post them inside a quote block in order to prevent that information would be lost if the link will be deprecated. Perhaps you could also add your personal view: what option would you choose and why?  
> **username_2**: @username_3 I didn't provide two competing options.  OP had two separate issues/questions labeled "1" and "2" so I gave one answer to each labeled "1" and "2".  Furthermore each of those questions is duplicated by questions over at SO, which is what I linked to.  I don't see the point in copy-pasting content from other SE sites since those questions and answers will be updated over time but my copy-paste will eventually become stale.  

---

### Answer by username_4 on 2019-03-22. Score: 15

You can use the "when" block combined with the built in "changeset" condition to conditionally run only certain stages of your monorepo's pipeline. From the when.changeset documentation:
changeset- 
Executes the stage if the build’s SCM changeset contains one or more files matching the given string or glob. Example: when { changeset "**/*.js" }
Here is an example Jenkinsfile using this strategy:
pipeline {
    agent any
    stages {
        stage('build matchengine') {
            when {
                changeset "**/matchengine/*.*"
            }
            steps {
                echo 'building match engine'
            }
        }
        stage('build posttrade') {
            when {
                changeset "**/posttrade/*.*"
            }
            steps {
                echo 'building post trade'
            }
        }
    }
}

, applicable to the monorepo project structure shown below:
 .(my-project)
   |-- Jenkinsfile
   |-- matchengine
   |-- posttrade
   |-- serverless
   |-- ui

This strategy won't scale past small codebases because it would be hard to keep track of which modules depend one each other. You would be better off using a build system like Bazel. Your CI job would simply issue a bazel build //... (build everything), and Bazel would calculate what actually needs to be built, and what needs to be tested. Further, there even exist bazel rules such as rules_docker and rules_k8s which can calculate which of your containers need to be rebuilt and pushed to a container registry, and which of your applications need to be redeployed to Kubernetes.

> **Comments:**
> **username_5**: Unfortunatelly changeset doesn't contain all changes from change requests, only delta between last two commits. However custom check can be easily implemented.  
> **username_6**: the problem is if the build fails, the diff looks clean and a retry won't rebuild  
> **username_4**: yep I suggest Bazel in my answer, a build system built for this exact use case of only rebuilding / testing / deploying code which is affected by the commit  

---

