Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple pipelines in one branch with Jenkinsfile

Is there a way to create two different pipelines for one branch repository? Currently I have one Jenkinsfile and it is manually built using the Jenkins GUI. I want to create a second Jenkinsfile that will also be built trough the Jenkins GUI but I do not know how to have both of these pipelines in the same branch and build whichever I want.

like image 840
Radoslav Hubenov Avatar asked Aug 20 '18 14:08

Radoslav Hubenov


People also ask

Can a Jenkinsfile have multiple pipelines?

Ultimately, a branch's Jenkinsfile describes the Pipeline for that branch. Fundamentally, there can be only one Jenkinsfile, and therefore one Pipeline, per Git repository branch.

How do I create a multi branch pipeline in Jenkins?

Step 1: Open Jenkins home page ( http://localhost:8080 in local) & click on New Item from the left side menu. Step 2: Enter Jenkins job name & choose the style as multibranch pipeline and click OK. Step 3: In the configure page, we need to configure only one thing – The Git Repo source.

Why do we need multi branch pipeline in Jenkins?

The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.


1 Answers

You can use declarative pipeline to do this. Create two files in your repo say firstPipeline.groovy and secondPipeline.groovy as follows and configure your jenkins job with the one which you want to build:

enter image description here

firstPipeline.groovy:

pipeline {
    stages {
        stage('stage 1') {
            steps {
                script{
                    //do your things of first pipeline stage
                }
            }
        }
        stage('stage 2') {
            steps {
                script{
                    //do your things of first pipeline stage
                }
            }
        }
    }
}

secondPipeline.groovy:

pipeline {
    stages {
        stage('stage 1') {
            steps {
                script{
                    //do your things of second pipeline stage
                }
            }
        }
        stage('stage 2') {
            steps {
                script{
                    //do your things of second pipeline stage
                }
            }
        }
    }
}
like image 132
awefsome Avatar answered Oct 21 '22 10:10

awefsome