Last active
September 5, 2020 08:48
-
-
Save ChaseFlorell/716ffd1e4213ecfc8a8b to your computer and use it in GitHub Desktop.
a script for TeamCity to get git commits and pass them along to Octopus Deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# credit for getting me going in the right direction | |
# http://blogs.lessthandot.com/index.php/uncategorized/access-git-commits-during-a-teamcity-build-using-powershell/ | |
# these properties should be entered into your configuration parameters section | |
$project = "%Octopus.Project%" | |
$deployTo = "%Octopus.DefaultEnvironment%" | |
$buildVersion = "%BuildVersion%" | |
$octopusApiKey = "%Octopus.BuildDeployBot.APIKey%" | |
$octopusServer = "%Octopus.Server.Url%" | |
# these properties should already be configured for you | |
$vcsGitUrl = "%vcsroot.url%" | |
$username = "%system.teamcity.auth.userId%" | |
$password = "%system.teamcity.auth.password%" | |
$serverUrl = "%teamcity.serverUrl%" | |
$buildTypeId = "%system.teamcity.buildType.id%" | |
$buildId = "%teamcity.build.id%" | |
$gitPath = "%env.TEAMCITY_GIT_PATH%" | |
$buildNumber = "%build.vcs.number%" | |
$checkoutDir = "%system.teamcity.build.checkoutDir%" | |
function Get-TeamCityLastSuccessfulRun{ | |
$AuthString = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$username`:$password")) | |
$Url = "$serverUrl/app/rest/buildTypes/id:$buildTypeId/builds/status:SUCCESS" | |
$Content = Invoke-WebRequest "$Url" -Headers @{"Authorization" = "Basic $AuthString"} -UseBasicParsing | |
return $Content | |
} | |
function Get-CommitsFromGitLog([string] $StartCommit, [string] $EndCommit){ | |
$fs = New-Object -ComObject Scripting.FileSystemObject | |
$git = $fs.GetFile("$gitPath").shortPath | |
$overviewUrl = "$serverUrl/viewLog.html?buildId=$buildId&buildTypeId=$buildTypeId&tab=buildResultsDiv" | |
$commitUrl = "$($vcsGitUrl.TrimEnd('.git'))/commit" | |
$Cmd = "$git log --pretty=format:""%s [%h...]($commitUrl/%H)"" $StartCommit...$EndCommit" | |
$Result = $(Invoke-Expression "$path $Cmd") | |
$nl = [environment]::NewLine | |
[string]$str = "#TeamCity Auto Deployment $nl" + "[click here for build overview]($overviewUrl) $nl$nl" | |
$Result | % {$str += " - $_ $nl"} | |
return $str | |
} | |
$Run = Get-TeamCityLastSuccessfulRun | |
$LatestCommitFromRun = (Select-Xml -Content "$Run" -Xpath "/build/revisions/revision/@version").Node.Value | |
$CommitsSinceLastSuccess = Get-CommitsFromGitLog -StartCommit "$LatestCommitFromRun" -EndCommit "$buildNumber" | |
$CommitsSinceLastSuccess > "$checkoutDir\build-artifacts\ReleaseNotes.md" | |
$Cmd = "octo.exe create-release --apiKey=$octopusApiKey --server='$octopusServer' --project=$project --deployto=$deployTo --enableServiceMessages --progress --waitfordeployment --packageversion=$buildVersion --releaseNotesFile=$checkoutDir\build-artifacts\ReleaseNotes.md" | |
Invoke-Expression $cmd |
How did you get around your issues with "%vcsroot.url%"? I'm running into the same thing.
Thanks for the quick response, @ChaseFlorell. I'll try to track it down there.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requirements:
env:PATH
env.TEAMCITY_GIT_PATH
notes:
%Octopus.BuildDeployBot.APIKey%
as a hidden password. (for security sake)ReleaseNotes.md
instead of just passing a string is because we want to preserve line breaks and markdown formatting."%vcsroot.url%"
alterations:
we have a custom tool that we use to extend our Git experience, this tool has a consistent naming convention for when it does merges. We've replaced
$Result | % {...}
line to filter out merge messages