0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【編集中】TFSをREST APIで操作したい!!

Last updated at Posted at 2020-03-21

TODO

  • PAT作成
  • PowerShellでビルド実行
  • PowerShellでビルド定義一括取得&一覧作成

はじめに

今までは、Chromeでサイトにアクセスして手動で操作していたのですが、そろそろ面倒になってきたので、設定や操作等は自動化していきたいということで...
当初はTFSのコマンドで操作しようと思っていたのですが、調べてみたらREST APIによる操作があるということで、勉強も兼ねて

ビルド定義

GetBuildDefinitions.ps1
# Get Build Definitions (api-version = 2.0)
#   https://docs.microsoft.com/ja-jp/azure/devops/integrate/previous-apis/build/definitions?view=tfs-2015#get-a-list-of-build-definitions
$getRequestUrl = "http://192.168.119.217:8080/tfs/Development/AtHomeDev/_apis/build/definitions?api-version=2.0"
 
# Call the REST API using Invoke-RestMethod. -UseDefaultCredentials for using windows authentication
#   ※TFS 2015 doesn't support Personal Access Tokens, this feature was introduced with TFS 2017
$json = Invoke-RestMethod -UseDefaultCredentials -uri $getRequestUrl
ConvertTo-Json $json
TFS_DeleteBuildList.ps1
Param(
   [string]$collectionurl = "http://172.27.194.176:8080/tfs/Development",
   [string]$projectName = "ScrumProject",
   [string]$builddefinitionID = "1",
   [string]$keepForever = "true",
   [string]$user = "username",
   [string]$token = "password"
)


#Get builds list which completed in the year 2017
$buildsUrl = "$($collectionurl)/$projectName/_apis/build/builds?definitions=$builddefinitionID&statusFilter=completed&api-version=2.0"   
#$builds = (Invoke-RestMethod -UseDefaultCredentials -Uri $buildsUrl -Method Get) | where({$_.finishTime -like '*2023*'})

$builds = (Invoke-RestMethod -UseDefaultCredentials -Uri $buildsUrl -Method Get)
#$builds.values.properties.finishTime[2]
#ConvertTo-Json $builds

#Write-Host "Builds deleted with the ID" : $builds.value

#Delete the builds 
foreach ($build in $builds.value.id)
{
    Write-Host $build
    $deleteurl = "$($collectionurl)/$projectName/_apis/build/builds/$build"+"?api-version=2.0"

    $result = (Invoke-RestMethod -UseDefaultCredentials -Uri $deleteurl -Method Delete)

    Write-Host "Builds deleted with the ID" : $build 
}

参考サイト

◆Azure DevOps Services REST API Reference
 https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-5.1&viewFallbackFrom=vsts-rest-2.3
  ※最初に見るサイト

◆REST API Overview for TFS 2015, 2017, and 2018 (RTW and Update 1)
 https://docs.microsoft.com/ja-jp/azure/devops/integrate/previous-apis/build/builds?view=tfs-2015#update-a-build
  ※公開されているAPIを見る限りは、自分がやりたいことは全部できるはず。。。

◆Visual Studio Team Services CI/CDアドベントカレンダー:PowerShellでビルド実行(8日目)
 https://kkamegawa.hatenablog.jp/entry/2017/12/08/232655
  ※第一弾として、これをやってみたい!!
◆invoke-vstsbuild.ps1
 https://gist.github.com/kkamegawa/fed4b31082f53dac70f2d0f069651e40
  ※参考となるPowerShellコード

◆Authenticate access with personal access tokens
 https://docs.microsoft.com/ja-jp/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page&viewFallbackFrom=tfs-2015
  ※やるにしても、認証どうするんだ!?と思って調べた結果、このPATを利用するということで

◆Modifying TFS2015 build definitions using Powershell
 https://blogs.infosupport.com/modifying-tfs2015-build-definitions-using-powershell/
  ※これができれば、最終的には自動化も見えてくる

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?