2016-06-01 92 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]) 
相关问题