1. 程式人生 > >使用gitlab-mirrors同步github和本地gitlab上的程式碼

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

概述

我本地搭建了gitlab,為的就是可以儲存一些開源的專案到本地,為了保持專案是最新的,所以要和遠端的倉庫做同步,所以就要使用gitmirror了,網上大部分使用gitlab-mirrors都是使用ssh方式去拉取程式碼的,但是因為某些原因我只能使用http的方式去拉取程式碼所以我的教程中是沒有任何關於ssh的步驟的,所以不要驚訝

操作

系統初始化設定

首先安裝pip

yum install python-pip

之後使用pip安裝下面模組

pip install python-gitlab

之後你要在你的系統中建立一個gitmirror使用者

adduser gitmirror

切換到這個使用者

su - gitmirror

之後登陸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