1
votes

In the official documentation I found how to trigger a new build via API and specify comment, properties but not a branch name in TeamCity. The idea is to trigger a new build from GitLab for a tag branch when the merge request was tagged with a version (I use tags as branches in the configuration). In TeamCity, I added exclusion rule to branch specification (-:refs/merge-requests/*) as I do not want to build every merge request, as we usually have them a lot for other branches as well. This rule also applied to the merge request that was made recently to master and tagged. As a result, I do not have builds for tagged merge requests.

I want to trigger a new build from GitLab (add a webhook when new tag is detected), but do not know whether it is possible to specify what branch to use?

Thank you.

1

1 Answers

3
votes

After Googling for a while, I finally found an answer. To trigger a build for a specific branch, branchName="v1.26.1" (as I use tags as branch featuer, I specified a tag name) attribute should be added to <build> tag in XML request.

Here is an example in Powershell how to trigger a new build in TeamCity for a specific tag/branch:

$Username = '!username!'
$Password = '!password!'
$BuildTypeId = '!build type id from teamcity!'

$Pair = "$($Username):$($Password)"
$EncodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($Pair))
$Headers = @{
  Authorization = "Basic $EncodedCredentials";
  Accept = "application/json"
}

$Body = '<build branchName="v1.26.1"><buildType id="#{buildTypeId}"/><comment><text>Build was triggered by GitLab.</text></comment><properties /></build>' -Replace "#{buildTypeId}", $BuildTypeId
Invoke-WebRequest -Uri 'http://teamcity.host/httpAuth/app/rest/buildQueue' -Method POST -Body $Body -UseBasicParsing -Headers $Headers -ContentType "application/xml"