How to have a mirror repository on GitLab without the premium

Mohammed Ragab
May 12 · 3 min read

In this article, I will explain an idea to have a semi-mirror repoistory feature without a premium version from GitLab as the mirror with pull mode is a premium

What is a mirror repository feature first?

If you have for example a codebase on a repository on GitHub or azure-DevOps and you want to have a mirror on GitLab to use for example the GitLab CI/CD feature or to have a backup repository and so on. GitLab offers a mirror repository feature with pull/push mode but only push available as free also the CI/CD repository is a premium.

We can make a very simple step to have this a simulation for this feature by writing a GitLab pipeline to do this using the following steps

  • Create a new empty GitLab project
  • Add a new .gitlab-ci.yml (GitLab CI) with the following content
stages:
- mirror-from-external
mirror-from-external:
stage: mirror-from-external
image: ubuntu:18.04
before_script:
- apt-get update -y && apt-get install openssh-client -y
- apt install git -y
- eval $(ssh-agent -s)
- echo "$GIT_SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan $GIT_LAB_HOST >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- git config --global user.name "${GIT_USER_NAME}"
- git config --global user.email "${GIT_USER_EMAIL}"
- ssh "git@$GIT_LAB_HOST"
script:
- echo $SOURCE_REPOSITORY
- git clone --mirror $SOURCE_REPOSITORY Your_Repo_folder_name
- cd Your_Repo_folder_name
- git remote remove origin
- git remote add origin $DESTINATION_REPOSITORY
- git push --prune --all
- git push --prune --tags
only:
- branches

Explanation :

We just use an ubuntu image with ssh and git then we cloned from the source repository and remove the remote origin then add the GitLab repository as a remote origin then push all and tags with prune to delete the deleted branches and tags

so now we have our pipeline and every time trigger this pipeline will take a snapshot from the source repository to the GitLab repository.

You can add a CI scheduler to trigger the pipeline automatically just go to CI/CD and choose the schedule option and create a new schedule.

Also, you can add a git hook in your source repository in your local machine to trigger the pipeline every push on your source repository by using GitLab API to do this

  • Go to Settings >> CI/CD and at the end expand the Pipeline triggers accordion and click add a trigger

Then we have an API to trigger our pipeline

  • Go to your .git\hooks on the source repository path on your development machine and create a new hook file with the name “pre-push” with the following content
#!/bin/sh
curl -X POST -F token=84accae6178539643a7177de83d552 -F ref=master https://your-git-lab-url/api/v4/projects/{projectID}/trigger/pipeline

Just replace the URL with your API URL as explained on the trigger

After that, every git push command will trigger the mirror pipeline to work that will take a snapshot from the source repository to the GitLab repository and that is all.

I hope that I helped you thanks.

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Mohammed Ragab

Written by

Software engineer

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store