在 VSTF 构建中查看 git-lfs 文件
Check out git-lfs files on VSTF build
我在 VSTS 上托管了一个存储库,其中包含一个通过 git-lfs 存储的文件。如果我只是让 VSTS 构建检出存储库,它只会下载包含文件 ID 的 git-lfs 元数据文件。
这是 VSTS 如何获取其来源的输出:
Syncing repository: MyRepo (Git)
Checking out c84ef2f2bbad4fa3dc70dbd4100534390b9c8f18 to d:\work\s
Checked out branch refs/heads/develop for repository MyRepo at commit c84ef2f2bbad4fa3dc70dbd4100534390b9c8f18
我需要做什么才能签出真实文件?
编辑:我假设我需要在 VSTS 检查源后手动调用 git lfs fetch
。但是在这种情况下我该如何处理身份验证(这是 VSTS 所要求的)?
VSTS 现在有一个 Allow Scripts to Access OAuth Token
选项。在构建定义上设置此选项后,OAuth 可用于构建脚本。
我创建了一个 extension,其中包含构建任务,这些任务将远程 Url 更改为使用 OAuth 令牌访问远程存储库。
更新
VSTS 现在支持开箱即用的 git LFS。这只是在构建定义中激活选项 Repository / Checkout files from LFS
的问题。它比下面的解决方案简单得多。
我尝试了 Pascal 的 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: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 跟踪。
更新
我确认流程已更改,请忽略以下回答。
我不得不说我刚刚发现:
在您的构建定义中,select Repository 选项卡,然后选中选项 Checkout files from LFS
再简单不过了。
流程再次更新(2017 年 3 月)。这次您需要编辑构建定义的 "Get Sources" 部分。启用右上角的"Advanced Settings"选项,勾选"Checkout files from LFS".
选项
对于 TFS Build 2015 Update 4(这个东西的自托管风格,与云服务 VSTS 相对),"Checkout files from 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
和 运行 上述脚本的构建步骤。
我在 VSTS 上托管了一个存储库,其中包含一个通过 git-lfs 存储的文件。如果我只是让 VSTS 构建检出存储库,它只会下载包含文件 ID 的 git-lfs 元数据文件。
这是 VSTS 如何获取其来源的输出:
Syncing repository: MyRepo (Git)
Checking out c84ef2f2bbad4fa3dc70dbd4100534390b9c8f18 to d:\work\s
Checked out branch refs/heads/develop for repository MyRepo at commit c84ef2f2bbad4fa3dc70dbd4100534390b9c8f18
我需要做什么才能签出真实文件?
编辑:我假设我需要在 VSTS 检查源后手动调用 git lfs fetch
。但是在这种情况下我该如何处理身份验证(这是 VSTS 所要求的)?
VSTS 现在有一个 Allow Scripts to Access OAuth Token
选项。在构建定义上设置此选项后,OAuth 可用于构建脚本。
我创建了一个 extension,其中包含构建任务,这些任务将远程 Url 更改为使用 OAuth 令牌访问远程存储库。
更新
VSTS 现在支持开箱即用的 git LFS。这只是在构建定义中激活选项 Repository / Checkout files from LFS
的问题。它比下面的解决方案简单得多。
我尝试了 Pascal 的 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: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 跟踪。
更新
我确认流程已更改,请忽略以下回答。
我不得不说我刚刚发现:
在您的构建定义中,select Repository 选项卡,然后选中选项 Checkout files from LFS
再简单不过了。
流程再次更新(2017 年 3 月)。这次您需要编辑构建定义的 "Get Sources" 部分。启用右上角的"Advanced Settings"选项,勾选"Checkout files from LFS".
选项对于 TFS Build 2015 Update 4(这个东西的自托管风格,与云服务 VSTS 相对),"Checkout files from 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
和 运行 上述脚本的构建步骤。