0

I have a GitHub actions CI which builds a my app in the last version of this app .git directory is mandatory for building it the problem is that GitHub action don't remove it in the docker build step of the CI.

Here is my CI template:

on:
  push:
  pull_request:

jobs:
  build:
    runs-on: ubuntu-20.04
    if: github.ref == 'refs/heads/3.3.5-MV'
    steps:
      - uses: actions/checkout@v2
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          file: ./Dockerfile
          tags: foo/bar:latest
          push: true
      -
        name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}

The Dockerfile

FROM ubuntu:focal
ENV TZ=Europe/Paris
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt update && apt upgrade -y && apt install -y [all my dependencies]

WORKDIR /usr/src/app

COPY . .

WORKDIR /usr/src/app/build

#default path on docker image /usr/local
RUN cmake ../ -DCMAKE_INSTALL_PREFIX=/usr/local/ -DTOOLS=0 

RUN make -j $(nproc) install

WORKDIR /usr/local/bin
3
  • Your Dockerfile controls what gets installed in your image. What does your Dockerfile look like? Commented Feb 17, 2022 at 12:34
  • @larsks I added the Dockerfile but as it works by the past and it works locally. But in the last version of the app, .git has became mandatory to be able to build. I checked the previously image created by GitHub action and the git repository is removed automatically by GitHub action. I think uses: actions/checkout@v2 copy everything except that to minimise the weight of the image. Commented Feb 17, 2022 at 13:04
  • @larsks here is the error #12 1.009 fatal: not a git repository (or any of the parent directories): .git Commented Feb 17, 2022 at 13:08

1 Answer 1

4

The issue is in the docker/build-push-action@v2 action, which by default ignores the checkout created using the actions/checkout@v2 action:

By default, this action uses the Git context so you don't need to use the actions/checkout action to check out the repository because this will be done directly by BuildKit.

The git reference will be based on the event that triggered your workflow and will result in the following context: https://github.com/<owner>/<repo>.git#<ref>.

When you pass a git build context to docker build, it won't include the .git directory.

If you read through the documentation for the docker/build-push-action@v2 action, you'll see that you can override this behavior:

However, you can use the Path context using the context input alongside the actions/checkout action to remove this restriction.

You would need to modify your workflow so that it includes an explicit context: ., like this:

        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          tags: foo/bar:latest
          push: true

I've put together an example repository that demonstrates this here; you can see the verification in this build run.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! It works like a charm. The purpose of the context wasn't very clear to me have a good day !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.