Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
karser committed Feb 10, 2020
0 parents commit c84091b
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/nbproject
/.idea
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM alpine:3.9

RUN apk add --no-cache git openssh-client

COPY ./mirror.sh /usr/local/bin
COPY ./server.sh /usr/local/bin

RUN mkdir -p /storage; \
chmod +x /usr/local/bin/mirror.sh; \
chmod +x /usr/local/bin/server.sh

EXPOSE 8080
VOLUME /storage
WORKDIR /storage

ENTRYPOINT ["/usr/local/bin/server.sh"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Dmitrii Poddubnyi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions mirror.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

set -eux

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch --prune
git remote set-head origin -d
git branch -a || 'true'
git push --prune dest +refs/remotes/origin/*:refs/heads/* +refs/tags/*:refs/tags/*
92 changes: 92 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# GitBitLabHub: Mirror your repositories between Bitbucket / Gitlab / Github using simple webhooks.
- 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.
- Single container mirrors a single repository.
- Single-threaded. It will ignore the next webhook while the previous one is being processed. DDoS protection out-of-the-box :).

## Why mirror repositories?
- You can pipe multiple services. For example, use Bitbucket private repositories
as a primary storage and self-hosted Gitlab instance as a purely CI/CD tool.
- Better safe than sorry. Mirroring repos is a single backup alternative for SaaS.

## How to use

### How to run using docker
```
docker run -it \
-e SRC_REPO=git@bitbucket.org:vendor/src_repo.git \
-e DEST_REPO=git@gitlab.example.com: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 can be obtained by running
```
base64 -w 0 < ~/.ssh/project_src_id_rsa
base64 -w 0 < ~/.ssh/project_dest_id_rsa
```

The 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: 'git@bitbucket.org:vendor/src_repo.git'
DEST_REPO: 'git@gitlab.example.com: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
```

The URL will be https://gitbitlabhub.example.com/vendor/src_repo

## How to setup mirroring

1. Generate 2 deployment keys for source and destination:
```
ssh-keygen -t rsa -f ~/.ssh/project_src_id_rsa
ssh-keygen -t rsa -f ~/.ssh/project_dest_id_rsa
```
Add these keys to the repository settings of source and destination correspondingly.
```
cat ~/.ssh/project_src_id_rsa.pub
cat ~/.ssh/project_dest_id_rsa.pub
```
The Source deployment key can be marked as readonly. Depending on the platform it's called Deploy Token or Access key.
See how to add deployment/access keys to [bitbucket](https://bitbucket.org/blog/deployment-keys), [bitbucket access keys](https://confluence.atlassian.com/bitbucketserver/ssh-access-keys-for-system-use-776639781.html),
[gitlab](https://docs.gitlab.com/ce/ssh/), [github](https://developer.github.com/v3/guides/managing-deploy-keys/).

2. Run this container with all environment variables configured properly (see [How to use](#how-to-use) section).

3. Create the webhook in the source repository.
See how to create a webhook in [bitbucket](https://confluence.atlassian.com/bitbucket/manage-webhooks-735643732.html),
[gitlab](https://docs.gitlab.com/ce/user/project/integrations/webhooks.html), [github](https://developer.github.com/webhooks/creating/).


## How to build

```
docker build --tag karser/gitbitlabhub:latest .
docker push karser/gitbitlabhub:latest
```
64 changes: 64 additions & 0 deletions server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/sh

set -eux

# Extract the protocol (includes trailing "://").
DEST_PROTO="$(echo $DEST_REPO | sed -nr 's,^(.*://).*,\1,p')"
# Remove the protocol from the URL.
DEST_URL="$(echo ${DEST_REPO/$DEST_PROTO/})"
# Extract the user (includes trailing "@").
DEST_USER="$(echo $DEST_URL | sed -nr 's,^(.*@).*,\1,p')"
DEST_USER=${DEST_USER:-git@}
# Remove the user from the URL.
DEST_URL="$(echo ${DEST_URL/$DEST_USER/})"
# Extract the port (includes leading ":").
DEST_PORT="$(echo $DEST_URL | sed -nr 's,.*:([0-9]+).*,\1,p')"
DEST_PORT=${DEST_PORT:-22}
# Remove the port from the URL.
DEST_URL="$(echo ${DEST_URL/$DEST_PORT/})"
# Extract the path (includes leading "/" or ":").
DEST_PATH="$(echo $DEST_URL | sed -nr 's,[^/:]*([/:].*),\1,p')"
# Remove the path from the URL.
DEST_HOST="$(echo ${DEST_URL/$DEST_PATH/})"


# Extract the protocol (includes trailing "://").
SRC_PROTO="$(echo $SRC_REPO | sed -nr 's,^(.*://).*,\1,p')"
# Remove the protocol from the URL.
SRC_URL="$(echo ${SRC_REPO/$SRC_PROTO/})"
# Extract the user (includes trailing "@").
SRC_USER="$(echo $SRC_URL | sed -nr 's,^(.*@).*,\1,p')"
SRC_USER=${SRC_USER:-git@}
# Remove the user from the URL.
SRC_URL="$(echo ${SRC_URL/$SRC_USER/})"
# Extract the port (includes leading ":").
SRC_PORT="$(echo $SRC_URL | sed -nr 's,.*:([0-9]+).*,\1,p')"
SRC_PORT=${SRC_PORT:-22}
# Remove the port from the URL.
SRC_URL="$(echo ${SRC_URL/$SRC_PORT/})"
# Extract the path (includes leading "/" or ":").
SRC_PATH="$(echo $SRC_URL | sed -nr 's,[^/:]*([/:].*),\1,p')"
# Remove the path from the URL.
SRC_HOST="$(echo ${SRC_URL/$SRC_PATH/})"
# name that can be used for the folder name
SRC_PROJECT="$(echo $SRC_PATH | sed -nr 's,.*/(.*)\.git,\1,p')"


mkdir -p ~/.ssh

eval `ssh-agent -s`
echo "$SRC_DEPLOY_KEY" | base64 -d | ssh-add -
echo "$DEST_DEPLOY_KEY" | base64 -d | ssh-add -
ssh -o StrictHostKeyChecking=no -T "$SRC_USER$SRC_HOST" -p "$SRC_PORT"
ssh -o StrictHostKeyChecking=no -T "$DEST_USER$DEST_HOST" -p "$DEST_PORT"

if [[ ! -d /storage/"$SRC_PROJECT" ]]; then
cd /storage
git clone --bare "$SRC_REPO" "$SRC_PROJECT"
cd /storage/"$SRC_PROJECT"
git remote add dest ssh://"$DEST_REPO"
fi

cd /storage/"$SRC_PROJECT"

while true; do nc -l -p 8080 -e sh -c 'echo -e "HTTP/1.0 200 OK\r\nDate: $(date)\r\nContent-Length: 2\r\n\r\nOK"; sh /usr/local/bin/mirror.sh;'; done

0 comments on commit c84091b

Please sign in to comment.