TeamCity - iterate vcs roots using powershell

I tried to create a meta runner to create a metadata file using powershell in TeamCity, and I was wondering if there is a way to iterate over different vcs routes?

My code is:

$fileName = "metadata.json" $vcsArray = @() for ($i = 0; $i -le 5; $i++) { $vcsObject= @{ "VCSNumber"="%build.vcs.number.Proj_App_TcTestApp%" } $vcsArray += $vcsObject } $content = @{ "TeamCityBuildLogUrl" = "http://teamcity.hps.com/viewLog.html?buildId=%teamcity.build.id%&tab=buildResultsDiv&buildTypeId=%system.teamcity.buildType.id%"; "TeamCityProjectName" = "%system.teamcity.projectName%"; "TeamCityBuildNumber" = "%system.build.number%"; "BuildDateGenerated" = (Get-Date).ToString(); "TeamCityExecutionAgentName" = "%teamcity.agent.name%"; "VCSes" = $vcsArray } } $content = $content | Add-Member @{"VCS Version2" = "testValue"} -PassThru # How to add more members dynamically. $content = ConvertTo-JSON $content New-Item $fileName -type file -force -value "// Metadata file generated by TeamCity`n" Add-Content $fileName $content cat $fileName # Test afterwards 

When I add another root, the root names eventually become identifiers, which makes it difficult to iterate over them, since I do not know the root names technically.

Here is an example use case: I have two vcs roots:

 %build.vcs.number.Proj_App_TcTestFW% %build.vcs.number.Proj_App_TcTestApp% 

Ideally, I would like to skip them like this:

 $vcsArray = @() foreach ($vcsRoot in vcsRoots) { $vcsObject=@ { "VCSName"= $vcsRoot; "VCSNumber"= "%build.vcs.number." + $vcsRoot% } $vcsArray += $vcsObject } 

But it seems to me that I need to hard-code the names in my script, so I'm at a loss right now.

Does TeamCity provide VCS routes so that I can iterate over them?

Thanks Alex

+5
source share
1 answer

Well, I have no actual TeamCity experience, but it looks like you can get the list of roots by issuing the REST command:

 Invoke-WebRequest -Uri 'http://teamcity.hps.com/httpAuth/app/rest/vcs-roots' -Method Get 

which should return an XML response with a list of roots:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <vcs-roots count="1"> <vcs-root id="TestProject1_TestProject1perforce" name="test-project1-perforce" href="/httpAuth/app/rest/vcs-roots/id:TestProject1_TestProject1perforce"/> </vcs-roots> 

Is this what you are looking for?

Literature:

+1
source

Source: https://habr.com/ru/post/1233834/


All Articles