简体   繁体   中英

Jenkins: How to merge some common files from one repo to another

2 repos, repo1 and repo2, having different folder structure. There are some common source files exist in both repos. After successful build, need to merge changed files from the common files from repo1 to repo2.

Here's what I've so far and it's not complete and I'm stuck on how to move further; is there a better way to achieve this?

def filesNames      = ""
def filesNamesList  = ""
def files2push      = []
def branch          = env.GERRIT_PATCHSET_REVISION     
//files that need to be pushed from repo1 to repo2 once build is successful
def getFileMap(file){
    def repo1Repo2FileMap = [:]
    def repo1Repo2FileMap = ["dir1/dir2/file1.h" : "dir1/file1.h",
                            "fldr1/fldr2/file2.h" : "fldr2/file2.h"
                            ]
    return  repo1Repo2FileMap."$file"
}
//get changed files list
def getChanged(branch){
    def headRevision = sh (script: "git ls-remote | grep refs/heads/${branch} | head -n 1 | awk \'{print \$1}\'",
                            returnStdout: true)

    headRevision = headRevision.replaceAll("[\n\r]","")
    def getChangedList = sh (
        returnStdout: true,
        script: "git diff --name-only  $headRevision"
    ).trim().readLines().collect()
    if(getChangedList !=""){
        files = readFile(file: getChangedList)
        fileName = files.readLines()
        fileNameList = fileName.join(" ")
    }
    return changedFileNameList
}
stage("Push files to repo2"){
    
    getChanged(branch).each{
        if(getFileMap(it)){
            //eg: cp dir1/dir2/file1.h dir1/file1.h
            cp $file $it
            files2push.add(it)
        }
    }
}
stage("Merge Changed files to repo2"){
    dir("repo2"){
        bat """
            git add $files2push
            git commit -m "commit msg"
            git push
        """
    }
}

How to handle merge conflict case, common files in repo2 could be edited by someone else when jenkins tries to merge. What I'm thinking is to log the merge conflict and let the user handle it by fixing the merge conflict and retriggering the jenkins job.

is there a better way to achieve this?

Yes. Git subtree

  • Move all shared files into new location
  • Change and verify all references on moved sources
  • Create 3-rd repo from shared resources-folder
  • Delete shared resources-folder from both repos
  • Add resources-repo with git subtree to the former location of resources-folder into both repos
  • Check all
  • Have fun

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