5

我有一个托管在 VSTS 上的存储库,包含一个通过 git-lfs 存储的文件。如果我只是让 VSTS 构建签出存储库,它只会下载包含文件 ID 的 git-lfs 元数据文件。

这是 VSTS 如何获取其来源的输出:

Syncing repository: MyRepo (Git)
Checking out c84ef2f2bbad4fa3dc70dbd4100534390b9c8f18 to d:\work\73\s
Checked out branch refs/heads/develop for repository MyRepo at commit c84ef2f2bbad4fa3dc70dbd4100534390b9c8f18

我需要做什么才能签出真实文件?

编辑:我假设我需要git lfs fetch在 VSTS 检查源后手动调用。但是在这种情况下,我该如何处理身份验证(VSTS 要求)?

4

5 回答 5

7

该流程已再次更新(2017 年 3 月)。这次您需要编辑构建定义的“获取源”部分。启用右上角的“高级设置”选项并选中“从 LFS 签出文件”选项。

在此处输入图像描述

于 2017-03-25T23:15:50.393 回答
6

更新

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 未跟踪此文件。

于 2016-03-08T19:29:01.027 回答
3

更新

我确认流程已更改,请忽略以下答案。


我不得不说我只是发现:

在您的构建定义中,选择Repository选项卡,然后选中Checkout files from LFS选项

这再简单不过了。

于 2016-10-23T15:34:52.127 回答
2

对于 TFS Build 2015 Update 4(这个东西的自托管风格,而不是云服务 VSTS),“从 LFS 签出文件”选项不存在,因此,我们必须这样做:

  • 检查Allow Scripts to Access OAuth Token选项选项卡。
  • 将此批处理文件提交到某个存储库中:

    REM This script is intended to fetch large (LFS) files during the TFS Build process.
    
    REM Solution derived from https://github.com/Microsoft/vsts-agent/issues/1134
    
    git config --unset-all http.extraheader
    git config --add http.extraheader "AUTHORIZATION: bearer %SYSTEM_ACCESSTOKEN%"
    git lfs fetch
    git lfs checkout
    git config --unset http.extraheader        
    
  • 添加 a 的构建步骤Batch Script,然后运行上述脚本。

于 2018-04-12T17:56:47.120 回答
0

VSTS 现在有一个Allow Scripts to Access OAuth Token选项。通过在构建定义上设置此选项,OAuth 可用于构建脚本。

我创建了一个包含构建任务的扩展,这些构建任务将远程 URL 更改为使用 OAuth 令牌访问远程存储库。

于 2015-12-11T07:47:13.040 回答