Home > OS >  How can I copy `.git` directory to the container with GitHub actions?
How can I copy `.git` directory to the container with GitHub actions?

Time:02-18

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

CodePudding user response:

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.

  • Related