簡體   English   中英

TFS 2017 vNext Build使用Powershell獲得工作空間

[英]TFS 2017 vNext Build Get workspace with powershell

我有一個項目,在構建過程中需要更改一些文件。 我必須使用Powershell來執行此操作。 我已經配置了所有必需的步驟來執行此操作。 所有步驟都可以在我的客戶端PC上進行。 構建服務器具有相同的配置。 已安裝Vs 2015(用於TF Powertools 2015)和VS 2017。 當我對構建進行排隊時,構建在他嘗試獲取工作區時失敗。 也許是因為生成代理僅創建本地工作區? 至此,所需的更改已經簽出。 我無法使用TF.exe簽入,因為有些簽入策略會阻止沒有相關工作項的簽入。 這是我在此步驟中嘗試做的:

  • 通過源路徑獲取工作區($ Env:BUILD_SOURCESDIRECTORY)
  • 獲取此工作空間的暫掛更改$pendingChanges = $tfsws.GetPendingChanges()
  • 創建帶有關聯工作項的workitemcheckininfo
  • 使用創建的workitemcheckininfo $changesetNumber = $tfsws.CheckIn($pendingChanges,"$CommentString checked in by BuildServer",$null, $workItemChanges,$null)

由於我沒有得到工作區(步驟1),因此以下步驟將不會開始。

這是我到目前為止嘗試過的:

1

$binpath   = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
#$binpath   = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0"
Add-Type -path "$binpath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.VersionControl.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.Common.dll"

$teamProjectCollection = "http://mytfs/tfs/defaultcollection"
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($teamProjectCollection)

#The next line fails, as the commandlet is from TF Powertools 2015 and the TFS server is 2017.
#I get the error message "Microsoft.TeamFoundation.Client.TfsTeamProjectCollection cannot be converted to Microsoft.TeamFoundation.Client.TfsTeamProjectCollection"
$tfsws = Get-TfsWorkspace -Server $tfs -Computer $hostname -Owner $Username

2

$binpath   = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
#$binpath   = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0"
Add-Type -path "$binpath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.VersionControl.Client.dll"
Add-Type -Path "$binpath\Microsoft.TeamFoundation.Common.dll"

$localReference = join-path $Env:BUILD_SOURCESDIRECTORY $TargetBranch
$teamProjectCollection = "http://mytfs/tfs/defaultcollection"
$tfsTeamProjectCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($teamProjectCollection)
$versioncontrolServer = $tfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

#The next lines fail, as $versioncontrolServer is nothing
[Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.EnsureUpdateWorkspaceInfoCache($versionControlServer, $username);
$tfsws = $versioncontrolServer.GetWorkspace($localReference)

有兩件事可能會導致問題:1st => build agent僅使用本地工作區? 2nd => TFS 2017和VS 2015是否不夠兼容?

有沒有一個好的工作示例或解決方案?

我想到了其他選擇。 也許我可以編寫一個可執行文件來完成我的工作。

我可以在沒有工作空間的情況下簽入,以后再關聯工作項嗎? 如何以編程方式將工作項與現有變更集關聯?

沒有工作空間就無法簽到。

但是,該過程將在“獲取源代碼”步驟中創建本地工作區。 因此,您可以繞過簽入策略直接簽入更改,然后稍后將工作項與特定的Changeset關聯。

要繞過/覆蓋簽入策略 ,可以在簽入命令下方運行( 可以在命令下方復制並另存為PowerShell / cmd腳本,然后添加PowerShell / Command任務以運行腳本 )。 請參閱Checkin命令

tf Checkin $source_dir /comment:"Change files" /noprompt /force /bypass /override:"Without associating workitem"

注意: 確保您使用的代理是2.122.1或更高版本,否則您可能會遇到錯誤,請參閱此相關主題以獲取詳細信息。

要將工作項與現有變更集相關聯:

帶有客戶端API的C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace APPI
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://xxx.xxx.xxx.xxx:8080/tfs/DefaultCollection";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(url));
            WorkItemStore wis = ttpc.GetService<WorkItemStore>();
            VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
            int wid = 194;
            int cid = 440;
            WorkItem wi = wis.GetWorkItem(wid);
            Changeset cs = vcs.GetChangeset(cid);
            ExternalLink el = new ExternalLink(wis.RegisteredLinkTypes["Fixed in Changeset"], cs.ArtifactUri.AbsoluteUri);
            wi.Links.Add(el);
            wi.Save();     
        }
    }
}

具有REST API的PowerShell:

Param(
   [string]$collectionurl = "http://server:8080/tfs/DefaultCollection",
   [string]$keepForever = "true",
   [string]$WorkitemID = "194",
   [string]$ChangesetID = "439",
   [string]$user = "UserName",
   [string]$token = "password/token"
)

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

function CreateJsonBody
{

    $value = @"
[
 {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "ArtifactLink",
      "url": "vstfs:///VersionControl/Changeset/$ChangesetID",
      "attributes": {
        "name": "Fixed in Changeset"
      }
    }
  }
]

"@

 return $value
}

$json = CreateJsonBody

$uri = "$($collectionurl)/_apis/wit/workitems/$($WorkitemID)?api-version=1.0"
$result = Invoke-RestMethod -Uri $uri -Method Patch -Body $json -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

您可能想簽出(不是雙關語) 針對VSTS / TFS的TFVC構建任務 它提供您所需的大多數東西。 除非您使用Update Gated Changes,否則它唯一遺漏的是工作項關聯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM