Running git lfs pull on AWS CodeBuild
Overview
The process of checking out from GitHub in a CodePipeline source action started failing with an error, causing the pipeline to abort.
The cause was that the files under app/assets/images/ in the Rails project being checked out had grown bloated in size.
This is the story of how I introduced Git LFS to work around that issue.
First, I contacted AWS Support about the problem
They told me there was an ongoing incident with the GitHub source action v2.
1 | ただいまリポジトリサイズが大きい場合に GitHub ソースアクション v2 がお問い合わせいただいたエラーで失敗する事象が発生しており、製品担当部署にて原因調査を進めております。 |
According to them, the issue could be worked around by using CodeCommit or S3.
- Temporarily switch from GitHub to CodeCommit
- Impacts the development flow
- Change the deployment flow, such as generating the files to upload to S3
- Would file generation be done with something like GitHub Actions? That seems likely to incur verification costs
Rather than a temporary workaround, I looked for an approach that would have minimal impact on the current operation and keep verification costs low.
Using Git Large File Storage
Git Large File Storage (LFS) replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise.
I figured that by using Git LFS, I could reduce the size of what gets checked out from GitHub, and then run git lfs pull where needed to work around the issue.
Changes on the repository being checked out
Add the following to the target repository.
.gitattributes
1 | app/assets/images/**.png filter=lfs diff=lfs merge=lfs -text |
Since the cause of the size bloat was the images under Rails’ app/assets/images/, I specified them as shown above.
Changes on the CodePipeline & CodeBuild side
The explanation is based on Terraform.
- Configure the CodePipeline source action to do a full clone.
- If you don’t need to use Git metadata, a shallow clone is preferable since it checks out fewer files
1 | resource "aws_codepipeline" "deploy" { |
- Add permissions to the IAM Role attached to CodeBuild
1 | # Git LFS 管理のオブジェクトを pull する為 |
- In the buildspec, install the git-lfs command and run
git-lfs pull.- Specifies v3.2.0, the latest at the time of writing
1 | phases: |
With the above changes, I was able to avoid the source action error and confirmed that the deployment succeeded.
By the way
Thinking “if CodePipeline doesn’t work, how about GitHub Actions?”, I gave it a try, but ran into no left space on device.
That’s all.
I hope this is helpful.
Running git lfs pull on AWS CodeBuild
https://kenzo0107.github.io/en/2022/06/16/git-lfs-pull-on-codebuild/