Last active
April 21, 2022 18:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#$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']" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Huge thanks to you guys!