Reuse stages of the Jenkins pipeline at multiple workstations

My team moves on to Jenkins 2, and I use the pipeline plugin so that our assembly can live in our repository. Since there are a lot of overhead costs in the distribution of repositories in our company, we have a single repository with many subprojects and submodules.

What I want is separate assemblies and report reports of Junit / checkstyle / etc for each submodule, as well as the final build and deployment step for each subproject combining all this together,

My current plan is to create separate jobs for each submodule so that they get their own junit / checkstyle / etc page. Then you have a project with several tasks for organizing the assembly of submodules for subprojects. Since all subprojects are simple jar assemblies, I want to highlight the main part of the logic in a common file, let her name JenkinsfileForJars in the root of the subproject. So the repo structure

  • subproject
    • JenkinsfileForJars.groovy
    • sub-ModuleA
      • Jenkinsfile
    • sub-moduleB
      • Jenkinsfile

My Jenkins file contains

def submoduleName = "submoduleA"
def pipeline
node {

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

}
pipeline.build()
pipeline.results()

And my JenkinsfileForJars contains

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;

When I run the Jenkins file above, I get the following error:

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

, , , script. , script null load.

Jenkinsfile JenkinsfileForJars.groovy?

+4
2

SCM, . groovy , , /, .

def pipeline
node( 'myAgentLabel' ) {
    stage ( 'checkout SCM' ) {
        checkout([
            $class: 'GitSCM'
            ,branches: scm.branches
            ,extensions: scm.extensions 
                + [[ $class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', trackingSubmodules: false]]
            ,doGenerateSubmoduleConfigurations: false
            ,userRemoteConfigs: scm.userRemoteConfigs
        ])
        pipeline = load( "${env.WORKSPACE}/path/to/submodule/myGroovyFunctions.grooovy" )
    }
    pipeline.build()
}

, scm. * Jenkins (In-process script)

+4

:

  • node. , .
  • node. , , ( ) node, , ( () ).

(, , .)

0

Source: https://habr.com/ru/post/1657776/


All Articles