通过docker windows容器中的powershell访问TFS(在线)

时间:2018-05-23 15:32:21

标签: windows tfs dockerfile

我有一个运行windowservercore的窗口2016服务器。我正在努力将CI管道移动到容器中。在我们的过程中,我们构建了一个version.html文件。该文件包含构建数据(如构建日期和构建nbr)和有关合并/分支的TFS 2017项目信息。

我们使用TeamCity运行PowerShell脚本,该脚本将连接并运行针对TFS 2017的查询。所以我查看了Docker hub以获取TFS,但没有任何运气。我也试过在微软的Docker hub上寻找并没有找到任何东西。

我尝试创建一个新的docker文件

    FROM microsoft/windowsservercore:10.0.14393.1480

# Setup shell
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN Mkdir BuildStage
COPY powershell/CopyBuildToStageDir.ps1 \BuildStage
Copy  powershell/BuildVersionFile.ps1 \BuildStage

RUN dir

但是当我在Windows容器中运行Powershell文件时,它说......

Unable to find type 

[09:25:00][Step 2/2] [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory].

[09:25:00][Step 2/2] At C:\BuildStage\BuildVersionFile.ps1:192 char:12

在PowerShell中,有这个功能

#============================================================================
# Setup TFS stuff
#============================================================================
function Setup-Tfs {

    # Connect to TFS
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") | out-null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client") | out-null

    $tfsServer =  "http://ourServer";
    $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfsServer) 
    $Script:versionControlServer = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer] ) 
    $Script:recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full

}

以下是我们如何使用PowerShell调用TFS获取Merge和Branch信息以构建version.html文件的更多细节...

   # Need to get the last 5 changesets of Merge information for both MAIN and Iteration
    Setup-Tfs

    $baseLocation = "$/OurBaseLocation/"
    $locationForMain = $baseLocation + $OurProjectLocation

    # Query history for the TFS path
    $vCSChangeSets = $versionControlServer.QueryHistory($locationForMain, $recursionType, 5)

    # History of Merge changes to MAIN application (only 5 deep)
    "<table border='2'>" | Out-File $VersionFname -append
    "<caption>Merge Info For: $AppName </caption>" | Out-File $VersionFname -append

    # Build out headers
    "<TH>Changeset</TH><TH>Date</TH><TH>Comment</TH>" | Out-File $VersionFname -append

    Foreach ($vCSChangeSet in $vCSChangeSets) {
        # write row
        $changeset =  $vCSChangeSet.ChangesetID 
        $CheckinNotesName =  $vCSChangeSet.Comment
        $CreationDate =  $vCSChangeSet.CreationDate

        if ($CheckinNotesName.ToUpper().Contains("MERGE")){
            "<TR>" | Out-File $VersionFname -append
            "<TD>$changeset</TD><TD>$CreationDate</TD><TD>$CheckinNotesName</TD>" | Out-File $VersionFname -append
            "</TR>" | Out-File $VersionFname -append
        }
        if ($CheckinNotesName.ToUpper().Contains("BRANCH")){
            "<TR>" | Out-File $VersionFname -append
            "<TD>$changeset</TD><TD>$CreationDate</TD><TD>$CheckinNotesName</TD>" | Out-File $VersionFname -append
            "</TR>" | Out-File $VersionFname -append
        }
    }

    # close table add space
    "</table><BR/><BR/>" | Out-File $VersionFname -append

我的猜测是我的docker文件需要为&#34; Microsoft.TeamFoundation.VersionControl.Client&#34;添加一些内容。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我发现最有效的方法是放弃TFS的PowerShell名称空间。而是使用TFS API。这是获取单个WI属性的示例。

#============================================
# Get-TFSFieldsByWiId
#============================================
function Get-TFSFieldsByWiId([string]$Id) {

    $url = 'http://YourTFSUrl:YourPort/YourProject/_apis/wit/workitems?ids=' + $Id+'&$expand=all&api-version=YourVersion'

    # Step 1. Create a username:password pair
    $credPair = "$(''):$($password)"

    # Step 2. Encode the pair to Base64 string
    $encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))

    # Step 3. Form the header and add the Authorization attribute to it
    $headers = @{ Authorization = "Basic $encodedCredentials" }

    # Step 4. Make the GET request
    $responseData = Invoke-WebRequest -Uri $url -Method Get -Headers $headers -UseBasicParsing -Body ($QueryToRun|ConvertTo-Json) -ContentType "application/json"

    $data = $responseData.Content
    $data = $data | ConvertFrom-Json
    $WIDetails = $data.value
    return $WIDetails
}