TL;DR Is there a way to import code into the Jenkinsfile from the local repository (other than the load step)?
Why?
I've experienced that for complex builds the Jenkinsfile gets kind of bulky and not very maintainable.
Now that the build job is code, it would be wonderful to have the same means as for other code.
That is, I would like to divide it into smaller (more maintainable) units and unit test them.
What I tried
- shared libraries: allow for dividing our Jenkins Pipeline logic into smaller files in a separate module and even unit test it.
However, they need to be in different repository and (if not on GitHub) must be configured into Jenkins. loadStep: Allow for loading groovy scripts from the repository.
However, the files must be scripts and not "full" groovy classes, making it difficult to have multiple files or classes that depend on each other. For example inheritance is impossible.
In addition, theses files are not displayed when doing a replay on a Jenkins job, which makes them hard to develop and debug.
My Questions
- Is there a way (or workaround) to create a shared library in the same repository as the
Jenkinsfileand import this library into theJenkinsfile? - Or is there even another way I haven't tried, yet?
Example directory structure
Similar to the directory structure described for shared libs I would like to have the following in a single repository.
(root)
+- someModule
| +- ...
+- jenkins # Classes/Scripts used by Jenkins in a separate module
| +- src # Groovy source files
| +- org
| +- foo
| +- Bar.groovy # for org.foo.Bar class
| +- test # Groovy test files
| +- org
| +- foo
| +- BarTest.groovy # Test for org.foo.Bar class
| +- pom.xml or build.groovy # Build for local library
+- Jenkinsfile # Build "someModule", uses classes from "jenkins" module
Jenkinfileinto smaller (more maintainable) units and unit test them within one project, i.e. "next" to theJenkinsfile. – schnatterer Jan 26 '18 at 15:15