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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The snippet looks like it automates creating a backup of TeamCity. I'm looking for automation of assignment of a build trigger
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
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 github.com/stack72/TeamCitySharp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.