更新
VSTS現在支持Git的LFS開箱。這只是在構建定義中激活選項Repository/Checkout files from LFS
的問題。這比下面的解決方案簡單得多。
我想帕斯卡Enable Git Remote Access建設的任務,但我不能使它發揮作用。調用git-lfs.exe不會崩潰,但它不會將LFS文件轉換爲真實文件。
下面是我如何使它工作。我首先必須在我的構建定義中啓用Allow Scripts to Access OAuth Token
選項。然後我創建了一個PowerShell腳本,拉的LFS依賴性:
# Inspired from here: http://ss64.com/ps/syntax-set-eol.html
function Set-UnixLineEndings([string]$file)
{
# Replace CR+LF with LF
$text = [IO.File]::ReadAllText($file) -replace "`r`n", "`n"
[IO.File]::WriteAllText($file, $text)
# Replace CR with LF
$text = [IO.File]::ReadAllText($file) -replace "`r", "`n"
[IO.File]::WriteAllText($file, $text)
}
if ((Test-Path env:SYSTEM_ACCESSTOKEN) -eq $false)
{
throw "OAuth token not available. Make sure that you select the option 'Allow Scripts to Access OAuth Token' in build 'Options' pane."
}
# git lfs needs the credentials of the git repository. When running
# under VSTS, these credentials are transfered to the git-lfs.exe
# application using the oauth token provided by VSTS. These
# credentials are stored in a file so that git lfs can find them.
$pwPath = Join-Path $PSScriptRoot pw.txt
$gitPwPath = $pwPath.Replace('\', '/') # Needs to be in unix format.
$repoUri = New-Object Uri $env:BUILD_REPOSITORY_URI
git config credential.helper "store --file=$gitPwPath"
@"
https://OAuth:$env:[email protected]$($repoUri.Host)
"@ | Set-Content $pwPath
# Again, needs to be in unix format... sigh...
Set-UnixLineEndings -file $pwPath
& ".\git-lfs.exe" pull
if ($LASTEXITCODE -ne 0)
{
throw 'Failed to pull LFS files.'
}
顯然,這假定您已經存儲的git-lfs.exe使用Git存儲庫,該文件沒有被LFS跟蹤。
我同意你的看法,你需要調用「git lfs fetch」。對於認證部分,您可以爲您的Visual Studio Team Services帳戶創建一個「個人訪問令牌」。檢查此鏈接瞭解詳細:https://msdn.microsoft.com/en-us/Library/vs/alm/Code/git/command-prompt#GetsetuptousethecommandprompttoolsCreateapersonalaccesstokenforyourVisualStudioTeamServicesaccount –
但在這種情況下,我需要定義一個單獨的服務帳戶,這可用於訪問代碼並將其憑據存儲在構建代理上。通常,構建代理可以在不需要服務帳戶的情況下籤出源代碼。有沒有辦法從cmd或ps腳本使用相同的方法? –