Skip to content

Instantly share code, notes, and snippets.

@wehappyfew
Created January 18, 2018 09:05
Show Gist options
  • Save wehappyfew/32b307505d2e57d06bd43a30e35f2df3 to your computer and use it in GitHub Desktop.
Save wehappyfew/32b307505d2e57d06bd43a30e35f2df3 to your computer and use it in GitHub Desktop.
Add a .pem file via on-build ARGS and clone a project from a private repo.
#Notice: add your .pem private key in the same path as the Dockerfile
# get the private key as on-build argument
ARG SSH_KEY_FILE = some-file.pem
ARG REPO_USER = some-user
ARG PROJECT = some-project
ENV REPO_URL = gitlab.example.com
#add the private key & set its permissions
RUN mkdir /root/.ssh/
COPY ./${SSH_KEY_FILE} /root/.ssh/${SSH_KEY_FILE}
RUN chmod 600 /root/.ssh/${SSH_KEY_FILE} && \
# make sure your domain is accepted
touch /root/.ssh/known_hosts && \
ssh-keyscan ${REPO_URL} >> /root/.ssh/known_hosts && \
#clone the project
ssh-agent bash -c 'ssh-add /root/.ssh/${SSH_KEY_FILE}; git clone git@${REPO_URL}:${REPO_USER}/${PROJECT}.git' && \
# move the project to a temp folder to copy to the next stage
mv ${PROJECT} stash_folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment