Did you ever face the problem of building a feature over multiple repositories? If you start testing the single repository, you may have unknown side effects when your code is rolled out. On the other hand, if you start testing the repositories together, you may have an inconsistent state of one of your code changes. This will lead to either errors in the test suite or wrong testing results. Broken tests, wrong test results and unknown side effects cause a pipeline to miss its validity. Developing effort and the risk of loss of quality increases significantly, but you can solve the situation with building up a multiple repositories pull request test.
1. How do we prepare the test?
The solution is as simple as complicated: Your pull request has to know, which code change belongs to a specific feature. While using three different repositories, how can you achieve to test all changes at once? Three things are necessary:
- GitHub Organizational Plugin,
- Pipeline plugin,
- naming convention.
2. Why do we need those things?
You can use the Github Organizational Folder Plugin to check your repositories for new pull requests. This plugin will check every affected repository for changes and activates the Jenkinsfile in the main directory, if a change has been made.
You can create a Jenkinsfile for a repository, which will be triggered after a pull request. The task of this Jenkinsfile is mainly to check, if branches with the same name from the same developer in other repositories exist and then uses them for the PR tests, if a change was detected.
You have to agree to naming conventions, that means you have to set the name for all affected branches in the different repositories. While working with pull requests, you need to find out the branch name first. Luckily, you can get the pull request number directly from the trigger. You need to GET the PR information from Github:
As you now have the name of the branch, you need to do the same for the remote name of the developer, who created the pull request.
How can we now ensure, that the pull request is only triggered once and not for every repository, where a Pull request was set from a developer?
The solution is to build a hierarchy into the repository structure. As every repository has a descriptive Jenkinsfile, we need a kind of control repository.
This is important to avoid uncontrollable starts of the PR_Test. If a Pull request exists there, the other repositories don’t have to activate their tests as the control repository will do it.
Other repositories have to check, if a branch with the given name exists in the control repository. If the branch was found, the control repository will start the test. If the branch doesn’t exist, one of the other repositories has to trigger the PR_test.
One way or another, we will start to build a new job called ‘PR_Test’.
3. Environment set up, how to start the test?
To be able to work with different repositories, you need a global job as pull request job. It is easy to start from different repositories, as you only have to hand over variables such as branch and remote name. Beginning with the tests, we have to set up our environment first. This will be done in parallel. It can be seen as the first test to assure that at least the installation is still functional. After that, your tests should run as expected. Beginning with short running tests like unit tests up to longer tests such as UI tests (if a UI exists). As the tests are built up in stages, the job PR_Test will fail and the result can be send to the developer.
4. Summary
With the help of different new plugins and pipeline scripting, you have a simple way to easily build complex structures like a Pull request chain. The preparations of your test can be automated across independent repositories. Your advantage: You don’t have to set up the environment manually, which decreases the testing effort. You have a higher warranty (depending on your tests suite), that multi branch features work as expected in the end product. At the end, this will save a lot of development time and increase the software quality significantly.