Skip to content

Instantly share code, notes, and snippets.

@Stygy
Last active April 21, 2022 18:02
Show Gist options
  • Save Stygy/7ce5ddd9b49342e9b03b6c182ab72c0f to your computer and use it in GitHub Desktop.
Save Stygy/7ce5ddd9b49342e9b03b6c182ab72c0f to your computer and use it in GitHub Desktop.
This powershell is being used as a Teamcity Build Step that sets the BuildNumber as Date.Counter (i.e 2018.02.02.1) and resets counter everyday. In collaboration with @axelwass
#$teamcityUser = '%teamcity.api.username%'
#$teamcityPass = '%teamcity.api.password%'
#$teamcityUrl = '%teamcity.api.url%'
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$teamcityUser = '****'
$teamcityPass = '****'
$teamcityUrl = 'yadayada.com'
$buildId = '123456'
$projectId = 'ProjectId'
function GetBuildCount
{
param
(
[string] $ServerUrl,
[string] $UserName,
[string] $Password,
[string] $BuildId,
[string] $ProjectId
)
$client = New-Object System.Net.WebClient
$pair = "$($UserName):$Password"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$client.Headers.Add("Authorization", "Basic $encodedCreds")
$date = Get-Date -Format yyyyMMddT000000zz00
$dateEncoded = [uri]::EscapeDataString($date)
$url = "https://$ServerUrl/httpAuth/app/rest/buildTypes/id:$projectId/builds?locator=sinceDate:$dateEncoded,defaultFilter:false"
$response = [xml]$client.DownloadString($url)
$builds = $response.builds.build | ? {$_.id -le "$BuildId"}
$count = $builds.count
if (!$count) {
$count = 1
}
return $count
}
$buildCount = GetBuildCount -ServerUrl $teamcityUrl -UserName $teamcityUser -Password $teamcityPass -BuildId $buildId -ProjectId $projectId
Write-Host "##teamcity[buildNumber '$([DateTime]::UtcNow.ToString("yyyy.MM.dd")).$buildCount']"
@Stygy
Copy link
Author

Stygy commented Feb 2, 2018

Lines 33 to 37 are a workaround for empty value on the first build of the day

@S1r-Lanzelot
Copy link

Huge thanks to you guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment