Skip to content

Instantly share code, notes, and snippets.

@ChaseFlorell
Last active September 5, 2020 08:48
Show Gist options
  • Save ChaseFlorell/716ffd1e4213ecfc8a8b to your computer and use it in GitHub Desktop.
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
# 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
@Schnyder
Copy link

How did you get around your issues with "%vcsroot.url%"? I'm running into the same thing.

@ChaseFlorell
Copy link
Author

ChaseFlorell commented Jan 14, 2020

oh boy @Schnyder, this was so very long ago :(
I don't even remember what the issue was.
Maybe take a look at PoshBar and see if it gives you and further details. That's likely where my final code ended up.

@Schnyder
Copy link

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