Skip to content

Instantly share code, notes, and snippets.

Embed
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
# 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
@ChaseFlorell

This comment has been minimized.

Copy link
Owner Author

ChaseFlorell commented Jan 30, 2015

requirements:

  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"

notes:

  • Set the %Octopus.BuildDeployBot.APIKey% as a hidden password. (for security sake)
  • The reason for the ReleaseNotes.md 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%"

alterations:

  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

    #before
    $Result | % {$str += " - $_  $nl"}
    
    #after
    $Result | where {-not $_.StartsWith("Merge branch")} | % {$str += " - $_  $nl"}
    
@Schnyder

This comment has been minimized.

Copy link

Schnyder commented Jan 14, 2020

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

@ChaseFlorell

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

Schnyder commented Jan 14, 2020

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
You can’t perform that action at this time.