There may not be a way to make your idea work with submodules. When a Jenkins multibranch pipeline polls SCM for changes, it has no visibility into submodules. I verified this recently. The result in "Scan Multibranch Pipeline Log" was a Communication error for url
response. The Jenkinsfile was not found, and so no build was triggered.
The good news is that I discovered a straightforward way to achieve what we're after without using submodules. Have a look at the Remote Jenkinsfile Provider Plugin.
When a pipeline is polling a repository to trigger new builds, it looks for two things for each branch:
- The polled branch has new SCM changes since the last build (HEAD is different).
- A Jenkinsfile exists in repository X on branch Y at path Z.
In a multibranch pipeline's Build Configuration section, normally only one Mode is available to tell Jenkins where to find the Jenkinsfile: "by Jenkinsfile". This mode allows us to specify Z (path) only; X and Y are implicitly set to the repository and branch being polled.
What the Remote Jenkinsfile Provider Plugin gives us is a new Build Configuration Mode: "by Remote Jenkins File Plugin". This mode allows us to explicitly set the repository, branch, and path for the Jenkinsfile - X, Y, and Z. In your example, X would be ci-repo
, but any Jenkinsfile in any repository is fair game. You could have ci-repo
be a submodule of dev-repo
if it's convenient, but no such relationship is required to make this work.
If desired, a "Match branches" option uses the polled branch name for Y, looking for a remote Jenkinsfile on a branch of the same name. For example, while polling dev-repo/feature-123
, it would look for a Jenkinsfile on ci-repo/feature-123
.
As long as the Jenkinsfile exists at the specified location, the second criteria for triggering new builds will be satisfied. You'll see something like this in the pipeline's polling logs:
Looking up DR/dev-repo for branches
Checking branch master from DR/dev-repo
No local file defined. Skipping Source Code SCM probe, since Jenkinsfile will be provided by Remote Jenkins File Plugin
Met criteria
Scheduled build for branch: master
Instructions for how to set this up, with screenshots, can be found on the plugin's page.
Caveat: Pushing changes to a remote Jenkinsfile (ci-repo
) will not trigger any builds of the polling repository (dev-repo
). If this behavior is desired, one solution would be to use webhooks. Another would be to create a pipeline that polls the remote Jenkinsfile's repository and builds other pipeline jobs as desired using the build step, like so:
build(job: "dev-repo/${BRANCH_NAME}", wait: false)