我有Teamcity构建代理,我需要从存储库中提取git日志。问题是,当我的shell脚本运行时,它不在git存储库中,因此git log不能开箱即用。
是……
使用 -C 国旗 git 命令。它允许您指定git目录。
-C
git
git -C 'git-working-dir' log
您无需克隆repo即可获取日志。 TeamCity已经有一个触发构建的文件和提交消息列表。您只需在脚本步骤中查询TeamCity API并获取日志即可。以下是我用来执行此操作的两个Powershell函数。
TeamCityGetChangeLog,需要服务器URL,用户名,密码和构建ID(您可以从TeamCity参数传入)。
# Gets the change log for the specified build ID function TeamCityGetChangeLog([string] $serverUrl, [string] $username, [string] $password, [int] $buildId){ $changelog = '' # If the build is running inside TeamCity if ($env:TEAMCITY_VERSION) { $buildUrl = "$serverUrl/httpAuth/app/rest/changes?build=id:$($buildId)" $authToken = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($username + ":" + $password)) # Get all the changes $request = [System.Net.WebRequest]::Create($buildUrl) $request.Headers.Add("Authorization", "Basic $authToken"); $xml = [xml](new-object System.IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd() # Get all commit messages for each of them $changelog = Microsoft.PowerShell.Utility\Select-Xml $xml -XPath ` "/changes/change" | Foreach { TeamCityGetCommitMessage $serverUrl $username $password $_.Node.id } } return $changelog }
这依赖于TeamCityGetCommitMessage,它也需要更改集ID。
# Get the commit messages, and files changed for the specified change id # Ignores empty lines, lines containing "#ignore", "merge branch"" or "TeamCity" function TeamCityGetCommitMessage([string]$serverUrl, [string]$username, [string]$password, [int]$changeId) { $getFilesChanged = $false; $request = [System.Net.WebRequest]::Create("$serverUrl/httpAuth/app/rest/changes/id:$changeId") $authToken = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($username + ":" + $password)) $request.Headers.Add("Authorization", "Basic $authToken"); $xml = [xml](new-object System.IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd() Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/change" | where { ($_.Node["comment"].InnerText.Length -ne 0) ` -and (-Not $_.Node["comment"].InnerText.Contains('#ignore')) ` -and (-Not $_.Node["comment"].InnerText.StartsWith("Merge branch")) ` -and (-Not $_.Node["comment"].InnerText.StartsWith("TeamCity change"))} | foreach { $getFilesChanged = $true $username = (&{If($_.Node["user"] -ne $null) {$_.Node["user"].name.Trim()} Else { $_.Node.Attributes["username"].Value }}) "<br /><strong>$($username + " on " + ([System.DateTime]::ParseExact($_.Node.Attributes["date"].Value, "yyyyMMddTHHmmsszzzz", [System.Globalization.CultureInfo]::InvariantCulture)))</strong><br /><br />" "$($_.Node["comment"].InnerText.Trim().Replace("`n", "`n<br />"))" } if ($getFilesChanged) { "<br /><br /><strong>Files Changed</strong><br /><br />" Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/change/files" | where { ($_.Node["file"].Length -ne 0)} | foreach { Select-Xml $_.Node -XPath 'file' | foreach { "$($_.Node.Attributes["file"].Value)<br />" } } } }
我的解决方案最终如下:
注册SSH凭据以访问TC代理上的Github。
git clone ssh://git@github.com/account-name/repo.git /var/tmp/projectname_tmp
cd 到目录 /var/tmp/projectname_tmp
cd
/var/tmp/projectname_tmp
git log >> output.file
对于奖励积分,您可以从给定日期中提取,如下所示:
git log --since='2016-11-01 03:00' >> output.log