Commit a3c8e16f authored by John Fox Woland's avatar John Fox Woland

. Init

parents
/mirror-g2g.conf
# DESCRIPTION
This script helps you to create GitHub mirrors of your GitLab repositories. You should have filesystem access to your GitLab server as **root** or **\<gitlab system user>** to use it. Original respository: https://git.woland.me/adm/mirror-g2g
# CONFIGURATION
* Copy `mirror-g2g.conf.samle` to `mirror.g2g.conf`.
* Configure your github username, name of the gitlab system user and gitlab repositories dir (as described at **"Gitlab custom hooks"**).
* `su - <gitlab system user> -c ssh-keygen`.
* add **~\<gitlab system user>/.ssh/id_rsa.pub** to github deploy keys.
# USAGE
To create mirror of **\<group>/\<repository>.git**:
* Create **\<repository>** GitHub repository (or import it from GitLab with GitHub importing tool).
* Run `mirror-g2g.sh <group>/<repository>` as **root** or **\<gitlab system user>** user.
# HELP TOPICS
## StackExchange
1. GitLab to GitHub mirror configuring: https://stackoverflow.com/questions/21962872/how-to-create-a-gitlab-webhook-to-update-a-mirror-repo-on-github
See also:
https://stackoverflow.com/questions/14288288/gitlab-repository-mirroring
2. Checking for remote git repository: https://superuser.com/questions/227509/git-ping-check-if-remote-repository-exists
3. Scripting with 'su': https://stackoverflow.com/questions/3420280/can-i-run-su-in-the-middle-of-a-bash-script
## GitLab custom hooks
https://docs.gitlab.com/ee/administration/custom_hooks.html
## Native GitLab Enterprise mirroring feature
https://about.gitlab.com/2016/05/10/feature-highlight-push-to-remote-repository/
GITHUB_USER="wolandtel"
GITLAB_SYS_USER="git"
REPODIR="/var/opt/gitlab/git-data/repositories"
#!/bin/bash
scriptName="$(basename $0)"
cfgName=${scriptName/.sh/.conf}
cfg=("$(dirname $0)/$cfgName" $HOME/.config/$cfgName)
for c in ${cfg[@]}; do
[ -r "$c" ] && {
. "$c"
break
}
done
[[ -z "$GITHUB_USER" || -z "$GITLAB_SYS_USER" || -z "$REPODIR" ]] && \
{
if [ -r "$c" ]; then
echo "Configuration file '$c' is incomplete" >&2
else
echo "Configuration file doesn't exist" >&2
fi
exit 1
}
CH_DIR="custom_hooks"
HOOK_FILE="post-receive"
HOOK="/usr/bin/git push --quiet github &"
function usage ()
{
echo "USAGE: $scriptName <namespace>/<repository>"
}
if [[ $(id -u) -eq 0 ]]; then
su - $GITLAB_SYS_USER -c "'$0' $@"
let res=1000+$?
exit $res
fi
if [[ "$(id -un)" != "$GITLAB_SYS_USER" ]]; then
echo "ERROR: You should be root or $GITLAB_SYS_USER to perform operation" >&2
exit 2
fi
r=($(echo "$1" | sed -r 's#.*/([^/]+/[^/]+)#\1#; s#\.git$##; s#/# #'))
namespace=${r[0]}
repository=${r[1]}
if [[ -z "$namespace" || -z "$repository" ]]; then
usage
exit 3
fi
github="git@github.com:$GITHUB_USER/$repository.git"
gitlab="$REPODIR/$namespace/$repository.git"
[ -d "$gitlab" ] || {
echo "ERROR: GitLab repository $namespace/$repository doesn't exists" >&2
exit 4
}
git ls-remote -qh "$github" > /dev/null 2>&1 || {
echo "ERROR: GitHub repository '$repository' doesn't exists or deploy key hasn't been configured on GitHub" >&2
exit 5
}
cd "$gitlab"
git remote add --mirror=push github "$github" || exit 6
[ -d "$CH_DIR" ] || mkdir "$CH_DIR"
cd "$CH_DIR"
[ -e "$HOOK_FILE" ] && {
echo "ERROR: $(pwd)/$HOOK_FILE already exists. Please add string '$HOOK' manually if hook is bash script"
exit 7
}
cat > "$HOOK_FILE" <<EOF
#!/bin/bash
$HOOK
EOF
chmod 755 "$HOOK_FILE"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment