Docker COPY fails Github actions with git lfs

Viewed 945

I am using Github actions to build docker images with a file from git lfs.
I have the following step:

     - 
        name: Checkout code
        uses: actions/checkout@v2
        with:
          lfs: true
      - 
        run: head something.json

Which prints the data correctly.

When I run the same command from my dockerfile in the build stage,
I get the following lfs metadata:

#7 [4/5] RUN head something.json
#7 sha256:***
#7 0.138 version https://git-lfs.github.com/spec/v1
#7 0.138 oid sha256:***
#7 0.138 size 1230424
#7 DONE 0.2s

Dockerfile:

FROM bitnami/python:3.9.1

COPY . .

RUN head something.json

Action:

jobs:
  build-image:
    runs-on: ubuntu-latest
    steps:
      - 
        name: Checkout code
        uses: actions/checkout@v2
        with:
          lfs: true
      - 
        run: head something.json
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          push: true
          file: Dockerfile

Why the docker build takes the metadata and not the actual file?

1 Answers

The fix

Run this step after your git checkout:

- run: git lfs pull

And add to your docker build action:

with:
  context: .

Fully working example on this repo: https://github.com/roim/docker-action-lfs

Why?

2 separate issues:

  1. The docker build in docker/build-push-action@v2 isn't using the git repo from the checkout action you specified. You can confirm by reading logs for that action and verifying it pulls a git repo again, into a new folder:
/usr/bin/docker buildx build --tag ***/www:latest --iidfile *** --secret id=*** --file ./Dockerfile --push https://github.com/***/www.git#heads/main
#1 [internal] load git source https://github.com/***/www.git#heads/main
#1 sha256:***
#1 0.013 Initialized empty Git repository in /var/lib/buildkit/runc-overlayfs/snapshots/snapshots/1/fs/
#1 0.334 ***    refs/heads/main
#1 0.622 From https://github.com/***/www
#1 0.622  * [new branch]      main       -> heads/main
#1 0.623  * [new branch]      main       -> origin/main
  1. The default checkout action seems to fetch LFS objects, but not resolve them in the working directory. Explicitly running git lfs pull will fix that.

--

Note that there is a long standing issue reported in 2015 where DockerHub doesn't support git-lfs--that's specific to DockerHub, not github actions.

As to why the default context doesn't respect lfs, I opened a new issue for that.

Related