TL;DR Is there a way to import code into the Jenkinsfile
from the local repository (other than the load
step)?
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.
load
Step: Allow for loading groovy scripts from the repository.Jenkinsfile
and import this library into the Jenkinsfile
?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
Workaround:
library identifier: 'shared-library@version', retriever: legacySCM(scm)
The approach currently taken in PR 37 will not work properly with build agents, and anyway will only work for scripts using the library
step, not the @Library
annotation.
By the way files loaded from the load
step do appear in Replay. But it is true that your script cannot statically refer to types defined in such files. In other words, you could simulate library vars/*.groovy
but not src/**/*.groovy
—the same limitation as the current PR 37.