Java, Web and Mobile

Blog

Mirror GitLab repos

|
Continuous Integration

Did you ever had the need to sync your Git Repository hosted on a GitLab CE instance to another Git repository (doesn’t have to be GitLab)? Here’s a short how to by just using the features offered by GitLab CE.

Approach

The approach described below uses a GitLab CI job that is triggered on every push, clones the code in a docker container and then pushes the code to the remote repository.

Please be careful: The keys for the communication with the remote Git repository are stored in the repository. But usually the project team also has access to the remote repository.

Set up keys for remote

In order to be able to push code to the remote repository we store the SSH keys in the local repo and use them then from within the container to push the code.

The keys are generated in a subdirecctory in the Git repository:

In order for the docker container to trust the foreign Git server you add the host to the known_hosts file

After that just add the keys folder with the gerenated files to your Git repository.

Set up GitLab CI job

In your .gitlab-ci.yaml you have to add a job that is able to push the code to the remote server. The used job is using a docker image based on Alpine Linux. The necessary tools (bash, git, openssh) are then installed.

For the build the keys that were generated in the step before are copied to the system SSH folders (~/.ssh). Then the necessary remote host is added and the push can be performed.

The complete example is shown below:

Conclusion

Now you have a simple mirroring of your Git repo that works with the standard GitLab CE features.