How to include multiple pipeline scripts into jenkinsfile

Uday Vishwakarma picture Uday Vishwakarma · Dec 12, 2017 · Viewed 9.1k times · Source

I have a jenkins file as below

pipelineJob('My pipeline job'){
displayName('display name')
logRotator {
    numToKeep(10)
    daysToKeep(30)
    artifactDaysToKeep(7)
    artifactNumToKeep(1)
}
definition{
    cps {
        script(readFileFromWorkspace('./cicd/pipelines/clone_git_code.groovy'))
        script(readFileFromWorkspace('./cicd/pipelines/install_dependencies_run_quality_checks.groovy'))
    }
}
}

with above jenkinsfile the last script file is replacing other scripts. Basically I have split tasks into multiple groovy files so that i wont repeat the same code in all jenkinsfile and reuse the same for other jobs as well, like I can now use the clone_git_code.groovy script in dev build as well as QA builds.

Answer

Daniel Majano picture Daniel Majano · Dec 12, 2017

You have to use shared libraries (https://jenkins.io/doc/book/pipeline/shared-libraries/). You can define multiple groovy files with classes to return a processed object or simply creating calls with method where you define a step and the execution will be sequential.