使用gitlab-mirrors同步github和本地gitlab上的代碼

概述

我本地搭建了gitlab,爲的就是能夠保存一些開源的項目到本地,爲了保持項目是最新的,因此要和遠程的倉庫作同步,因此就要使用gitmirror了,網上大部分使用gitlab-mirrors都是使用ssh方式去拉取代碼的,可是由於某些緣由我只能使用http的方式去拉取代碼因此個人教程中是沒有任何關於ssh的步驟的,因此不要驚訝python

操做

系統初始化設置git

首先安裝pipgithub

yum install python-pipweb

以後使用pip安裝下面模塊vim

pip install python-gitlabapi

以後你要在你的系統中建立一個gitmirror用戶bash

adduser gitmirrorssh

切換到這個用戶ide

su - gitmirrorsvn

以後登錄gitlab建立gitmirror用戶而且賦予Administrators權限

而且建立gitmirror-group組

以後拉取代碼

su - gitmirror
mkdir repositories
touch private_token
git clone https://github.com/samrocketman/gitlab-mirrors.git
cd gitlab-mirrors
chmod 755 *.sh
cp config.sh.SAMPLE config.sh

修改配置

vim config.sh

#Environment file

#
# gitlab-mirrors settings
#

#The user git-mirrors will run as.
system_user="gitmirror"
#The home directory path of the $system_user
user_home="/home/${system_user}"
#The repository directory where gitlab-mirrors will contain copies of mirrored
#repositories before pushing them to gitlab.
repo_dir="${user_home}/repositories"
#colorize output of add_mirror.sh, update_mirror.sh, and git-mirrors.sh
#commands.
enable_colors=true
#These are additional options which should be passed to git-svn.  On the command
#line type "git help svn"
git_svn_additional_options="-s"
#Force gitlab-mirrors to not create the gitlab remote so a remote URL must be
#provided. (superceded by no_remote_set)
no_create_set=false
#Force gitlab-mirrors to only allow local remotes only.
no_remote_set=false
#Enable force fetching and pushing.  Will overwrite references if upstream
#forced pushed.  Applies to git projects only.
bash-4.2$ cat config.sh
#Environment file

#
# gitlab-mirrors settings
#

#The user git-mirrors will run as.
system_user="gitmirror"
#The home directory path of the $system_user
user_home="/home/${system_user}"
#The repository directory where gitlab-mirrors will contain copies of mirrored
#repositories before pushing them to gitlab.
repo_dir="${user_home}/repositories"
#colorize output of add_mirror.sh, update_mirror.sh, and git-mirrors.sh
#commands.
enable_colors=true
#These are additional options which should be passed to git-svn.  On the command
#line type "git help svn"
git_svn_additional_options="-s"
#Force gitlab-mirrors to not create the gitlab remote so a remote URL must be
#provided. (superceded by no_remote_set)
no_create_set=false
#Force gitlab-mirrors to only allow local remotes only.
no_remote_set=false
#Enable force fetching and pushing.  Will overwrite references if upstream
#forced pushed.  Applies to git projects only.
force_update=false
#This option is for pruning mirrors.  If a branch is deleted upstream then that
#change will propagate into your GitLab mirror.  Aplies to git projects only.
prune_mirrors=false

#
# Gitlab settings
#

#This is the Gitlab group where all project mirrors will be grouped.
gitlab_namespace="gitmirror-group"
#This is the base web url of your Gitlab server.
gitlab_url="http://gitlab.bboysoul.com:88"
#Special user you created in Gitlab whose only purpose is to update mirror sites
#and admin the $gitlab_namespace group.
gitlab_user="gitmirror"
#Generate a token for your $gitlab_user and set it here.
gitlab_user_token_secret="$(head -n1 "${user_home}/private_token" 2> /dev/null || echo "")"
#Sets the Gitlab API version, either 3 or 4
gitlab_api_version=4
#Verify signed SSL certificates?
ssl_verify=true
#Push to GitLab over http?  Otherwise will push projects via SSH.
http_remote=true

#
# Gitlab new project default settings.  If a project needs to be created by
# gitlab-mirrors then it will assign the following values as defaults.
#

#values must be true or false
issues_enabled=false
wall_enabled=false
wiki_enabled=false
snippets_enabled=false
merge_requests_enabled=false
public=false

注意上面有個參數就是http_remote=true這個就是使用http去推送代碼的關鍵了。

完成以後在private_token這個文件里加入gitmirror用戶的Personal Access Tokens在你登錄gitmirror這個用戶以後,點擊用戶頭像,點擊設置,點擊Access Tokens建立就能夠了

以後能夠使用下面的命令去建立一個項目 ./add_mirror.sh --git --project-name gitlab-mirrors --mirror https://github.com/samrocketman/gitlab-mirrors.git

由於是http的方式因此會讓你輸入你的gitmirror這個帳號的帳號和密碼,建立完成以後就會在gitmirror-group這個組看到這個項目的代碼了

你能夠使用下面的命令刪除一個倉庫

./delete_mirror.sh --delete r410-fancontroller

執行下面命令手動同步代碼

./git-mirrors.sh

固然你能夠設置計劃任務來同步代碼

* 12 * * * /home/gitmirror/gitlab-mirrors/git-mirrors.sh

列出當前全部的項目

/ls-mirrors.sh

歡迎關注Bboysoul的博客www.bboysoul.com

Have Fun

相關文章
相關標籤/搜索