Teamcity REST API(二)

  • Post author:
  • Post category:其他


0.teamcity内置的有用的变量

“%system.teamcity.buildType.id%”

“%system.teamcity.projectName%”

“%teamcity.build.triggeredBy%”

“%system.teamcity.buildConfName%”

“%system.teamcity.configuration.properties.file%”

“**********************”

“%teamcity.agent.name%”

“%teamcity.agent.work.dir%”

“%teamcity.agent.work.dir.freeSpaceMb%”

“%teamcity.agent.home.dir%”

“%teamcity.agent.tools.dir%”

“%teamcity.agent.jvm.os.version%”

“%teamcity.agent.os.arch.bits%”

“%DotNetFramework4.8_x64%”

“%DotNetFramework4.8_x64_Path%”

“%teamcity.build.checkoutDir%”

“%teamcity.build.workingDir%”

“%system.teamcity.build.tempDir%”

“%system.teamcity.build.properties.file%”

1.关闭/打开特定build的trigger

#用curl关闭build名为aa,trigger的id为TRIGGER_107的trigger。
#-d "false",则是打开相应trigger
#http://\<TeamCity Server host>:\<port>/app/rest/buildTypes/name:aa/triggers 可以查看aa这个build的所有trigger
#apiKey是user token,需要先行定义
apiKey="xxxxx"
curl -H "Authorization: Bearer ${apiKey}" \
-H "Content-Type:text/plain" \
-X PUT  \
-d 'true'  \
"http://\<TeamCity Server host>:\<port>/app/rest/buildTypes/name:aa/triggers/TRIGGER_107/disabled"
#使用Invoke-RestMethod实现相同功能
$apiKey = "xxxxx"
$headers = @{}
$headers.Add("Authorization","Bearer $apiKey")
$headers.Add("Content-Type", "text/plain")
Invoke-RestMethod   -Uri "http://\<TeamCity Server host>:\<port>/app/rest/buildTypes/name:aa/triggers/TRIGGER_107/disabled"  -Method Put -Headers $headers -Body "true"

2.触发一个build


参考链接

#jq用于生成json形式的body。当然方式有很多种。
apiKey="xxxxx"
(echo '{"buildType":{"id":"ManagementAndMonitoring_GainTeamcityBuildStatus"}}' |jq .) >body.json #生成目标json,存放到body.json文件中。
 curl -H "Authorization: Bearer ${apiKey}" -H  "Content-Type: application/json" -X POST -d @body.json http://\<TeamCity Server host>:\<port>/app/rest/buildQueue
#通过访问http://\<TeamCity Server host>:\<port>/httpAuth/app/rest/buildTypes可以获取所有build的id,
#根据build的名字搜索,获取buildType id="ManagementAndMonitoring_GainTeamcityBuildStatus"
#注意$Body转换的json格式
$apiKey = "xxxx"
$headers = @{}
$headers.Add("Authorization","Bearer $apiKey")
$headers.Add("Content-Type", "application/json")
$Body = @{
buildType = @{id = "ManagementAndMonitoring_GainTeamcityBuildStatus"}
}| ConvertTo-Json -Depth 10
Invoke-RestMethod -Headers $headers -Uri "http://\<TeamCity Server host>:\<port>/app/rest/buildQueue" -Method Post -Body $Body
#输出Body转化后的结果
{
    "buildType":  {
                      "id":  "ManagementAndMonitoring_GainTeamcityBuildStatus"
                  }
}

以上是触发一个简单build,若要触发一个自定义build,则在body种需要加更多属性值。


参见此处

3.对于一个已经连上的agent,进行授权和取消授权

$apiKey = "xxxx"
$buildName="xxxx"  #需要授权或者取消授权的build
$headers = @{}
$headers.Add("Authorization","Bearer $apiKey")
$headers.Add("Content-Type", "text/plain")
Invoke-RestMethod -Headers $headers -Uri "http://\<TeamCity Server host>:\<port>/app/rest/agents/$buildName/authorized" -Method PUT -Body "false"  #false,是取消授权,true是授权。
apiKey="xxxxx"
curl -H "Authorization: Bearer ${apiKey}" \
-H "Content-Type:text/plain" \
-X PUT  \
-d 'true'  \
"http://\<TeamCity Server host>:\<port>/app/rest/agents/$buildName/authorized"

4.获取build重要信息

获取排队的build:Invoke-RestMethod -Headers $header -Uri "http://\<TeamCity Server host>:\<port>/app/rest/buildQueue" -OutFile E:\queued.xml   #queued.xml中有所有在排队的build的名字
获取正在运行的build:Invoke-RestMethod -Headers $header -Uri "http://\<TeamCity Server host>:\<port>/app/rest/builds?locator=running:true" -OutFile E:\running.xml  #running.xml中有所有正在运行的build
获取目标build最近一次运行的结果:$status=(Invoke-RestMethod -Headers $header -Uri "http://\<TeamCity Server host>:\<port>/app/rest/buildTypes/name:$buildName/status")  #$status存放build的状态:SUCESS,FAILURE,UNKNOWN(从未运行过)
获取某个build配置的状态:Invoke-RestMethod -Headers $headers -Uri "http://\<TeamCity Server host>:\<port>/app/rest/buildTypes/name:buildName/builds?locator=lookupLimit:1&fields=build(state)" 
#获取这个配置的最近一次build的状态:state可以是queued/running/finished



版权声明:本文为qq_40492048原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。