Skip to content

Mirror Bitbucket / Gitlab / Github repositories using simple webhooks. Alpine based, pure bash

License

Notifications You must be signed in to change notification settings

karser/gitbitlabhub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitBitLabHub: Mirror your repositories between Bitbucket / Gitlab / Github using simple webhooks.

Docker Pulls

  • It's a simple shell script (written in pure bash, even the webserver part).
  • The docker image is Alpine based and the size is about 10 MB.
  • One container mirrors one repository.

Why mirror repositories?

  • You can pipe multiple platforms. For example, use Bitbucket private repositories as a primary storage and self-hosted Gitlab instance as a purely CI/CD tool.
  • Backup your primary repository to another platform.

Video tutorial

Video tutorial

How to setup mirroring

  1. Generate an ssh key for source and destination repositories:
ssh-keygen -t rsa -f ~/.ssh/project_id_rsa
  • It will generate 2 keys, the PRIVATE key to ~/.ssh/project_id_rsa and the PUBLIC key to ~/.ssh/project_id_rsa.pub.
  • The PUBLIC key is used as Deploy Key while the PRIVATE key should be used in SRC_DEPLOY_KEY and DEST_DEPLOY_KEY env variables.
  1. Add this PUBLIC key as a Deploy Key for the source and destination repositories. Depending on the platform it's called Deploy Key or Access key. See how to add Deploy/Access Key to bitbucket, bitbucket access keys, gitlab, github.
  • Add the PUBLIC key to the source repo with read-only access.
  • Add the PUBLIC key to the destination repo with write access.
  1. Run this container in docker with all environment variables configured properly (see How to run section).
  • Use the PRIVATE key for SRC_DEPLOY_KEY and DEST_DEPLOY_KEY env variables. Don't forget to encode it with base64: base64 -w 0 < ~/.ssh/project_id_rsa
  • If you configured everything properly it should mirror the repo after the first launch. If it didn't see the logs in the container output.
  1. Create the webhook in the source repository. Using default settings should be enough. See how to create a webhook in bitbucket, gitlab, github.

How to run

How to run using docker

docker run -it \
    -e [email protected]:vendor/src_repo.git \
    -e [email protected]:2222/vendor/dest_repo.git \
    -e SRC_DEPLOY_KEY=base64_encoded_key== \
    -e DEST_DEPLOY_KEY=base64_encoded_key== \
    -p 8181:8080/tcp \
    karser/gitbitlabhub

Where SRC_DEPLOY_KEY and DEST_DEPLOY_KEY are PRIVATE ssh keys encoded with base64. You can use the same key for both though. It can be obtained by running

ssh-keygen -t rsa -f ~/.ssh/project_id_rsa
base64 -w 0 < ~/.ssh/project_id_rsa

With this configuration the webhook url will be http://localhost:8181

How to run using docker-compose and traefik

version: '3.3'

services:
  gitbitlabhub_repo1:
    image: karser/gitbitlabhub
    restart: always
    networks:
      - webproxy
    environment:
      SRC_REPO: '[email protected]:vendor/src_repo.git'
      DEST_REPO: '[email protected]:2222/vendor/dest_repo.git'
      SRC_DEPLOY_KEY: 'base64_encoded_key=='
      DEST_DEPLOY_KEY: 'base64_encoded_key=='
    labels:
      - "traefik.enable=true"
      - "traefik.backend=gitbitlabhub_repo1"
      - "traefik.frontend.rule=Host:gitbitlabhub.example.com;PathPrefixStrip:/vendor/src_repo"
      - "traefik.port=8080"
      - "traefik.docker.network=webproxy"
    volumes:
      - ./gitbitlabhub:/storage

networks:
  webproxy:
    external: true

With this configuration the webhook url will be https://gitbitlabhub.example.com/vendor/src_repo

Known issues:

  • It will ignore a webhook if it is still processing the previous one. As a rule of thumb, wait 5-10 seconds between git push runs.

How to build

docker build --tag karser/gitbitlabhub:latest .
docker push karser/gitbitlabhub:latest