我正在尝试为构建创建一个脚本,用于签出文件、编辑文件并将其重新签入。
我希望它在作为开发人员或构建代理运行时能够工作。
我有一个类似的解决方案this https://stackoverflow.com/a/25455769/852806,密码存储在文件中并在构建时检索。
i.e.
文件创建:
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
目前)。
我尝试过以下这个帖子 https://stackoverflow.com/questions/6911310/how-do-i-run-as-network-service将密码文件创建为“网络服务”并尝试key https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertto-securestring?view=powershell-5.1对于安全字符串,但在我的用户和网络服务下无法让任何东西工作。
如何简单地存储独立于运行脚本的用户而工作的凭据?
或者这只是错误的方法,我应该以某种方式使用 PAT?
构建允许您通过构建定义中的设置访问 PAT 令牌。这些是动态生成的 PAT 令牌,因此您无需在任何地方存储任何秘密。
要在开发人员的计算机上运行脚本,您可以要求开发人员输入 PAT 或使用 if else 逻辑,您可以向他询问用户名密码。
更多信息请访问
https://www.visualstudio.com/en-us/docs/build/scripts/#use-the-oauth-token-to-access-the-rest-api 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)