Bionix1441
Bionix1441

Reputation: 2319

How to have modular Jenkins Pipeline?

I would like to create a Jenkins declarative pipeline and would like to have the pipeline structure as following:

  mainPipeline.groovy
  stage1.groovy
  stage2.groovy
  stage3.groovy

mainPipeline looks like the following:

 pipeline {

      stages {
         stage('stage1') {
            // Call method from the file Stage1.groovy
         }
         stage('stage2') {
            // Call method from the file Stage2.groovy
         }
      }
 }

I have two main questions:

  1. How do I link these files to a Library?
  2. How do I configure Jenkins Pipeline, so that Jenkins not only knows the main JenkinsFile which is mainPipeline but also the submodules?

Upvotes: 3

Views: 2556

Answers (1)

crash
crash

Reputation: 670

I would not recommend to separate your Jenkinsfile into separate files, since there are better options:

  • You can execute jobs within your pipeline with Pipeline: Build Step plugin. Use this to execute stages that gonna be used by multiple jobs. For example I use this to deploy my applications in a common deploy job.
  • You can extend Jenkins with your own libraries, which you can load per job or for all jobs. See: Extending with Shared Libraries

For both methods the defining Jenkinsfiles/Groovy scripts can come from SCM.

If you really want to load script from the project path then check this question. If you want to use multiple Jenkinsfiles from the project path, you can just add more Jenkinsfiles as "Project Recognizers" when you configure the job.

Upvotes: 2

Related Questions