簡體   English   中英

通過Powershell在Docker Windows容器中訪問TFS(在Prem上)

[英]Accessing TFS (on prem) via powershell in docker windows container

我有一個運行windowservercore的window 2016服務器。 我正在將CI管道移入容器。 在我們的過程中,我們構建一個version.html文件。 該文件包含生成數據(如生成日期和生成nbr)以及有關已發生的合並/分支的TFS 2017項目信息。

我們與TeamCity一起運行了一個PowerShell腳本,該腳本將連接並針對TFS 2017運行查詢。因此,我在TFS的Docker Hub上進行了查找,但沒有任何運氣。 我還嘗試在docker hub上查看Microsoft,但未找到任何內容。

我試圖創建一個新的碼頭工人文件

    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以獲得合並和分支信息以構建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文件需要為“ Microsoft.TeamFoundation.VersionControl.Client”添加一些內容

任何幫助,將不勝感激。

我發現最有效的方法是放棄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
}

暫無
暫無

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

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