Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergeyKozlov/2ba2057480bb0b9cd4f02479c8a15b52 to your computer and use it in GitHub Desktop.
Save SergeyKozlov/2ba2057480bb0b9cd4f02479c8a15b52 to your computer and use it in GitHub Desktop.
Clone a GitLab git repository inside a Dockerfile/build run inside GitLab CI
# Add the following key to the 'Deploy Keys' section in Settings/Repository of your GitLab repository
# Dockerfile:
FROM centos:7
ARG SSH_KEY_GITLAB_DEPLOY_KEY
RUN \
### ssh configuration, see https://docs.gitlab.com/ee/ci/ssh_keys/#ssh-keys-when-using-the-docker-executor
# run ssh-agent inside build environment
eval $(ssh-agent -s); \
# add ssh-key from GitLab variable to ssh agent store incl. fixing line endings
echo "${SSH_KEY_GITLAB_DEPLOY_KEY}" | tr -d '\r' | ssh-add -; \
# create the SSH directory and give it the right permissions
mkdir -p ~/.ssh; \
# Allow access to gitlab.your.lan
echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config; \
chmod 700 ~/.ssh; \
\
git clone ssh://git@gitlab.your.lan:2222/yourgroup/yourrepository.git;
# Run with docker build and include Deploy Key fully as build-arg
docker build . --force-rm --build-arg SSH_KEY_GITLAB_DEPLOY_KEY="-----BEGIN OPENSSH PRIVATE KEY-----
c8g6t5...."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment