Shane Callanan
Shane Callanan

Reputation: 2305

Adding trigger to build config in TeamCity via REST API and PowerShell

I'm trying to add a build trigger to a build configuration in an automated fashion through PowerShell and the TeamCity 8 REST API.

Using the following question as a reference, it would appear that what I am trying to do is possible:Adding a Trigger to a build configuration in TeamCity using the REST API

But, whenever I try to add the trigger to the build, using the following code, I get a (405) Method Not Allowed error:

$triggerXML= "<trigger id=`"TriggerID`" type=`"buildDependencyTrigger`">
                  <properties>
                      <property name=`"afterSuccessfulBuildOnly`" value=`"true`"/>
                      <property name=`"dependsOn`" value=`"BuildID`"/>
                  </properties>
              </trigger>"

$webclient.UploadString('http://teamcity:8111/httpAuth/app/rest/buildTypes/BuildID', "POST", $triggerXML)

Has anyone implemented this successfully using PowerShell?

Upvotes: 0

Views: 1735

Answers (1)

Giulio Vian
Giulio Vian

Reputation: 8343

Not that API, but I have scripts that automate TeamCity.

Here is a code snippet I use:

$TeamCityHostAndPort = "myteamcityserver:8111"

# authenticate with NTLM
$LoginUrl = "http://$TeamCityHostAndPort/ntlmLogin.html"
Invoke-WebRequest -Uri $LoginUrl -UseDefaultCredentials -SessionVariable TeamCitySession | Out-Null

#start backup
$StartBackupUrl = "http://$TeamCityHostAndPort/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=TeamCity_Backup_"
$filename = Invoke-RestMethod -WebSession $TeamCitySession -Method Post -Uri $StartBackupUrl

notice the first call to authenticate (I disabled built-in users and stick with Windows auth) and the authenticated session passed to subsequent calls. Invoke-RestMethod is Powershell v4.

Upvotes: 2

Related Questions