bash 如何生成更改日志:自上次 Hudson 构建以来的 git log?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2798703/
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-17 22:05:20  来源:igfitidea点击:

How to generate changelog: git log since last Hudson build?

gitbashcontinuous-integrationhudsonphing

提问by takeshin

I'm using Phing to do post build tasks in Hudson.

我正在使用 Phing 在 Hudson 中执行后期构建任务。

I want to generate changelog containing all commits since last successful Hudson build. But looks like neither Hudson nor Git plugin for Hudson does not provide %last_build_time%variable.

我想生成包含自上次成功构建 Hudson 以来所有提交的变更日志。但是看起来 Hudson 和 Hudson 的 Git 插件都没有提供%last_build_time%变量。

This would be satisfying solution, (but how to get the time?):

这将是令人满意的解决方案,(但如何获得时间?):

git log --pretty="%s" --since="%last_build_time%"

The only way I see for now is extracting it from the job xml file, but I do not know if it is possible with Phing.

我现在看到的唯一方法是从作业 xml 文件中提取它,但我不知道 Phing 是否可行。

How do you generate your change logs?

您如何生成更改日志?

采纳答案by takeshin

I have extracted last successful build date using bash:

我已经使用 bash 提取了上次成功构建的日期:

git log --pretty="%s" --since="`date -r ./../lastSuccessful/build.xml "+%F %T"`"

(In xml file I had to replace "with &quote;entity).

(在 xml 文件中,我不得不"&quote;实体替换)。

回答by levigroker

@takeshin's answer is fine if you have access to the build.xml file, but this may break, especially if you are building on a slave node (since the slave does not have the referenced build.xml).

如果您有权访问 build.xml 文件,@takeshin 的回答很好,但这可能会中断,特别是如果您在从属节点上构建(因为从属节点没有引用的 build.xml)。

Fear not, since you can access this information via Jenkins directly, using its remote access api:

不要害怕,因为您可以直接通过 Jenkins 访问此信息,使用其远程访问 api:

https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

For example:

例如:

http://<host>/jenkins/job/<job_name>/lastSuccessfulBuild/api/xml

(will give you the xml content... you could replace xml with json to get json content back instead of XML, for example).

(将为您提供 xml 内容……例如,您可以用 json 替换 xml 以获取 json 内容而不是 XML)。

NOTE that you may need to use authentication if you've setup your Jenkins instance to require it. Again, fear not: https://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients

请注意,如果您已将 Jenkins 实例设置为需要身份验证,则可能需要使用身份验证。同样,不要害怕:https: //wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients

Then it's a simple matter of parsing the XML for what you want. Something like this, perhaps:

然后,根据您的需要解析 XML 是一件简单的事情。像这样的事情,也许:

curl --silent --user $USER:$API_TOKEN $URL | grep "<lastBuiltRevision>" | sed 's|.*<lastBuiltRevision>.*<SHA1>\(.*\)</SHA1>.*<branch>.*||'

So, pulling it all together, you can end up with a (relatively) simple shell script to retrieve the last good revision hash from Jenkins:

所以,把它们放在一起,你最终可以得到一个(相对)简单的 shell 脚本来从 Jenkins 中检索最后一个好的修订哈希:

#!/bin/sh
GIT_LOG_FORMAT="%ai %an: %s"
USER=<username>
API_TOKEN=<api_token>

LAST_SUCCESS_URL_SUFFIX="lastSuccessfulBuild/api/xml"
#JOB_URL gets populated by Jenkins as part of the build environment
URL="$JOB_URL$LAST_SUCCESS_URL_SUFFIX"

LAST_SUCCESS_REV=$(curl --silent --user $USER:$API_TOKEN $URL | grep "<lastBuiltRevision>" | sed 's|.*<lastBuiltRevision>.*<SHA1>\(.*\)</SHA1>.*<branch>.*||')
# Pulls all commit comments since the last successfully built revision
LOG=$(git log --pretty="$GIT_LOG_FORMAT" $LAST_SUCCESS_REV..HEAD)
echo $LOG

Cheers,

干杯,

Levi

列维