简体   繁体   中英

Jenkins multibranch pipeline with multiple repo to build for the changed repo only

So, I have really a complex repository structior (atleast what I think), and I need to achieve CI-CD there. All my webhooks and setting are complete and now I am stuck with the deployment parts.

So I have 4 different repos which ultimately builds a tar file for the whole production.

Repo1: https://gitlab.example.com/repo1
Repo2: https://gitlab.example.com/repo2
Repo3: https://gitlab.example.com/repo3
Repo4: https://gitlab.example.com/repo4

I have JenkinsFile at the root of every repo (its kind of same Jenkinsfile under every project).

Here is snip of my git checkout pipeline script:

    properties ([
    [$class: 'GitLabConnectionProperty', gitLabConnection: 'gitlab'],
    pipelineTriggers([[
        $class: "GitLabPushTrigger",
        triggerOnPush: true,
        triggerOnMergeRequest: true,
        // triggerOpenMergeRequestOnPush:"both",
        triggerOnNoteRequest: true,
        noteRegex: "CI BUILD THIS AGAIN",
        // skipWorkInProgressMergeRequest: true,
        // ciSkip:false ,
        //setBuildDescription: true,
        addNoteOnMergeRequest:true,
        addCiMessage: true,
        addVoteOnMergeRequest: true,
        acceptMergeRequestOnSuccess: true,
        //branchFilterType: "NameBasedFilter",
        //targetBranchRegex: "feature-decc-changes",
        //includeBranchesSpec: "feature-decc-changes,master,stage,prod",
        excludeBranchesSpec: "dev"
    ]])
])

stage('CHECKOUT: Checkout the Repos '){
        steps {
            checkout([$class: 'GitSCM', branches: [[name: "**"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/node']],
                        submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
                        url: 'git@gitlab.example.com:repo1.git']]])

            checkout([$class: 'GitSCM', branches: [[name: "**"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/angular']],
                        submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
                        url: 'git@gitlab.example.com:repo2.git']]])

            checkout([$class: 'GitSCM', branches: [[name: "**"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/common']],
                        submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
                        url: 'git@gitlab.example.com:repo3.git']]])

            checkout([$class: 'GitSCM', branches: [[name: "**"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/extra']],
                        submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
                        url: 'git@gitlab.example.com:repo4.git']]])
        }

}

Here for any change in in any of the branch, it is creating the build and randomly checking out any of the branch from the repo and creating the build.

But I want jenkins to checkout the branch which has changed just now. For exa. if I pushed a change to "feature-abc" branch, under repo2, it should checkout that branch "feature-abc" from repo2 and all other repo. Also if feature-abc branch doesn't exists, checkout from a custom repo (eg. default master).

Please let me know if I am not clear. I will try to provide more information.

Based on what I understood... A push on a branch in a repo and you will build all the same branches in all the other repos and this repo

Try env.BRANCH_NAME it will give you the branch that is being checked out(when a push occurs) ...using that you can check out the other repos...

When checking out other branches check if the branch is existing or not git ls-remote --heads git@github.com:user/repo.git existingbranch it will return a hashCode if it exists, if not get it from your custom repo

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM