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

和魁
2023-12-01

概述

我本地搭建了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

转载于:https://juejin.im/post/5c052323518825314143457c

 类似资料: