Check out git-lfs files on VSTF build

后端 未结 5 1006
迷失自我
迷失自我 2021-01-03 08:56

I\'ve a repository hosted on VSTS, containig a file stored through git-lfs. If I just let VSTS build checkout the repository it just downloads the git-lfs metadata file cont

5条回答
  •  轮回少年
    2021-01-03 09:22

    Update

    VSTS now supports git LFS out of the box. It's just a matter of activating the option Repository / Checkout files from LFS in the build definition. It is much simpler than the solution below.


    I tried Pascal's Enable Git Remote Access build task but I was not able to make it work. Calling git-lfs.exe does not crash but it does not convert the LFS files to the real files.

    Here is how I was able to make it work. I first had to enable the Allow Scripts to Access OAuth Token option in my build definition. I then created a PowerShell script that pulls the LFS dependencies:

    # 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.'
    }
    

    This obviously assumes that you have stored git-lfs.exe in your git repository AND that this file is not tracked by LFS.

提交回复
热议问题