2016-06-01 131 views
0

我在Jenkins中使用了Groovy插件,並且想要操作一些Git存儲庫。從Jenkins的Groovy系統腳本中執行Git操作

我想:

  • 在回購A,結賬提交X
  • 在回購B,結賬提交Y
  • 在回購 C
  • ,作出承諾,推動

我很高興如果有人能夠指出我如何使用Git插件(在Groovy中)做到這一點,或者如何調用系統命令在一個特定的路徑(如"git checkout X".execute(in_this_path)將是太棒了。

回答

0

挑戰在於改變工作目錄,所以你可以在不同路徑上的多個git存儲庫上工作。爲了解決這個問題,我使用了具有目錄(File directory)方法的java.lang.ProcessBuilder,它改變了工作目錄。這裏是一個完整的例子:

//executes the command in the working dir 
//and prints the output to a log file 
def runProcess(workingDir, command) { 
    ProcessBuilder procBuilder = new ProcessBuilder(command); 
    procBuilder.directory(workingDir); 
    procBuilder.redirectOutput(new File("${workingDir}/groovyGit.log")) 

    Process proc = procBuilder.start(); 

    def exitVal = proc.waitFor() 
    println "Exit value: ${exitVal}" 

    return proc 
} 

//configuring the working dir for each git repository 
def repoA = "repo A working dir" 
def repoB = "repo B working dir" 
def repoC = "repo C working dir" 

//configuring the wanted revision to checkout for each repository 
def repoARevision = "repo a revision" 
def repoBRevision = "repo b revision" 

def commitMsg = "commit msg" 

//checkout A 
runProcess(new File(repoA), ["git", "checkout", repoARevision]) 

//checkout B 
runProcess(new File(repoB), ["git", "checkout", repoBRevision]) 

//check status before commit 
runProcess(new File(repoC), ["git", "status"]) 

//commit 
runProcess(new File(repoC), ["git", "commit", "-a", "-m", commitMsg]) 
相關問題