Multiple projects sharing a jenkinsfile Multiple projects sharing a jenkinsfile jenkins jenkins

Multiple projects sharing a jenkinsfile


I am working on the same pb :Sharing a pipeline across many projects

I am using jenkinsFile.properties (each project has its own)where I put my specific data :

apiName=testhost.Dev=localhost

Then I access my data in the jenkinsFile with

props = readProperties  file: 'jenkinsFile.properties'targetHost = props["host.${devHost}"]apiName = props["apiName"]

Then I can use them and work in a very crude way :

    sh '''        git clone ...        mvn -X clean install    '''


I had the same answer to a different thread but the question was another one. It's up to moderators to decide if its a duplicate question and if not this answer is valid as it will be found with search more easily.

You can use the Pipeline Shared Groovy Library plugin to have a library that all your projects share in a git repository. In the documentation you can read about it in detail.

If you have a lot of Pipelines that are mostly similar, the global variable mechanism provides a handy tool to build a higher-level DSL that captures the similarity. For example, all Jenkins plugins are built and tested in the same way, so we might write a step named buildPlugin:

// vars/buildPlugin.groovydef call(body) {    // evaluate the body block, and collect configuration into the object    def config = [:]    body.resolveStrategy = Closure.DELEGATE_FIRST    body.delegate = config    body()    // now build, based on the configuration provided    node {        git url: "https://github.com/jenkinsci/${config.name}-plugin.git"        sh "mvn install"        mail to: "...", subject: "${config.name} plugin build", body: "..."    }}

Assuming the script has either been loaded as a Global Shared Library or as a Folder-level Shared Library the resulting Jenkinsfile will be dramatically simpler:

Jenkinsfile (Scripted Pipeline)

buildPlugin {    name = 'git'}

The example shows how a jenkinsfile passes name = git to the library.I currently use a similar setup and am very happy with it.


Jenkins has the shared libraries concept to allow multiple builds to share common pipeline instructions without replicating code. details at https://jenkins.io/doc/book/pipeline/shared-libraries/.

At this point what remains unclear is how to allow parameters overrides to persist each time the Jenkinsfile is reloaded. Seems like a weird design choice.

Update 07/13/17All my parameters are defined in my pipelines to avoid the issue with parameters. I prefer to have parameters defined in my code repository but I can foresee cases where this won't be practical.

Update 04/18I gave up using shared libraries - the reason for it is that it cannot be tested. It won't run on dev environments (say your PC), so any development needs to be pushed to your repo basically to perform any testing. This is not a sustainable approach for any kind of software development. At the moment my jenkinsfiles are duplicated because I found no workaround for this using jenkins.