2017-08-30 72 views
1

我想對於檢查出文件的生成創建腳本,編輯它,並檢查它放回PowerShell的/ VSTS建設 - 存儲憑證獨立/用戶的不可知論行書

我希望它在作爲開發人員運行時工作,或作爲構建代理工作。

我有一個類似於this的解決方案,其中密碼存儲在文件中併爲構建檢索。

文件創建:

read-host -prompt Password -assecurestring | convertfrom-securestring | out-file .\ps-password.pwd -ErrorAction Stop 

文件使用:

# *VSTS Login* 
$Username = $tfsUserName 
$Password = Get-Content $tfsUserPasswordPath | ConvertTo-SecureString 

$creds = New-Object -typename System.Management.Automation.PSCredential -ArgumentList $Username,$Password 
$tfsServer = New-Object System.Uri("https://myaccount.visualstudio.com") 
$tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($tfsServer,$creds) 
$tfsCollection.Authenticate() 
"***************** Authenticated *****************" 

" *VSTS Check Out file* from $fileToUpdate" 
Add-TfsPendingChange -Edit -Item $fileToUpdate -Verbose -ErrorAction Stop -wa 0 


# read the file, update the number and save it back 
$stuff = Get-Content $fileToUpdate 
# modify stuff 
Set-Content -Value $stuff -Path $fileToUpdate 


# *VSTS Check In* Check in the file after changes. 
" *VSTS Check In" 
New-TfsChangeset -Item $fileToUpdate -Verbose -Comment "***NO_CI***" -Override true -ErrorAction Stop 

SecureStrings是基於機器上/用戶賬戶,所以當我從Powershell ISE作爲運行生成正常工作我的帳戶,但不是從構建服務器觸發(它現在運行爲NetworkService)。

我試過以下this post創建密碼文件爲'網絡服務',並嘗試key作爲安全字符串,但在我的用戶和網絡服務下無法獲得任何東西。

我該如何簡單地存儲將獨立運行腳本的用戶的憑據?

或者這只是錯誤的方式來做到這一點,我應該以某種方式使用PAT?

回答

2

構建允許您通過構建定義中的設置來訪問PAT令牌。這些動態生成的PAT令牌,所以你不需要在任何地方存儲任何祕密。

要在開發人員的機器上運行腳本,您可以要求開發人員輸入PAT或使用if if邏輯,您可以在其中詢問用戶名密碼。

https://www.visualstudio.com/en-us/docs/build/scripts/#use-the-oauth-token-to-access-the-rest-api

更新(完全解決方案)更多信息:

在你的構建,你必須去到 '選項',然後打開 '允許腳本訪問的OAuth令牌'。

您的最終腳本將如下所示。

Add-PSSnapin Microsoft.TeamFoundation.PowerShell 
# This file requires the TFS Power Tools (2015+). When installing, you must select Custom Installation and select PowerShell Cmdlets 

# *VSTS Login* 
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/$($env:SYSTEM_DEFINITIONID)?api-version=2.0" 
Write-Host "URL: $url" 
$definition = Invoke-RestMethod -Uri $url -Headers @{ 
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" 
} 
Write-Host "Definition = $($definition | ConvertTo-Json -Depth 100)" 
"***************** Authenticated *****************" 

" *VSTS Check Out file* from $fileToUpdate" 
Add-TfsPendingChange -Edit -Item $fileToUpdate -Verbose -ErrorAction Stop -wa 0 


# read the file, update the number and save it back 
$stuff = Get-Content $fileToUpdate 
# modify stuff - make sure you actually make a change! 
Set-Content -Value $stuff -Path $fileToUpdate 


# *VSTS Check In* Check in the file after changes. 
" *VSTS Check In" 
New-TfsChangeset -Item $fileToUpdate -Verbose -Comment "***NO_CI***" -Override true -ErrorAction Stop 
+0

已經向上致謝,感謝您的幫助,但這似乎讓我陷入了一大堆其他問題。不確定這種方式是否與我現有的腳本兼容? – HockeyJ

+1

拍攝你可能有的任何問題,我想你可以用這個令牌替換密碼,它應該可以工作。 –

+0

賓果!這是答案。我已經用搜索者的最終腳本更新了你的答案。要等到我可以爲你添加+50賞金,那麼我會將其標記爲答案。謝謝! – HockeyJ