更新
VSTS 现在支持开箱即用的 git LFS。只需激活Repository / Checkout files from LFS
构建定义中的选项即可。它比下面的解决方案简单得多。
我尝试了 Pascal 的启用 Git 远程访问构建任务,但我无法使其工作。调用 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:SYSTEM_ACCESSTOKEN@$($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 未跟踪此文件。