【问题标题】:Check out git-lfs files on VSTF build查看 VSTF 构建中的 git-lfs 文件
【发布时间】:2016-03-09 02:14:36
【问题描述】:

我有一个托管在 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

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

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

【问题讨论】:

  • 我同意你的观点,你需要调用“git lfs fetch”。对于身份验证部分,您可以为您的 Visual Studio Team Services 帐户创建一个“个人访问令牌”。查看此链接了解详情:msdn.microsoft.com/en-us/Library/vs/alm/Code/git/…
  • 但在这种情况下,我需要定义一个单独的服务帐户,该帐户可用于访问代码并将其凭据存储在构建代理上。通常,构建代理有一种方法可以在不需要服务帐户的情况下签出源代码。有没有办法在 cmd 或 ps 脚本中使用相同的方法?

标签: git tfsbuild azure-devops git-lfs


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    更新

    VSTS 现在支持开箱即用的 git LFS。只需在构建定义中激活选项Repository / Checkout files from LFS。它比下面的解决方案简单得多。


    我尝试了 Pascal 的 Enable Git Remote Access 构建任务,但我无法让它工作。调用 git-lfs.exe 不会崩溃,但不会将 LFS 文件转换为真实文件。

    这是我如何使它工作的。我首先必须在我的构建定义中启用Allow Scripts to Access OAuth Token 选项。然后我创建了一个提取 LFS 依赖项的 PowerShell 脚本:

    # 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 跟踪。

    【讨论】:

    • 有一个带有 git-lfs 的 open issue 不支持在远程 URL 中传递的凭据。像您一样将令牌存储在文件中是目前支持的唯一方法。
    【解决方案3】:

    更新

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


    我不得不说我只是发现:

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

    选项

    再简单不过了。

    【讨论】:

    • 是的,同时此功能在 VSTS 中开箱即用。因此接受这个答案作为解决方案。
    【解决方案4】:

    对于 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        
      
    • 添加Batch Script的构建步骤,并运行上述脚本。

    【讨论】:

    • 感谢extraheader 语法。 +1
    【解决方案5】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-10-25
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      相关资源
      最近更新 更多