Reusing stages of a jenkins pipeline in multiple jobs
<p>My team is moving to Jenkins 2 and I am using the pipeline plugin so that our build can live in our repository.  Because getting repositories allocated has lots of overhead in our company we have a single respository with many sub-projects &amp; sub-modules in it.  </p>

<p><strong>What I want</strong> is separate builds and reporting of Junit/checkstyle/etc reports for each sub-module as well as a final "build and deploy" step for each sub-project putting it all together.  </p>

<p><strong>My current plan</strong> is to create separate jobs for each sub-module so that they get their own junit/checkstyle/etc reports page.  Then have a multi-job project to orchestrate the sub-module builds for the sub-projects.  Since all of the sub-projects are simple jar builds, I want to put bulk of the logic in a common file, lets call it JenkinsfileForJars at the root of the sub-project.  So the repo structure is</p>

<ul>
<li>sub-project

<ul>
<li><em>JenkinsfileForJars.groovy</em></li>
<li>sub-moduleA

<ul>
<li><em>Jenkinsfile</em></li>
</ul></li>
<li>sub-moduleB

<ul>
<li><em>Jenkinsfile</em></li>
</ul></li>
</ul></li>
</ul>

<p>My Jenkinsfile contains </p>

<pre><code>def submoduleName = "submoduleA"
def pipeline
node {

    pipeline = load("${env.WORKSPACE}/subproject/JenkinsfileForJars.groovy")

}
pipeline.build()
pipeline.results()
</code></pre>

<p>And my JenkinsfileForJars contains</p>

<pre><code>def build() {

    stage('Build') {
        // Run the maven build
        dir("subproject") {
            sh "./gradlew ${submoduleName}:build"
        }

    }
}
def results() {

    stage('Results') {
        dir("subproject/${submoduleName}") {
            junit 'build/test-results/TEST-*.xml'
            archive 'build/libs/*.jar'
            publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'build/reports/cobertura/', reportFiles: 'frame-summary.html', reportName: 'Cobertura Report'])
            publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'build/reports/findbugs/', reportFiles: 'main.html', reportName: 'Fidbugs Report'])
            publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'build/reports/pmd/', reportFiles: 'main.html', reportName: 'PMD Report'])
            step([$class: 'CheckStylePublisher', pattern: 'build/reports/checkstyle/main.xml', unstableTotalAll: '200', usePreviousBuildAsReference: true])
        }
    }

}

return this;
</code></pre>

<p>When I run the Jenkinsfile above I get the following error:</p>

<pre><code>Running on master in /var/lib/jenkins/workspace/jobA
[Pipeline] {
[Pipeline] load
[Pipeline] { (/var/lib/jenkins/workspace/jobA/subproject/JenkinsfileForJars.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.NullPointerException: Cannot invoke method build() on null object
</code></pre>

<p>As far as I can tell, I am following what is shown in the documents for <a href="https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#triggering-manual-loading" rel="noreferrer">loading manual scripts</a> and the <a href="https://github.com/jenkinsci/workflow-plugin-pipeline-demo/blob/641a3491d49570f4f8b9e3e583eb71bad1aa493f/flow.groovy" rel="noreferrer">example given</a> for a loaded script.  I do not understand why my script is null after the load command.</p>

<p><strong>How do I get my Jenkinsfile to load JenkinsfileForJars.groovy?</strong></p>