简体   繁体   English

Jenkins:如何将一些常见文件从一个仓库合并到另一个仓库

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

2 repos, repo1 and repo2, having different folder structure. 2 个 repos,repo1 和 repo2,具有不同的文件夹结构。 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.构建成功后,需要将repo1的通用文件中的更改文件合并到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.如何处理合并冲突情况,当 jenkins 尝试合并时,repo2 中的常用文件可能被其他人编辑。 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.我在想的是记录合并冲突并让用户通过修复合并冲突并重新触发 jenkins 作业来处理它。

is there a better way to achieve this?有没有更好的方法来实现这一目标?

Yes.是的。 Git subtree Git 子树

  • Move all shared files into new location所有共享文件移动到位置
  • Change and verify all references on moved sources更改并验证移动源上的所有引用
  • Create 3-rd repo from shared resources-folder从共享资源文件夹创建3-rd repo
  • Delete shared resources-folder from both repos从两个存储库中删除共享资源文件夹
  • Add resources-repo with git subtree to the former location of resources-folder into both repos将带有 git 子树的资源存储库添加到资源文件夹的前位置到两个存储库中
  • Check all选择所有
  • Have fun玩得开心

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个仓库合并一个分支并排除一些文件? - How to merge a branch from another repo and exclude some files? 如何使用 Jenkins 将代码从一个 Git 存储库 A 合并到 Git 存储库 B - How to merge code from one Git repo A to Git repo B using Jenkins 如何将一些文件从一个 git 存储库移动到另一个(不是克隆),保留历史记录 - How to move some files from one git repo to another (not a clone), preserving history 如何在没有一些文件的情况下将一个分支合并到另一个分支? - How to merge one branch to another without some files? 如何仅合并来自另一个分支的某些文件 - How to merge only some files from another branch 在没有共同祖先的情况下如何将提交从一个git repo移动到另一个git repo - How to move commits from one git repo to another when there is no common ancestor 使用不同的树结构将更改从一个repo合并到另一个repo - Merge changes from one repo to another with different tree structures 如何将文件从一个分支的目录合并到另一个分支? - How to merge files from one branch's directory into another branch? 如何从git仓库从一个文件夹到另一个已经包含文件但不是仓库的文件夹中获取文件? - How can I get files from a git repo from one folder to another folder that already contains files, but is not a repo? 从另一个仓库git合并 - merge from another repo git
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM