Multiple projects sharing jenkinsfile

I have several projects with similar build steps and am considering reusing the Jenkinsfile pipeline in these projects. I am having a hard time finding documentation on how to implement such a standard (in my opinion) setup.

Here are my requirements:

1) Jenkins file is stored in a repo shared by multiple projects

2) Each project has its own parameter: the location of the project in the repo.

3) Each project should be independent in Jenkins from a user point of view, at least which means, for example, execution and logs are available in every project entry in Jenkins

How can I achieve this? based on How do pipeline options and jenkins GUI options work together? I understand that I can use freestyle, but the magazines are not directly accessible with this option. I was also suggested to use Jenkinsfile in each of these independent jobs, but this sounds like too much unnecessary configuration in my opinion.

I originally thought about replicating my work with the pipeline (which means copying the job, including defining the options, defining the repository and credentials, and locating the jenkinfile), the problem I run into with this idea is that every time I I start the job, the pipeline erasing the default parameter values

eg. defining a property projectSvnPath

in the Jenkinsfile without the default NO will erase the value of the job parameter projectSvnPath

in Jenkins. For this reason, I was unable to use this parameter.

properties([
  parameters([
    string(name: 'projectSvnPath',      description: '*', )
   ])
])

      

+3


source to share


2 answers


Jenkins has the concept of shared libraries, allowing multiple assemblies to share common pipeline instructions without replicating code. see https://jenkins.io/doc/book/pipeline/shared-libraries/ for details .

At this point, it remains unclear how to allow parameter overrides every time the Jenkins file is reloaded. Sounds like an odd design choice.



Update 07/13/17 All my parameters are defined in my pipelines to avoid parameter issues. I prefer to have the parameters defined in my code repository, but I can foresee cases where this will not be practical.

Update 04/18 I gave up on using shared libraries - the reason is that it could not be tested. It won't work in developer environments (say your PC), so any development needs to be redirected to your repo in order to do any testing. This is not a sustainable approach for any kind of software development. At the moment my jenkinsfiles are duplicated because I haven't found a workaround for this using jenkins.

0


source


I had the same answer on a different thread , but the question was different. It is up to the moderators to decide if its a duplicate question and if this answer is invalid, as it will be found more easily searchable.

You can use the Pipeline Shared Groovy Library Plugin to have a library that all your projects use in a git repository. You can read more about this in the documentation .

If you have many pipelines that are mostly similar, the globals mechanism provides a handy tool for creating a higher level DSL that reflects the similarities. For example, all Jenkins plugins are built and tested the same way, so we can write a step called buildPlugin:

// vars/buildPlugin.groovy
def 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 is loaded as a global shared library or as a folder level shared library, the resulting Jenkinsfile will be significantly simpler:

Jenkinsfile (script)

buildPlugin {
    name = 'git'
}

      

This example shows the jenkinsfile passing name = git to the library. I am currently using a similar setup and I am very happy with it.

0


source







All Articles