Skip to content

Instantly share code, notes, and snippets.

@timvw
Created September 25, 2015 09:50
Show Gist options
  • Select an option

  • Save timvw/b4a6fee16c95ff162847 to your computer and use it in GitHub Desktop.

Select an option

Save timvw/b4a6fee16c95ff162847 to your computer and use it in GitHub Desktop.
Download all the latest buildartifacts from a set of builds from teamcity
# define parameters
$baseDir = "c:\temp" #"%system.agent.work.dir%"
$username = "ICTEAM\TIMVW"
$password = "Password"
$teamcityUrl = "http://teamcity.icteam.be"
$branchName = "feature/MS-001" #$branchName = "<default>"
$buildTypeIds = @(
"MS_Nightly_Module1",
"MS_Nightly_Module2",
)
# derive working directories
$workDir = "$baseDir\work"
$downloadDir = "$workDir\downloads"
$extractDir = "$workDir\extract"
$databaseDir = "$baseDir\database"
$installersDir = "$baseDir\installers"
# ensure working directories are emtpty
$dirs = $workDir, $downloadDir, $extractDir, $databaseDir, $installersDir
$dirs | Where { Test-Path $_ } |% { Remove-Item $_ -Force -Recurse }
$dirs |% { New-Item $_ -ItemType directory } | Out-Null
function downloadLatestArtifacts {
param(
[string] $buildTypeId,
[string] $branchName, # teamcity does not seem to accept "refs/head/develop". <default> seems to work though.
[string] $username,
[string] $password,
[string] $teamcityUrl,
[string] $downloadDir,
[string] $extractDir
)
$webclient = new-object System.Net.WebClient
# tomcat does not handle auth very well. Let's brute-force it.
$credentials = [System.Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($username + ":" + $password))
$webClient.Headers["Authorization"] = "Basic {0}" -f $credentials
$url = "$teamcityUrl/httpAuth/app/rest/builds?count=1&buildType=id:$buildTypeId&locator=branch:name:$branchName,status:SUCCESS"
[xml]$xml = $webClient.DownloadString($url)
$buildId = $xml.builds.build.id
$buildCount = $xml.builds.count
if ($buildCount -ne 0) {
$url = "$teamcityUrl/repository/downloadAll/{0}/{1}:id" -f $buildTypeId, $buildId
$artifactPath = "$downloadDir\$buildTypeId.zip"
$webclient.DownloadFile($url, $artifactPath)
[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
[System.IO.Compression.ZipFile]::ExtractToDirectory($artifactPath, $extractDir)
}
}
$buildTypeIds |% { downloadLatestArtifacts -buildTypeId $_ -branchName $branchName -username $username -password $password -teamcityUrl $teamcityUrl -downloadDir $downloadDir -extractDir $extractDir }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment