Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
a script for TeamCity to get git commits and pass them along to Octopus Deploy
# credit for getting me going in the right direction
# 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 = ""
$buildId = ""
$gitPath = "%env.TEAMCITY_GIT_PATH%"
$buildNumber = "%build.vcs.number%"
$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\"
$Cmd = "octo.exe create-release --apiKey=$octopusApiKey --server='$octopusServer' --project=$project --deployto=$deployTo --enableServiceMessages --progress --waitfordeployment --packageversion=$buildVersion --releaseNotesFile=$checkoutDir\build-artifacts\"
Invoke-Expression $cmd

This comment has been minimized.

Copy link
Owner Author

commented Jan 30, 2015


  1. Octo.exe installed on your build agent, and available in your env:PATH
  2. Git installed on your build agent, and available at env.TEAMCITY_GIT_PATH
  3. TeamCity VCS must be set to "checkout on the agent"


  • Set the %Octopus.BuildDeployBot.APIKey% as a hidden password. (for security sake)
  • The reason for the instead of just passing a string is because we want to preserve line breaks and markdown formatting.
  • Sometimes TeamCity configuration parameter replacement is flaky. (your mileage may vary)
    • i actually had issues with "%vcsroot.url%"


  1. 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

    $Result | % {$str += " - $_  $nl"}
    $Result | where {-not $_.StartsWith("Merge branch")} | % {$str += " - $_  $nl"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.