7
votes

I was trying to create a meta-runner to generate a metadata file using powershell in TeamCity and I was wondering if there was a way to iterate over the different vcs routes?

My Code:

$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 names of the roots end up becoming the identifiers, which makes it difficult to iterate over them since I don't technically know the names of the roots.

Here's an example use-case: I have two vcs roots:

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

Ideally, I'd like to iterate through them like so:

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

    $vcsArray += $vcsObject
}

But it seems that I have to hardcode the names in my script, so I'm currently at a loss.

Does TeamCity expose the VCS routes in such a way that I can iterate over them?

Thanks Alex

1

1 Answers

3
votes

Ok, I don't have an actual TeamCity experience, but it looks like you can get a list of roots by issuing a REST command:

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

which should return a XML responce 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 is what are you looking for?

References: