Search code examples
jenkinsjenkins-pipelinecobertura

Add additional stage to jenkinsfile along with importing jenkins-shared-libraries code


Our project uses a jenkins-shared-library which has generic pipeline stages. We are looking at adding a stage that will inspect the code coverage and fail the pipeline if the coverage targets aren't met. The Cobertura plugin available with jenkins is capable of doing so, but I am facing challenges implementing it. Is there a way to add a custom pipeline stage in our jenkinsfile that will run after the shared-library code is run all as part of the same pipeline? Is it possible to import 2 shared libraries and use both together as part of the same pipeline? I am relatively new to this and any help is greatly appreciated. Thanks!


Solution

  • To answer your questions :
    Is there a way to add away to add a custom pipeline stage in our jenkinsfile that will run after the shared-library code is run all as part of the same pipeline?
    Is it possible to import 2 shared libraries and use both together as part of the same pipeline ?
    Yes to both of the question, you can add any number of custom stages in your pipeline and also it can run after the shared-library code is running.
    Example of Jenkinsfile and new shared library file as below:

    # stagelibrary variable will be used later to contain old_stagelibraries and is filled in # stage ('Old stage')
    def oldstagelibrary
    
    # newstagelibrary variable will contain path of your new sharedlibrary
    def newstagelibrary
    stage('Old stage') {   
                steps {
                  script {
                            // Load Shared library Groovy file old_stagelibraries.Give your path of old_stagelibraries file which is created
                            oldstagelibrary = load 'C:\\Jenkins\\old_stagelibraries'
                            // Execute your function available in old_stagelibraries.groovy file.
                            oldstagelibrary.MyOld_library()       
                          }               
                      }
            }
    # Add your new stage in the Jenkinsfile and use your new_stagelibraries file that is created
    stage('New stage') {   
                steps {
                  script {
                            // Load Shared library Groovy file new_stagelibraries which will contain your new functions.Give your path of new_stagelibraries file which is created
                            newstagelibrary = load 'C:\\Jenkins\\new_stagelibraries'
                            // Execute your function MyNew_library available in new_stagelibraries.groovy file.
                            newstagelibrary.MyNew_library()       
                          }               
                      }
            }
    

    Create a file named : new_stagelibraries(groovy file)

    #!groovy
    // Write or add Functions(definations of stages) which will be called from your jenkins file
    def MyNew_library()
     {
         echo "Function execution of MyNew_library"
         // You can add yoiur functionality here
     }
    
    return this