sorin
sorin

Reputation: 170846

How to manage multiple Jenkins pipelines from a single repository?

At this moment we use JJB to compile Jenkins jobs (mostly pipelines already) in order to configure about 700 jobs but JJB2 seems not to scale well to build pipelines and I am looking for a way to drop it from the equation.

Mainly i would like to be able to have all these pipelines stored in a single centralized repository.

Please note that keeping the CI config (Jenkinsfile) inside each repository and branch is not possible in our use case, we need to keep all pipelines in a single "jenkins-jobs.git" repo.

Upvotes: 10

Views: 9953

Answers (3)

wahdan
wahdan

Reputation: 1258

as @Juh_ said, you can use jenkins shared libraries, here is a complete steps, Suppose that we have three branches:

  • master
  • develop
  • stage

and we want to create a single Jenkins file so that we can change in only one place. All you need is creating a new branch ex: common. This branch MUST have this structure. What we are interested for now is adding a new groovy file in vars directory, ex: common.groovy. Here we can put the common Jenkins file that you wish to be used across all branches.

Here is a sample:

def call() {
 node {
        stage("Install Stage from common file") {
             if (env.BRANCH_NAME.equals('master')){
                   echo "npm install from common files master branch"
             }
             else if(env.BRANCH_NAME.equals('develop')){
                   echo "npm install from common files develop branch"
             }
        }
        stage("Test") {
            echo "npm test from common files"
        }
    }
}

You must wrap your code call function in order to be used in other branches. now we have finished work in common branch we need to use it in our branches. go to any branch you wish to use this pipline ex: master and create Jenkinsfile and put this one line of code:

common()

This will call the common function that you have created before in common branch and will execute the pipeline.

Upvotes: -1

Juh_
Juh_

Reputation: 15599

I think this is the purpose of jenkins shared libraries

I didn't dev such library my-self but I am using some. Basically:

  • Develop the "shared code" of the jenkins pipeline in a shared library
    • it can contains the whole pipeline (seq of steps)
  • Add this library to the jenkins server
  • In each project, add a jenkinsfile that "import" those using @Library

Upvotes: 1

Benjamin
Benjamin

Reputation: 217

As far as I know this is not possible yet, but in progress. See: https://issues.jenkins-ci.org/browse/JENKINS-43749

Upvotes: 3

Related Questions