在teamcity中, 有一个系统变量system.teamcity.build.changedFiles.file
(官方文档), 可以获取到变更文件
这个文件表示构建时的变更内容, 如果没有变更, 则文件内容为空
文件内容大致为
/path/to/file:xxxxx-xxxxx
前面是文件, 中间是冒号, 最后是git hash
所以我们可以通过一下shell脚本获取到变更内容:
changelist=$(awk -F: '{print $NF}' %system.teamcity.build.changedFiles.file% | uniq | xargs -r git show --no-merges --first-parent --pretty=format:"> **%an**: %s" --no-patch --date=short)
if [ -z "$changelist" ]; thenchangelist="无变更内容"
fi
echo "$changelist"
用到的命令:
-
awk
一个文本处理工具-F
指定分隔符为冒号'{print $NF}'
指定输出内容为最后一个分割符后面的内容%system.teamcity.build.changedFiles.file%
指定变更文件 -
uniq
去重, 此处用于将git hash去重 -
xargs
参数传递工具-r
如果为空, 则取消执行后面的命令 -
git
版本控制show
显示详细信息--no-merges
去除merge记录--first-parent
显示第一父元素(不显示merge进来的树形结构)--pretty=format:"> **%an**: %s"
指定格式化为> **作者**: 提交日志
, 这里是markdown格式, 如果想要简单的作者:提交日志
可使用--pretty=format:"%an: %s"
--no-patch
不显示详细变更内容(否则会显示变更的文件以及变更详情)--date=short
根据日期排序
附发送到企业微信机器人的完整脚本
#!/bin/bash
changelist=$(awk -F: '{print $NF}' %system.teamcity.build.changedFiles.file% | uniq | xargs -r git show --no-merges --first-parent --pretty=format:"> **%an**: %s" --no-patch --date=short)
if [ -z "$changelist" ]; thenchangelist="无变更内容"
fi
br=$'\r\n'
changelist="$changelist$br$br打包目录为: $latestJarDir"
message=$(jq -n --arg content "生产环境打包, 本次变更: $br$changelist" '{msgtype: "markdown",markdown: {content: $content}}')
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' -H 'Content-Type: application/json' -d "$message"