gitazure-devopsgit-lfs

Upload a binary file to git LFS through AzureDevOps Services REST API?


I am using the AzureDevOps Services REST API for automation. I am pushing a binary file to my repo with this API add-a-binary-file.

However, these binary files are becoming increasingly large, and I have started to use git lfs for my project. I do not see a way to upload the file to git lfs and update the reference through the API though. is this supported? If so, how can I do it?

I have reviewed the documentation, but I haven't found any example where this is done. The sample page where they explain how to add a binary file just uploads an image directly into the repository.


Solution

  • Currently, there is no Azure DevOps Services REST API to upload binary files to Git LFS.

    Normally, we can use Git LFS by installing the client, setting up LFS tracking for files on a local repository, and then pushing changes to Azure Repos. If you need to automate this process, you can use Git LFS commands in a script. This may require some scripting to achieve your needs.

    For example, in Azure DevOps, you can run the git lfs command to upload a binary file to git LFS in a script task.

    trigger:
    - none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - checkout: self
      persistCredentials: true
    - script: |
        git config --global user.email "you@example.com"
        git config --global user.name "Your Name"
        git checkout main
        
        # create abc.largefile 
        touch abc.largefile
        echo "something" > abc.largefile
        
        echo "Adding large files to Git LFS"
        git lfs track "*.largefile"
        git add "*.largefile"
        
        echo "Committing changes"
        git add .gitattributes
        
        git commit -m "Track large files with Git LFS"
        echo "Pushing changes"
        git push origin main
        
        git show HEAD