簡體   English   中英

Jenkins:如何將一些常見文件從一個倉庫合並到另一個倉庫

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

2 個 repos,repo1 和 repo2,具有不同的文件夾結構。 兩個存儲庫中都存在一些常見的源文件。 構建成功后,需要將repo1的通用文件中的更改文件合並到repo2。

到目前為止,這是我所做的,但它並不完整,我被困在如何進一步前進; 有沒有更好的方法來實現這一目標?

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
        """
    }
}

如何處理合並沖突情況,當 jenkins 嘗試合並時,repo2 中的常用文件可能被其他人編輯。 我在想的是記錄合並沖突並讓用戶通過修復合並沖突並重新觸發 jenkins 作業來處理它。

有沒有更好的方法來實現這一目標?

是的。 Git 子樹

  • 所有共享文件移動到位置
  • 更改並驗證移動源上的所有引用
  • 從共享資源文件夾創建3-rd repo
  • 從兩個存儲庫中刪除共享資源文件夾
  • 將帶有 git 子樹的資源存儲庫添加到資源文件夾的前位置到兩個存儲庫中
  • 選擇所有
  • 玩得開心

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM