Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple projects sharing a jenkinsfile

Tags:

I have multiple projects with similar build steps and I am looking into reusing a Jenkinsfile pipeline across these projects. I am having a hard time to find documentation on how to implement such an standard (to my opinion) setup.

Here are my requirements :

1) a Jenkinsfile is stored in repo, shared across multiple projects

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

3) Each project should be independent in Jenkins from a user perspective at least, meaning for example the executions and logs are available in each project's entry in Jenkins

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

I initially thought about replicating my pipeline job (meaning copy the job including parameters definition, Repository and credential & jenkinfile location definition), the problem I am having with this idea is that every single time I run the job, the pipeline is erasing the parameters default values

e.g. defining a projectSvnPath property in the Jenkinsfile with NO default value will erase my job parameter projectSvnPath value in Jenkins. For that reason I was not able to use this option.

properties([
  parameters([
    string(name: 'projectSvnPath',      description: '*', )
   ])
])
like image 293
NicolasW Avatar asked Jun 13 '17 21:06

NicolasW


1 Answers

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=test
host.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
    '''
like image 191
terrasson marc Avatar answered Sep 30 '22 15:09

terrasson marc