【问题标题】:Adding trigger to build config in TeamCity via REST API and PowerShell通过 REST API 和 PowerShell 添加触发器以在 TeamCity 中构建配置
【发布时间】:2015-01-21 11:57:37
【问题描述】:

我正在尝试通过 PowerShell 和 TeamCity 8 REST API 以自动方式将构建触发器添加到构建配置中。

使用以下问题作为参考,看来我正在尝试做的事情是可能的:Adding a Trigger to a build configuration in TeamCity using the REST API

但是,每当我尝试使用以下代码将触发器添加到构建中时,都会收到 (405) Method Not Allowed 错误:

$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)

有人使用 PowerShell 成功实现了这一点吗?

【问题讨论】:

    标签: rest powershell automation teamcity build-triggers


    【解决方案1】:

    不是那个 API,但我有自动化 TeamCity 的脚本。

    这是我使用的代码 sn-p:

    $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
    

    请注意第一次身份验证调用(我禁用了内置用户并坚持使用 Windows 身份验证),并且经过身份验证的会话传递给后续调用。 Invoke-RestMethod 是 Powershell v4。

    【讨论】:

    • sn-p 看起来像是自动创建 TeamCity 的备份。我正在寻找构建触发器分配的自动化
    • 我有,同样的结果。如果它没有实现,我不会感到惊讶,只是我能够找到一个人(在我的问题中提到)声称用不同的语言做了我想做的事情
    • 如果已经在 C# 中完成,也可以在 PowerShell 中完成。在获得正确的身份验证之前,我撞了头——这就是为什么我把我的代码,即使它是一个不同的 API。也就是说,我会耐心地对 TeamCitySharp 行为进行逆向工程,在 github.com/stack72/TeamCitySharp 研究代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 2021-02-10
    • 2011-07-12
    • 2013-06-15
    相关资源
    最近更新 更多