自动同步github项目到本地gitlab

许多公司选择gitlab用作内部代码管理软件。有时候可能需要将一些开源项目同步到内部gitlab,但是只有企业版的才支持该功能。 通过git+定时任务可以简单地实现该需求:

1.将github项目clone到本地

1
git clone https://github.com/xxxx/xxxx.git

2.添加用于同步github项目的内部gitlab远程仓库

1
2
3
4
5
6
7
8
git remote add gitlab http://gitlab.your_company.com/xxxx/auto-pull-github.git

# 查看远程remote
git remote -v
gitlab http://gitlab.your_company.com/xxxx/auto-pull-github.git (fetch)
gitlab http://gitlab.your_company.com/xxxx/auto-pull-github.git (push)
origin https://github.com/xxxx/xxxx.git (fetch)
origin https://github.com/xxxx/xxxx.git (push)
  1. 自动同步脚本
1
2
3
4
5
6
# git-auto-sync.sh
project_path=/path/to/project

cd $project_path
git pull origin master
git push gitlab master
  1. 设置定时任务
1
2
3
4
5
6
crontab -e
# 每小时同步执行一次同步脚本
0 */1 * * * /path/to/git-auto-sync.sh

# 查看定时任务执行情况
tail -f /var/log/cron