0

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?

Community
  • 1
  • 1
ShaneC
  • 2,237
  • 2
  • 32
  • 53

1 Answers1

2

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.

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
  • The snippet looks like it automates creating a backup of TeamCity. I'm looking for automation of assignment of a build trigger – ShaneC Jan 21 '15 at 14:09
  • I have, same result. I wouldn't be surprised if it's not implemented, it's just the fact that I was able to find someone (mentioned in my question) who claims to have done what I'm trying to do in a different language – ShaneC Jan 21 '15 at 14:38
  • If it has been done in C#, it can be do in PowerShell also. I banged my head before getting authentication right -- that is why I put my code even if it is a different API. That said, I would patiently reverse engineer TeamCitySharp behaviour, studying the code at https://github.com/stack72/TeamCitySharp – Giulio Vian Jan 21 '15 at 15:57