TFS: Best way to trigger build on server restart,

2020-02-14 10:39发布

In short, the requirement is to verify that our latest released software can be built and then installed after the latest Windows updates and/or other patches were applied. So the build server VM(s) will be configured just for this purpose and the build only needs to run after an update.

Since such updates usually are followed with a restart, I am thinking of a server restart event triggering a build and deployment. Does such option exist in TFS 2017?

If there is no way to do it through TFS then, I guess, a PowerShell script that runs on startup should work?

2条回答
手持菜刀,她持情操
2楼-- · 2020-02-14 11:22

No such a build-in function to achieve that. However create a PowerShell script that runs on startup should work. Just as Jessehouwing said, you can create the script with the REST API to trigger builds.

  1. Create a script to trigger the specific build definition. (Reference below sample)

  2. Run the script on startup:


Param(
   [string]$collectionurl = "http://server:8080/tfs/DefaultCollection",
   [string]$projectName = "ProjectName",
   [string]$keepForever = "true",
   [string]$BuildDefinitionId = "34",
   [string]$user = "username",
   [string]$token = "password"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

function CreateJsonBody
{

    $value = @"
  {
  "definition": {
    "id": $BuildDefinitionId
  },

  "parameters": "{\"system.debug\":\"true\",\"BuildConfiguration\":\"debug\",\"BuildPlatform\":\"x64\"}"
}
"@

 return $value
}

$json = CreateJsonBody

$uri = "$($collectionurl)/$($projectName)/_apis/build/builds?api-version=2.0"
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
查看更多
做自己的国王
3楼-- · 2020-02-14 11:35

There is no existing trigger that handles this, but there is a simple REST API to query and trigger builds.

It would be easy to create an on startup job in the task scheduler, use the REST API to query a list of Build Definitions based on a certain name or tag and then queue it.

查看更多
登录 后发表回答