如何在 Groovy 脚本(windows)中使用 GIT 命令(diff、log 等)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45956629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:53:17  来源:igfitidea点击:

How to use GIT commands (diff, log, etc.) in Groovy script (windows)

gitjenkinsgroovy

提问by rock

I just started learning DevOps and have a query. It might be very basic so please don't mind.

我刚开始学习 DevOps 并有一个疑问。这可能是非常基本的,所以请不要介意。

Setup: Jenkins, GIT, Groovy, Java are installed on single windows server.

设置:Jenkins、GIT、Groovy、Java 安装在单个 Windows 服务器上。

My Goal is to write a Groovy script which will do following: 1. Execute GIT commands (on local GIT repository) to pull some data (result). 2. Take further actions based on above result.

我的目标是编写一个 Groovy 脚本,它将执行以下操作: 1. 执行 GIT 命令(在本地 GIT 存储库上)以提取一些数据(结果)。2. 根据以上结果采取进一步措施。

Query: How to execute GIT commands in Groovy script? What all is needed? Would be great if someone can please share a sample basic script.

查询:如何在 Groovy 脚本中执行 GIT 命令?什么都需要?如果有人可以分享一个示例基本脚本,那就太好了。

回答by Juan Sebastian

On a broader spectrum, what you want to achieve is just call linux commands from groovy, now regarding that:

在更广泛的范围内,您想要实现的只是从 groovy 调用 linux 命令,现在关于:

There are 3 ways out of this, either you can just call the git commands from a shell script (since i understand you want to use jenkins for this), use some sort of git jenkins plugin, or if you absolutely want to use groovy for it, you can take a look at this question Groovy executing shell commands, to summarize, you can do the following:

有 3 种方法可以解决这个问题,您可以只从 shell 脚本调用 git 命令(因为我知道您想为此使用 jenkins),使用某种 git jenkins 插件,或者如果您绝对想使用 groovy它,你可以看看这个问题Groovy execution shell commands,总结一下,你可以做到以下几点:

def proc = "git command args".execute()
def b = new StringBuffer()
proc.consumeProcessErrorStream(b)

println proc.text
println b.toString()

on b you would have the errors of executing the linux command if there were any,

在 b 上,如果有的话,你会在执行 linux 命令时遇到错误,

Best Regards,

此致,

回答by daggett

check jenkins pipeline examples

检查詹金斯管道示例

https://jenkins.io/doc/pipeline/examples/

https://jenkins.io/doc/pipeline/examples/

simplest pipeline with git:

使用 git 的最简单管道:

node {
    stage('Clone sources') {
        git url: 'https://github.com/jfrogdev/project-examples.git'
    }
}

git pipeline plugin doc:

git 管道插件文档:

https://jenkins.io/doc/pipeline/steps/git/

https://jenkins.io/doc/pipeline/steps/git/

回答by boney dsilva

In Jenikins>manageJenkins>Script Console

在 Jenikins>manageJenkins>Script Console

execute following command

执行以下命令

println(["git","--version"].execute().text)

println(["git","--version"].execute().text)