Git LFS 指標檔取代 PNG?檢查 CI 建置實際讀取的檔案
Git LFS pointer instead of a PNG? Check the file your CI build actually reads
若 checkout 未從 Git LFS 取回原始內容,名為 .png 的檔案仍可能是文字指標,令 CI 建置無法解碼圖片。文章示範以 `file`、`git check-attr` 和 PNG 八字節簽名檢查檔案,並在 GitHub Actions 的 checkout 步驟設定 `lfs: true`;本地實驗中,取回 LFS 物件後,同一路徑恢復為 680 × 300 PNG。
assets/banner.png exists. Git reports a clean checkout. The image decoder still rejects it. Before touching your rendering code, check whether the file contains a PNG or a Git LFS pointer with a .png filename.
Git has the reference; LFS has the bytes
Git LFS stores a small text pointer in the commit and keeps the original content separately. A checkout needs to replace the pointer with the binary file before a build can read that asset.
Start with the same path your build reads. A filename tells you nothing about its contents. If file reports ASCII text and the filter is lfs, check the pointer before blaming your decoder.
file assets/banner.png
git check-attr filter -- assets/banner.png
In a disposable local repository, a checkout with GIT_LFS_SKIP_SMUDGE=1 preserved the pointer at the exact path. A small release preview then left the banner area empty because the decoder could not read it. This is the actual rendered preview from the local lab, not an image of a GitHub-hosted runner.
The text begins with version https://git-lfs.github.com/spec/v1. The next lines identify the binary by SHA-256 and report its intended size. In the lab the size line said 5,198 bytes; it describes the full PNG, not the much shorter pointer on disk.
Fetch the object before rendering
On GitHub Actions, enable the lfs input on your checkout step when the job needs the repository's LFS files. The checkout action documentation lists that input as off by default.
steps:
- uses: actions/checkout@v6
with:
lfs: true
For a narrower local fetch, run this from the checkout with Git LFS available and access to the repository's LFS objects:
git lfs pull --include='assets/banner.png' --exclude=''
The checkout input is convenient when the build uses many files; the targeted command was enough for this one banner. Fetch the assets for the job that reads them, rather than assuming a different job populated this job's checkout.
After that pull against the same disposable local remote, file reported a 680 × 300 PNG. Rerunning the same preview rendered the banner from the file at the same path.
There's a quick way to see that the commit itself did not change: inspect its recorded blob with git cat-file blob HEAD:assets/banner.png. In the experiment it still contained the original pointer, but the SHA-256 of the working file matched the object ID. A clean working tree by itself did not tell those two states apart.
Fail at the input, not at the end of the build
If PNG is a hard requirement, check its eight-byte signature first. Use a decoder too if the release already has one available. The signature catches the text pointer; an actual read checks more than the first few bytes.
from pathlib import Path
from PIL import Image
asset = Path("assets/banner.png")
with asset.open("rb") as source:
if source.read(8) != b"\x89PNG\r\n\x1a\n":
raise SystemExit(f"{asset}: expected PNG; inspect the checkout for an LFS pointer")
with Image.open(asset) as image:
image.verify()
If pulling still leaves text, save the fetch error and check access to the LFS object or any reported quota problem. Do not publish a build artifact that quietly dropped an image. The useful distinction is which step failed: resolving the tracked object, or decoding binary data that actually arrived.
Sources: Git LFS pointer specification, git lfs pull manual, and actions/checkout. Visuals: original diagrams and two actual renders from a disposable Git 2.56.1 / Git LFS 3.6.1 local reproduction, with a synthetic banner. The GitHub Actions snippet illustrates the documented input; the lab did not run on GitHub.
Disclosure: This article was prepared using a fully autonomous AI workflow. The local Git LFS reproduction and PNG checks were executed and the results reviewed.
來源:DEV Community · dev.to








