Deploy repository as part of a docker image

Hi,

I want to include the repository in a Docker image. Or to express it differently I am using docker to build a docker image which should contain my repository

In the docker file I have the following lines:

## Copy us into the container
COPY . /tmp/working

## Create the repository
RUN git clone /tmp/working /repo

# We need to prepare the branches otherwise cloning from them will fail
WORKDIR /repo
RUN \
  # store the current branch
  CURRENT_BRANCH=`git branch | grep "^*" | sed 's/* //g' -` && \
  # pull all remote references into our temporary repository
  git pull --all && \
  # check out all branches so that they are available
  git branch -a | tail --lines=+3 | while read ref; do echo $ref | sed 's/remotes\/origin\///g' - | xargs git checkout; done && \
  # checkout the branch we have been on
  git checkout $CURRENT_BRANCH && \
  # debug the branches we have in the image
  git branch -a

When I build this image on my computer it works fine but when used during the CI process it fails since the process does perform a clone but leaves me with a detached head. I tried setting the stragey variable with no effect :frowning:

.gitlab-ci.yml

build_image:
  stage: test
  image: docker:20-git
  variables:
    GIT_STRATEGY: clone
  services:
    - docker:18-dind
  tags:
    - docker
  script:
    - docker --version
    - docker build -f Dockerfile --build-arg CURRENT_BRANCH=$CI_COMMIT_BRANCH --build-arg GITDIR=$CI_PROJECT_DIR .

Any suggestions what I could do to get the full repository into the docker container building the image during the CI process?

Hi @mado89
why don’t you just clone the repo from GitLab directly in container during Docker build?

Dockerfile

ARG REPOPATH
ARG CURRENT_BRANCH

RUN git clone "$REPOPATH" /repo &&\
         cd /repo &&\
         git checkout "$CURRENT_BRANCH"

WORKDIR /repo

job definition

build_image:
  stage: test
  image: docker:20-git
  services:
    - docker:18-dind
  tags:
    - docker
  script:
    - export REPOPATH="https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}:${CI_SERVER_PORT}/${CI_PROJECT_PATH}.git"
    - docker build -f Dockerfile --build-arg CURRENT_BRANCH=$CI_COMMIT_BRANCH --build-arg REPOPATH=$REPOPATH .

Thanks @balonik! I was thinking in the same direction. Thank you for also providing a working solution for your suggestion!